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