简体   繁体   中英

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

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?

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} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM