How to Put All Required Jar Files in a Library Folder Inside the Final Jar File with Maven

How do I put all required JAR files in a library folder inside the final JAR file with Maven?

following this link:

How To: Eclipse Maven install build jar with dependencies

i found out that this is not workable solution because the class loader doesn't load jars from within jars, so i think that i will unpack the dependencies inside the jar.

How to put all required jars in a lib folder inside the final jar with Maven without provided jars?

Add <excludeScope>provided</excludeScope> under <overWriteIfNewer>true</overWriteIfNewer>

How to add local jar files to a Maven project?

Install the JAR into your local Maven repository (typically .m2 in your home folder) as follows:

mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true

Where each refers to:

<path-to-file>: the path to the file to load e.g → c:\kaptcha-2.3.jar

<group-id>: the group that the file should be registered under e.g → com.google.code

<artifact-id>: the artifact name for the file e.g → kaptcha

<version>: the version of the file e.g → 2.3

<packaging>: the packaging of the file e.g. → jar

Reference

  • Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
  • Maven Install Plugin Usage: The install:install-file goal

How can I put all dependencies into one folder inside a JAR file with Maven?

No.

The structure of the JAR needs to follow the names of the packages.

How to add Maven dependency jar file from the lib folder

Have a look at system dependencies.

You basically need to define <scope>system</scope>.

<project>
...
<dependencies>
<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
</dependencies>
...
</project>

This is if you don't have your own hosted artifact repository server such as Nexus, Artifactory or Archiva.

If you do, then as Karl-Heinz suggested, you would be better off placing it there, as it's not good practice to commit binary artifacts to version control.

How to have a final jar with the dependency inside that jar in the jar format?

To make a fat jar that includes all you jar files, add the following code to your pom.xml. When you clean and build the project this will automatically make a fat jar file. You need to give your main class inside <mainClass> tag. That's it.

<project>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>final-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Including dependencies in a jar with Maven

You can do this using the maven-assembly plugin with the "jar-with-dependencies" descriptor. Here's the relevant chunk from one of our pom.xml's that does this:

  <build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>


Related Topics



Leave a reply



Submit