Maven – Specify the default main class in the built jar using Manifest

We can set the default main class in the META-INF/MANIFEST.MF. This could be configured in the maven-jar-plugin in pom.xml. Let’s use the previous example.

 

1. Edit the pom.xml as follow.

<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>info.ykyuen.eureka</groupId>
  <artifactId>testng-example</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>testng-example</name>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>info.ykyuen.eureka.Addition</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 

2. So instead of specify the main class after the build…

  • java -cp .\target\testng-example-1.0.jar info.ykyuen.eureka.Addition 12 32

 

3. We can now just simply run the jar file.

  • java -jar .\target\testng-example-1.0.jar 12 32

specify-default-main-class-in-jar-using-manifest-1
 

Done =)

Reference: How do I specify a main class in the manifest of my generated jar file?

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.