将Maven应用程序打包到一个jar中-例外

时间:2018-12-11 15:49:58

标签: java maven jar maven-assembly-plugin

我的应用程序中有一些依赖项,我希望在构建过程中将所有依赖项打包到一个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>

1 个答案:

答案 0 :(得分:1)

可能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>