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

Thursday, June 27, 2013

How to create an executable jar file with maven

Executable jars may be created using the maven shade plugin. The shade plugin allows for the creation of 'uber-jars'. This are self contained jar files which contain the application code as well as all dependencies.
The shade plugin is configured in the build-section of your pom.xml:
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

To make the jar file executable, is has to contain an MANIFEST.MF which names the class containing the main function. Just add the following lines to the plugin config:

<configuration>
  <transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
      <mainClass>path.to.MainClass</mainClass>
    </transformer>
  </transformers>
</configuration>

See also: http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

Monday, December 27, 2010

How To Create An Empty Java Project With Maven

To create an empty maven-managed java project simply type:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

You will be asked for the groupId and artifactId for your project, the project version and the main package for your code.

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId': : cloudobjects
Define value for property 'artifactId': : demoProject
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  cloudobjects: : de.cloudobjects
Confirm properties configuration:
groupId: cloudobjects
artifactId: demoProject
version: 1.0-SNAPSHOT
package: de.cloudobjects
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: cloudobjects
[INFO] Parameter: packageName, Value: de.cloudobjects
[INFO] Parameter: package, Value: de.cloudobjects
[INFO] Parameter: artifactId, Value: demoProject
[INFO] Parameter: basedir, Value: /Users/helmut
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: /Users/helmut/demoProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.293s
[INFO] Finished at: Mon Dec 27 15:08:58 CET 2010
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------


Now you have a simple maven-managed java project.

If you are using eclipse you have (at least) two options:

To create an eclipse project from your maven project cd into the project-directory and type:

$ mvn eclipse:eclipse

If you are using the M2Eclipse-Plugin, you can import the maven project directly into eclipse.

To import a maven project into eclipse choose "File -> Import" and select "Maven -> Existing Maven Projects":


In the next step select the newly created project folder and the projects pom.xml:


Now you have an eclipse-project with full maven dependency management.

More information about the M2Eclipse-Plugin can be found in the sonatype M2Eclipse manual.