使用 Maven 程序集插件从 jar-with-dependencies 中删除文件

remove file from jar-with-dependencies with maven assembly plugin

我使用 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>

我的descriptor.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 seam 中的 exclude 也没用。

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

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

有效的解决方案,我忘记在程序集文件中添加 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>

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