06 November 2016

Easy Way to Create wrapper for Java Project

Most of the articles I have written for android related development issues. I thought need to write something new for java and spring related article.when I was in university when we running project what wed did was always run the project using IDE. But after joining to industry we know that we have to write executable file to run project in different environments. Let's say customer server may be windows, linux or mac .Then we have to create executable file which run in all platforms. If the project is simple what we can do is create executable jar and give to customer.

 But if the project contents configuration files this approach is not good.  Then we have to use better way to bundle executable files. So in this tutorial I will show you to how to create wrapper for java project but make sure you have to use maven.

First you have to add following property to pom.xml

1
2
3
<properties>
    <wrapper.location>${project.build.directory}/generated-resources/appassembler/jsw/#wrapper-name</wrapper.location>
</properties>

Then need to add following plugins to pom.xml and make sure change the main class and wrapper name.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-resources</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/conf</outputDirectory>
                    <resources>
                        <resource>
                            <directory>${project.basedir}/src/main/resources</directory>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <excludes>
                <exclude>**/*.yml</exclude>
                <exclude>**/*.txt</exclude>
            </excludes>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>appassembler-maven-plugin</artifactId>
        <version>1.10</version>
        <executions>
            <execution>
                <id>generate-jsw-scripts</id>
                <phase>package</phase>
                <goals>
                    <goal>generate-daemons</goal>
                </goals>
                <configuration>
                    <repositoryLayout>flat</repositoryLayout>
                    <configurationDirectory>conf</configurationDirectory>
                    <daemons>
                        <daemon>
                            <id>#wrapper-name</id>
                            <wrapperMainClass>org.tanukisoftware.wrapper.WrapperSimpleApp</wrapperMainClass>
                            <mainClass>#Main-class</mainClass>
                            <commandLineArguments>
                                <commandLineArgument>start</commandLineArgument>
                            </commandLineArguments>
                            <platforms>
                                <platform>jsw</platform>
                            </platforms>
                            <generatorConfigurations>
                                <generatorConfiguration>
                                    <generator>jsw</generator>
                                    <configuration>
                                        <property>
                                            <name>wrapper.java.additional.1</name>
                                            <value>-Xloggc:logs/gclog</value>
                                        </property>
                                        <property>
                                            <name>wrapper.java.additional.2</name>
                                            <value>-XX:MaxDirectMemorySize=256M</value>
                                        </property>
                                        <property>
                                            <name>configuration.directory.in.classpath.first</name>
                                            <value>conf</value>
                                        </property>
                                        <property>
                                            <name>set.default.REPO_DIR</name>
                                            <value>lib</value>
                                        </property>
                                        <property>
                                            <name>wrapper.logfile</name>
                                            <value>logs/wrapper.log</value>
                                        </property>
                                    </configuration>
                                    <includes>
                                        <include>linux-x86-32</include>
                                        <include>linux-x86-64</include>
                                    </includes>
                                </generatorConfiguration>
                            </generatorConfigurations>
                        </daemon>
                    </daemons>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <executions>
            <execution>
                <id>make-log-dir</id>
                <phase>package</phase>
                <configuration>
                    <tasks>
                        <mkdir dir="${wrapper.location}/logs" />
                        <copy todir="${wrapper.location}/conf">
                            <fileset dir="target/conf" />
                        </copy>
                        <chmod dir="${wrapper.location}/bin" includes="**/*" perm="0755" />
                    </tasks>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.3</version>
        <configuration>
            <descriptor>src/assembly/dep.xml</descriptor>
            <finalName>#app-name</finalName>
        </configuration>
        <executions>
            <execution>
                <id>create-archive</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>




And final Step is need to add assembly file to your project. Create assembly folder inside src folder and put the following dep.xml file in to it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<formats>
    <format>zip</format>
</formats>
<fileSets>
    <fileSet>
        <directory>${wrapper.location}</directory>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>
</assembly>

And now you can see the wrapper-name.zip file in target folder. after extracting it inside the bin folder four executable files are there and bat file for windows and wrapper for linux . All the dependency jar files are under lib folder. and configurations in conf folder. Now in this way we can easily handle projects. Hope now you will learn something from my tutorial.

Android Dynamically Change Style colors

In this Tutorial I will discuss How to Change Android Style theme colors. Last few days I had to I have searched how to change android theme style color dynamically but android doesn't support to change theme colors dynamically. (Note :You can changed android pre-defined theme styles dynamically not the dynamic generated themes)

If we using Theme.AppCompat.Light.DarkActionBar then these are the main properties in layout.



If You want to change theme what you did was change the primary color and primaryDark color. And this code helps you to dynamically change the theme (above mention colors) colors.

This is for change primary Dark color.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static void setStatusBarColor(String color) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = currentActivity.getWindow();
        int statusBarColor = Color.parseColor(color);

        if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }
        window.setStatusBarColor(statusBarColor);
    }
}




This is for change primary color


1
2
getSupportActionBar().setBackgroundDrawable(
        new ColorDrawable(Color.parseColor("#AA3939")));



I hope this article help you to resolve your problem.
Thanks