将 Maven 应用程序打包成一个 jar - 有一个例外

Package maven app into a single jar - with an exception

我的应用程序中有一些依赖项,我希望在构建期间将所有依赖项打包到一个 jar 中 - 除了一个依赖项(团队的实用程序 jar)。 了解如何使用 maven-assembly-plugin 构建到单个 jar,不知道如何排除特定依赖项。

这是相关的 pom.xml 文件:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file://repository_path</url>
    </repository>
</repositories>
....
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.10</source>
                <target>1.10</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>app-name</finalName>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.company.appname.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>
    </plugins>
</build>
...
<dependencies>
    <dependency>
        <groupId>com.company.utils</groupId>
        <artifactId>utils</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

可能Including and Excluding Artifacts对你有帮助。

更具体地说,您需要创建自己的 assembly.xml 文件:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>jar-with-dependencies-exclude</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <excludes>
                <exclude>com.fasterxml.jackson.datatype:jackson-datatype-jsr310</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

然后更改您的插件配置以使用此文件:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <finalName>app-name</finalName>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.company.appname.Main</mainClass>
            </manifest>
        </archive>
        <descriptors>
            <descriptor>src/main/assembly.xml</descriptor>
        </descriptors>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
</plugin>