首页 > 解决方案 > 使用 maven 程序集插件从 jar-with-dependencies 中删除文件

问题描述

我使用 maven-assembly-plugin 3.1.1 版来制作一个包含所有依赖项的 jar。我想排除我的 application.yml 但是我无法从 jar 中删除它。

pom.xml:

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

我的描述符.xml:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
    <id>test</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>compile</scope>
            <excludes>
                <exclude>
                    application.yml
                </exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.build.outputDirectory}</directory>
            <excludes>
                <exclude>
                    application.yml
                </exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

结果 :

The following patterns were never triggered in this artifact exclusion filter:
o  'application.yml'

如果我使用 * 模式而不是 application.yml,则所有文件都将被很好地删除。

我试图放置绝对路径,但它没有任何改变:

        <excludes>
            <exclude>
                env/dev/conf/application.yml
            </exclude>
        </excludes>

我的 fileSets 接缝中的排除也无用。

还在 fileSets 和 dependencySet 中尝试了这种模式,但 application.yml 仍然包括:

         <exclude>
            **/application.yml
        </exclude>

标签: mavenjarmaven-assembly-plugin

解决方案


有效的解决方案,我忘了在程序集文件中添加 unpackOptions :

pom.xml:

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

描述符.xml:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.basedir}</directory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <unpackOptions>
                <excludes>
                    <exclude>**/application.yml</exclude>
                </excludes>
            </unpackOptions>
        </dependencySet>
    </dependencySets>
</assembly>

推荐阅读