Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only create executable jar-with-dependencies in Maven

Tags:

maven

jar

In my pom.xml I use the maven-assembly-plugin to create an executable jar-with-dependencies when running "mvn clean install". Now it first creates the non-executable "name-version.jar" and after that the "name-version-jar-with-dependencies.jar".

Can I configure the pom.xml somehow, so that it doesn't create the non-executable JAR file?

At the moment I use <appendAssemblyId>false</appendAssemblyId> so it just overwrites the first file...

Also I get several "... already added, skipping" messages. Can I somehow prevent them?

This is the maven-assembly-plugin definition in my pom.xml:

<plugin>     <artifactId>maven-assembly-plugin</artifactId>     <version>2.2-beta-5</version>     <configuration>         <appendAssemblyId>false</appendAssemblyId>         <archive>             <manifest>                 <mainClass>my.main.class</mainClass>             </manifest>         </archive>         <descriptorRefs>             <descriptorRef>jar-with-dependencies</descriptorRef>         </descriptorRefs>     </configuration>     <executions>         <execution>             <id>make-assembly</id>             <phase>package</phase>             <goals>                 <goal>single</goal>             </goals>         </execution>     </executions> </plugin> 
like image 355
phil Avatar asked Feb 16 '11 12:02

phil


People also ask

How do I create an executable jar of Maven project?

First, we specify the goal copy-dependencies, which tells Maven to copy these dependencies into the specified outputDirectory. In our case, we'll create a folder named libs inside the project build directory (which is usually the target folder). The most important part of this is the manifest configuration.

Which maven command creates an executable JAR file with all dependencies of the project?

Use the maven-shade-plugin to package all dependencies into one über-JAR file. It can also be used to build an executable JAR file by specifying the main class.

How do I create an executable JAR file?

In Eclipse you can do it simply as follows : Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.

Does a JAR file contain all dependencies?

Normally, when we package a project into a jarW file, the jar file doesn't contain its dependencies, so the dependency jar files would need to be included in the classpathW in order to execute a class in the project's jar file that uses one of the dependencies.


1 Answers

I got it to work using the maven-jar-plugin and the single goal of maven-assembly-plugin like this

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-assembly-plugin</artifactId>     <version>3.1.0</version>     <executions>         <execution>             <phase>package</phase>             <goals>                 <goal>single</goal>             </goals>             <configuration>                 <finalName>finalName</finalName>                 <archive>                     <manifest>                         <mainClass>                             mainClass                         </mainClass>                     </manifest>                 </archive>                 <descriptorRefs>                     <descriptorRef>jar-with-dependencies</descriptorRef>                 </descriptorRefs>             </configuration>         </execution>     </executions> </plugin> 

Anyway the most important thing is to tell maven not to generate the default jar using this configuration of the maven-jar-plugin

<plugin>     <artifactId>maven-jar-plugin</artifactId>     <version>3.0.2</version>     <executions>         <execution>             <id>default-jar</id>             <phase>none</phase>         </execution>     </executions> </plugin> 
like image 95
Matteo Casaro Avatar answered Sep 18 '22 21:09

Matteo Casaro