Executable JAR by maven

You can simply use

java -cp path-to-your-jar:path-to-your-lib your-full-main-class-name 

to run the jar.

For example:
java -cp /i88/ca/example.jar:/i88/ca/lib/* it.i88.ca.example.App

If you encounter "Failed to load Main-Class manifest attribute" problem, this is probably because you have no Main-Class attribute specified in the manifest file.

To create an executable JAR, one simply needs to set the main class that serves as the application entry point.

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>ca.i88.Main</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
This will add

Main-Class: ca.i88.App

in the Manifest.txt when you "mvn clean install" or "mvn package". By this way this application's entry point is set.

Another plugin can do the same (Recommended):

Add the maven-shade-plugin plugin and rebuild.
The maven-shade-plugin will take artifacts (jars) produced by the package goal (produces customer code .jar), and created a standalone .jar that contains the compiled customer code, and the resolved dependencies from the pom.xml.

Within Eclipse:
  1. Open the context (right-click) menu for the pom.xml file, choose Maven, and then choose Add Plugin.
  2. In the Add Plugin window, type the following values:
    • Group Id: org.apache.maven.plugins
    • Artifact Id: maven-shade-plugin
    • Version: 3.2.1
  3. Now build again.
    This time we will create the jar as before, and then use the maven-shade-plugin to pull in dependencies to make the standalone .jar.
    1. Open the context (right-click) menu for the project, choose Run As, and then choose Maven build ....
    2. In the Edit Configuration windows, type package shade:shade in the Goals box.
    3. Choose Run.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
          <execution>
            <phase>package</phase>

            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>

                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>ca.i88.App</mainClass>
                </transformer>
              </transformers>
            </configuration>

          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Popular posts from this blog

How to delete / clear queue of PowerMTA