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>
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With