Maven:从JAR中排除“META-INF/maven”文件夹

6

我使用Maven构建JAR。当我检查JAR时,我发现在META-INF文件夹中有一个maven文件夹。我希望它被排除在构建之外。我的pom.xml中的当前构建代码如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>Libraries</classpathPrefix>
                        <mainClass>com.company.Main</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Built-By>Me</Built-By>
                    </manifestEntries>
                </archive>
                <!-- <excludes>
                    <exclude>META-INF/maven/**</exclude>
                </excludes> -->
            </configuration>
        </plugin>
        <!-- ...more plugins... -->
    </plugins>
</build>

我看到使用exclude标签可以排除一些内容,但是似乎并不起作用。这可能只适用于本地文件/文件夹?maven文件夹不是源代码的一部分,它只是由Maven添加进来的。 这个答案有点用,但它使用了不同的artifact,因此当我把它粘贴到我的pom.xml中时会生成第二个JAR。我想要使用我的当前构建代码,并像上面描述的那样排除maven文件夹。如何使用maven构建规则来实现呢?
2个回答

9

maven-jar-plugin 使用 maven-archiver 进行打包处理。它提供了配置项 addMavenDescriptor,默认值为 true。将其设置为 false 将会移除 META-INF/maven 目录。

...
<archive>
   <addMavenDescriptor>false</addMavenDescriptor>
   ....
</archive>

您可以在此处查找相关参考资料。


对我不起作用。所有内容都是从干净的状态构建的,但问题仍然存在。 <exclude>META-INF/maven/**</exclude> 对我有用。 - Wortig

1

您可以使用Maven Shade插件来创建JAR文件,并在pom.xml文件中使用以下配置来排除Maven文件夹:

<profile>
    <id>shade</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <excludes>
                                    <exclude>META-INF/**</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </executions>
                ...
</profile>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接