How to create a jar with Maven that includes dependencies in a separate folder for each dependency

GotoFinal

I want build a Maven project with dependencies outside the .jar, but every dependency must be in a separate directory as:

libs/${dependency.groupId}/${dependency.artifactId}/${dependency.version}

Just like this:

/build
  my.jar
  /libs
    /org.yaml
      /snakeyaml
        /1.17
          snakeyaml-1.17.jar
    /etc...

I know that there is a maven-dependency-plugin, but how to configure it like that? Or there is any other plugin that can do this?

Tunaki

What you really want to do here is to create a custom archive for your project. For that, you want to use the maven-assembly-plugin.

There are multiple aspects to consider:

  • You want a base directory of build. This is set by the <baseDirectory> parameter.
  • You want to have all of the project's dependencies in a repo-like directory layour. For that, you can use a custom outputFileNameMapping for the dependencySet. This enables to use a set of properties; in this case we're interested in the group id, artifact id and version. We need to disable <useProjectArtifact>, otherwise it would also include the project main artifact.
  • For the main artifact, we simply use a <file> element, pointing to the main artifact of the project.

The assembly descriptor would look like:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>dir</format> <!-- to turn into final archive type -->
    </formats>
    <baseDirectory>build</baseDirectory>
    <files>
        <file>
            <source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
            <outputDirectory>libs</outputDirectory>
            <outputFileNameMapping>${artifact.groupId}/${artifact.artifactId}/${artifact.version}/${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}</outputFileNameMapping>
            <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
    </dependencySets>
</assembly>

Note that it uses a <format> of dir, meaning that it does not "package" the output structure inside an archive but leaves it as a directory structure. In the future, you'll be able to customize this assembly descriptor with your launcher scripts and make a final archive of it by changing this format to the one you'll want.

Finally, the configuration of the plugin itself will be

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor> <!-- path to the above assembly descriptor -->
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

With such a configuration, launching mvn clean install will output the wanted directory structure inside target/${finalName}.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to find the usage of a jar imported by maven dependency?

From Java

How to create a jar without meta-inf folder in maven?

From Java

Intellij Maven: Create jar with all library dependencies

From Java

How to include maven dependencies in a jar file?

From Java

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

From Java

How can I create an executable JAR with dependencies using Maven?

From Java

How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

From Java

How do you create a maven assembly that includes shell scripts and properrties file not in jar?

From Java

How can I create an executable jar without dependencies using Maven?

From Java

How To: Eclipse Maven install build jar with dependencies

From Java

How to create a runnable jar with maven that includes dependencies in a separate "lib" folder (not an uber jar)

From Java

How to download maven dependency as *.jar file?

From Java

How to get path to dependency jar with maven

From Dev

Maven dependency plugin copy jar with dependencies

From Dev

How to create maven uber jar which includes dependencies with scope provided

From Dev

maven dependency jar file displayed as folder icon

From Dev

Does sbt/maven includes the complete dependency in jar file?

From Dev

How to unpack just the files from a sub-folder in maven-dependency-plugin with goal unpack-dependencies?

From Dev

How do I create a Netbeans style Jar with all dependencies in a lib folder?

From Dev

Maven: how to include module dependencies into general dependency-jars folder

From Dev

How do I create an executable jar file including dependencies where the main class is in Test, using Maven

From Dev

how to add jar dependency in maven?

From Dev

Create a jar that contain dependency management maven

From Dev

Maven: How to have jar-with-dependencies exclude "provided" dependencies?

From Dev

Maven - How to exclude dependencies from generated JAR

From Dev

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

From Dev

How to create jar file without java classes but with a folder using maven

From Dev

How can I create an executable/runnable JAR with dependencies using Maven?

From Dev

How to create a separate file inside each folder using pathlib in python?

Related Related

  1. 1

    How to find the usage of a jar imported by maven dependency?

  2. 2

    How to create a jar without meta-inf folder in maven?

  3. 3

    Intellij Maven: Create jar with all library dependencies

  4. 4

    How to include maven dependencies in a jar file?

  5. 5

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

  6. 6

    How can I create an executable JAR with dependencies using Maven?

  7. 7

    How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

  8. 8

    How do you create a maven assembly that includes shell scripts and properrties file not in jar?

  9. 9

    How can I create an executable jar without dependencies using Maven?

  10. 10

    How To: Eclipse Maven install build jar with dependencies

  11. 11

    How to create a runnable jar with maven that includes dependencies in a separate "lib" folder (not an uber jar)

  12. 12

    How to download maven dependency as *.jar file?

  13. 13

    How to get path to dependency jar with maven

  14. 14

    Maven dependency plugin copy jar with dependencies

  15. 15

    How to create maven uber jar which includes dependencies with scope provided

  16. 16

    maven dependency jar file displayed as folder icon

  17. 17

    Does sbt/maven includes the complete dependency in jar file?

  18. 18

    How to unpack just the files from a sub-folder in maven-dependency-plugin with goal unpack-dependencies?

  19. 19

    How do I create a Netbeans style Jar with all dependencies in a lib folder?

  20. 20

    Maven: how to include module dependencies into general dependency-jars folder

  21. 21

    How do I create an executable jar file including dependencies where the main class is in Test, using Maven

  22. 22

    how to add jar dependency in maven?

  23. 23

    Create a jar that contain dependency management maven

  24. 24

    Maven: How to have jar-with-dependencies exclude "provided" dependencies?

  25. 25

    Maven - How to exclude dependencies from generated JAR

  26. 26

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

  27. 27

    How to create jar file without java classes but with a folder using maven

  28. 28

    How can I create an executable/runnable JAR with dependencies using Maven?

  29. 29

    How to create a separate file inside each folder using pathlib in python?

HotTag

Archive