Thursday, February 26, 2015

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.4</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:
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</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

Labels

ad ( 3 ) Amazon ( 47 ) Answer ( 1 ) API ( 22 ) automation ( 1 ) blog ( 21 ) book ( 2 ) career ( 12 ) cdn ( 2 ) centos ( 1 ) cloud ( 115 ) CRM ( 20 ) current ( 9 ) data ( 14 ) database ( 61 ) design patterns ( 14 ) development ( 3 ) eCommerce ( 23 ) email ( 92 ) ETL ( 2 ) framework ( 3 ) git ( 7 ) google ( 174 ) IDE ( 10 ) Integration ( 1 ) interview ( 15 ) Java ( 355 ) Java EE ( 99 ) javascript ( 39 ) Jboss ( 4 ) JPA ( 48 ) JSF ( 44 ) linux ( 312 ) Marketing ( 18 ) mobile ( 42 ) model ( 1 ) MySQL ( 191 ) online money ( 11 ) OOP ( 7 ) open source ( 26 ) performance ( 69 ) php ( 46 ) python ( 47 ) quality ( 5 ) referral ( 3 ) restful ( 22 ) security ( 126 ) SEO ( 17 ) social ( 26 ) software ( 72 ) spam ( 4 ) Spring ( 1 ) SQL ( 22 ) system ( 29 ) testing ( 14 ) tool ( 72 ) UML ( 8 ) virtual ( 10 ) web ( 216 ) web services ( 13 ) WildFly ( 3 ) wordpress ( 7 )