Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

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.

22 November 2014

Generate webapp using maven archetype plugin

In this example describes how to create web app using maven archetype plugin.And here i used jetty server. First You have to install maven in your computer. http://mavenforu.blogspot.com/2013/09/maven-installation-guide.html in here i have explain how to install maven in windows.So then go to your workspace and type this command.

mvn archetype:generate -DgroupId=com.sajith.maven -DartifactId=MyFirstWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
GroupId and DartifactId can be change to your package name and project name.

Then Change the POM file and add the jetty maven plugin.Inside the Plugins notation add these plugin.
<plugin>
     <groupId>org.mortbay.jetty</groupId>
     <artifactId>maven-jetty-plugin</artifactId>
     <version>6.1.10</version>
     <configuration>
     <scanIntervalSeconds>10</scanIntervalSeconds>
     <connectors>
     <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
          <port>8080</port>
          <maxIdleTime>60000</maxIdleTime>
     </connector>
     </connectors>
     </configuration>
 </plugin>
Then Enter this command
mvn clean install
mvn jetty:run

How to use maven for Android


If you are Android developer you must used some development IDE(eclipse, idea) for developingandroid application.

But when you want to build the APK file you no need to use IDE. So Maven introduce the maven build plugin for android.

Here i have describe how to install maven(http://sajithforu.blogspot.com/2014/10/maven-installation-guide.html).

First you need android keystore for verify the apk file.So this will help you to how to create keystore in android.(http://developer.android.com/tools/publishing/app-signing.html).

First you have to install android sdk development kit in your machine.Then First set ANDROID_HOME path variable in your machine.



Second step genarate the keystore set KEYSTORE_HOME path variable in your PC.




Third create your android application using any IDE.

Inside the project create pom.xml file and put this code. and change the  App_name and relevant changes.

Make sure you have correct platform version



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>App</groupId>
        <artifactId>modules</artifactId>
        <version>1.0.8</version>
    </parent>
 
    <groupId>App</groupId>
    <artifactId>App</artifactId>
    <version>1.0.8</version>
    <packaging>apk</packaging>
    <name>App-name</name>
 
    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.roboguice</groupId>
            <artifactId>roboguice</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>App</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jarsigner-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <id>signing</id>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                        <phase>package</phase>
                        <inherited>true</inherited>
                        <configuration>
                            <includes>
                                <include>target/*.apk</include>
                            </includes>
                            <keystore>${env.KEYSTORE_HOME}/android-pos.keystore</keystore>
                            <storepass>keystorepassword</storepass>
                            <keypass>keypass</keypass>
                            <alias>your_alias</alias>
                            <verbose>true</verbose>
                            <arguments>
                                <argument>-sigalg</argument>
                                <argument>MD5withRSA</argument>
                                <argument>-digestalg</argument>
                                <argument>SHA1</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <release>true</release>
                    <sdk>
                        <platform>17</platform>
                        <path>${env.ANDROID_HOME}</path>
                    </sdk>
                    <sign>
                        <debug>false</debug>
                    </sign>
                    <extractDuplicates>true</extractDuplicates>
                    <proguard>
                        <skip>false</skip>
                        <config>proguard.cfg</config>
                        <configs>
                            <config>${env.ANDROID_HOME}/tools/proguard/proguard-android.txt</config>
                        </configs>
                        <filterMavenDescriptor>false</filterMavenDescriptor>
                        <filterManifest>false</filterManifest>
                        <jvmArguments>
                            <jvmArgument>-Xms256m</jvmArgument>
                            <jvmArgument>-Xmx512m</jvmArgument>
                        </jvmArguments>
                    </proguard>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>maven-android-plugin</artifactId>
                <configuration>
                    <zipalign>
                        <verbose>true</verbose>
                        <skip>false</skip>            
                        <inputApk>${project.build.directory}/${project.artifactId}-${project.version}.apk</inputApk>
                        <outputApk>${project.build.directory}/${project.artifactId}-${project.version}-RELEASE.apk>
                        </outputApk>
                    </zipalign>
                    <manifest>
                        <debuggable>false</debuggable>
                        <versionCodeAutoIncrement>true</versionCodeAutoIncrement>
                    </manifest>
                    <proguard>
                        <skip>true</skip>
                    </proguard>
                </configuration>
                <executions>
                    <execution>
                        <id>zipalign</id>
                        <phase>install</phase>
                        <goals>
                            <goal>zipalign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>findbugs-maven-plugin</artifactId>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


Then go to the project and run these maven cvommands to compile and run the project.
         mvn clean install
         mvn android:compile
         mvn android:run

And that is all.Thanks

19 November 2014

Java JDBC connection using maven

In this article I will describe how to create jdbc connection and read values from mysql database.

First In your POM file add the following dependency.

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.17</version>
     <type>jar</type>
     <scope>compile</scope>
 </dependency>

Then From your java application you can check the Jdbc connection.In my application i have database called java_form  and table called user_details.

In here i have First check the database connection.And next method insert data to database. Then Retrieve data from database after all  close the connection.

public class DataBaseConnectivity {
    Connection connection;

    /**
     *  Connect database
     * @return
     */
    public boolean connect_database() {
        boolean check_connection = false;
        String database_Url= "jdbc:mysql://localhost/java_form";
        String database_Class= "com.mysql.jdbc.Driver";        
        String username = "root";
        String password = "";
        try {
            Class.forName(database_Class);
          connection = DriverManager.getConnection(database_Url, username, password);          
           
            if (connection != null) {
                check_connection = true;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return check_connection;
    }

    /**
     *  Insert DATA to table
     * @param uId
     * @return
     */
    public boolean insert_uId(String uId) {
        String query = "INSERT INTO user_details (uID)VALUES (" + uId + ")";
        Statement statement = null;
        try {
            statement = connection.createStatement();
            int resultSet = statement.executeUpdate(query);
            System.out.println("UID added to database");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return true;
    }

    /**
     *   Retrive data from table
     * @return
     */
    public boolean retrive_Data() {
        String query = "Select uID from user_details";

        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                System.out.println("UID value : " + tableName);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return true;
    }
}

21 October 2014

Maven Installation guide

In this Thread i am going to how to use maven.First i will give you what is Maven.Maven is open source Building tool.it is distributed by Apache.If you really new with software development cycle i will tell why we want to use building tool.In software development cycle there are five main development cycles.Planing,Implementation,testing and documenting and Deployment and Maintenance are the development process.So when you build some product there may be bugs,errors.So you have to give the Quality output to your customer.So then you have to testing and development except most bugs fixed.So it is high cost for fixed errors and rebuild.So that's why we used building tool for make easier  our development process.Using Building tool we can test application,documentation easier.

How to install MAVEN in to windows

1 Step :
Download the maven using this url
(http://maven.apache.org/download.cgi)  and extract the zip file in any drive in your machine.

2 Step :
Download and install java in to your machine.

3 Step :
Set Path variables for java and maven.




4 Step:
Check maven is installed properly.


Thanks