Reputation: 3695
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?
Upvotes: 0
Views: 1681
Reputation: 137269
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:
build
. This is set by the <baseDirectory>
parameter.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.<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}
.
Upvotes: 3