Road to create executable jar using maven

In this post I explained the procedure to create executable jar file using maven build tool.

You need to create java maven project using eclipse.

Here is example of a java class having main method.

package com.test;
 import org.junit.Assert;
 public class VerifyString {

   public boolean matchString(String str1, String str2){
      if(str1.equals(str2)){
          System.out.println("both strings are equal");
          return true;
      }
      else {
          System.out.println("both strings are not equal");
          return false;
        }
    }
    public static void main(String[] str){
      VerifyString verifyString = new VerifyString();
      Assert.assertTrue(verifyString.matchString("Akib", "Akib"));
      Assert.assertFalse(verifyString.matchString("Akib", "Sachin"));  
   }
}
pom.xml

 <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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>maven-junit-test</groupId>
  <artifactId>maven-junit-test</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>maven-junit-test</name>
 
  <dependencies>
      <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.3</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>maven-junit-test</finalName>
    <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-assembly-plugin</artifactId>
         <version>2.2.1</version>
         <executions>
            <execution>
               <id>package-jar-with-dependencies</id>
               <phase>package</phase>
               <goals>
                  <goal>single</goal>
               </goals>
               <configuration>
                 <appendAssemblyId>false</appendAssemblyId>
                 <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                 </descriptorRefs>
                 <archive>
                   <manifest>
                      <mainClass>com.test.VerifyString</mainClass>
                   </manifest>
                 </archive>
               </configuration>
            </execution>
         </executions>
       </plugin>
    </plugins>
  </build> 
</project>

Run below commands to create jar

  
  mvn clean
  mvn install

After build success, you should see a “maven-junit-test.jar” executable jar file created under “traget” folder in maven java project.

No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution