I am trying to create one jar file all the dependent jar files in it. Maven creates a separate lib folder and copies all the jars into it then the executable jar works as the jar and the lib are at the same location but I want the jars to be part of the main jar and not outside in a lib folder. How do I do this?
Asked
Active
Viewed 1,035 times
2 Answers
1
You should use the Maven Assembly plugin or the Maven Shade plugin.
The fact is that the JAR file format doesn't have native support for including dependencies inside itself (there's a 20-year old RFE logged for it). So you can achieve what you're asking for but it will always be some sort of workaround. For example you have to be careful about possible file name conflicts - that is something that the Shade plugin is trying to solve, the Assembly plugin is easier to use, IMHO, but will keep overwriting files.
Have a look at this example how to use the assembly plugin and how to deal with DLLs: Package Dll in Jar using Maven- single goal

Community
- 1
- 1

Jakub Kotowski
- 7,411
- 29
- 38
-
1I tried shade plugin but got this as I am using a dll also to jar: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.2:shade (default) on project MIM: Error creating shaded jar: error in opening zip file C:\Users\adeb.m2\repository\com\aware\nistpack\5.0.4\nistpack-5.0.4.dll -> [Help 1] Please help – Abhijit Deb Dec 19 '13 at 17:44
-
I'm not sure how it's with shade and DLLs because shade is working with the java classes themselves. With assembly it might be easier, have a look at: http://stackoverflow.com/questions/11578903/package-dll-in-jar-using-maven-single-goal – Jakub Kotowski Dec 19 '13 at 19:04
-
@AbhijitDeb maybe see https://stackoverflow.com/questions/20098271/maven-project-with-pi4j-library-error-in-opening-zip-file – rogerdpack Dec 14 '20 at 17:49
0
<build>
<finalName>MIM</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.tsc.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<versionRange>
[2.1,)
</versionRange>
<goals>
<goal>prepare-package</goal>
<goal>
copy-dependencies
</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Abhijit Deb
- 39
- 2
- 9