Maven assembly plugin add dependencies

2020-06-30 03:38发布

问题:

I have a java application and using maven-assembly-plugin I want to add my dependencies to a /lib folder in my release.

In a web project its simple because jars are already in the war file. I coudln't figure out to do it in standart java application since all external jars are in my local .m2 repository.

回答1:

I often work with this assembly configuration

<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>assembly</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <outputFileNameMapping>${artifact.groupId}.${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
            <useProjectArtifact>false</useProjectArtifact>
            <!-- you may place excludes here -->
        </dependencySet>
    </dependencySets>

    <files>
        <file>
            <outputDirectory>/</outputDirectory>
            <source>${project.build.directory}/${project.artifactId}-${project.version}.jar</source>
            <destName>${project.artifactId}.jar</destName>
        </file>
    </files>

    <fileSets>
        <fileSet>
            <outputDirectory>config</outputDirectory>
            <directory>config</directory>
        </fileSet>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>src/main/bin</directory>
        </fileSet>
    </fileSets>

</assembly>

And then I reference it in my POM with

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>create-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

If you also need a runnable JAR:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifestEntries>
                <Class-Path>config/</Class-Path>
            </manifestEntries>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <classpathLayoutType>custom</classpathLayoutType>
                <customClasspathLayout>$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension}</customClasspathLayout>
            </manifest>
        </archive>
    </configuration>
</plugin>