Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put all dependencies in separate folder for runnable jar?

Tags:

java

maven

I'm using mvn package to create a runnable jar with all dependencies packed inside, which runs fine. But I'd prefer to have all external dependencies packed in a separate folder. What would I have to change therefore?

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                    <mainClass>my.MainApp</mainClass>
                </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        </plugin>
like image 289
membersound Avatar asked Apr 11 '14 13:04

membersound


People also ask

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.

Which tag is used to include dependency jar file details?

You can use the newly created jar using a <classifier> tag.

What is a jar with dependencies?

The value jar-with-dependencies tells Maven to build a JAR file with dependencies which is another term for a Fat JAR. The executions XML element tells Maven which Maven build phase and goal this Maven plugin should be executed during. The maven-assembly-plugin should always be executed during the package phase.

How to create single executable jar with dependencies in Maven?

Maven - Creating a Single executable Jar with dependencies. Apache Maven Assembly plugin allows to create an executable jar which includes all its dependencies within it. The resultant jar is also called 'fat jar'. Create a project using maven-archetype-quickstart with following parameters.

Is MyMy jar an executable jar?

my.jar will not be an executable JAR since it won't contain itself the dependency classes. It sounds like you want to create an assembly, like a custom ZIP? @Tunaki it don't need to contain dependency classes, in real project there will be launcher too, that will get all libraries and run that .jar with valid classpath. I will edit title.

Can I create a JAR file without using the IDE?

When creating a jar file, we usually want to run it easily, without using the IDE. To that end, we'll discuss the configuration and pros/cons of using each of these approaches for creating the executable. A quick and practical guide to building and managing Java projects using Apache Maven. Where is the Maven Local Repository?

How do I use the dependency plugin?

The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location. The plugin analyses the dependencies in your project, and copies dependencies from Maven Central (or the repository you have configured).


Video Answer


1 Answers

Use the maven-dependencies-plugin to specify an output directory for the copy-dependencies execution.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
        <goal>copy-dependencies</goal>
        </goals>
        <configuration>
        <outputDirectory>${project.build.directory}/lib/</outputDirectory>
        </configuration>
      </execution>
    </executions>
</plugin>

Update:

To let the jar know where to find the lib folder, you can specify this as a Class-Path value in the manifest using the maven-jar-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <archive>
        <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <mainClass>foo.bar.MainClass</mainClass>
        </manifest>
    </archive>
    </configuration>
</plugin>

Hope this helps.

like image 197
Will Avatar answered Oct 27 '22 00:10

Will