简体   繁体   English

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

[英]Maven: Exclude “META-INF/maven” folder from JAR

I use Maven to build a JAR .我使用Maven构建一个JAR When I check the JAR , I see a maven folder inside the META-INF folder.当我检查JAR ,我在META-INF文件夹中看到了一个maven文件夹。 I want it to be excluded from the build.我希望它从构建中排除。 My current build code in the pom.xml looks like this:我当前在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>

I read that using the exclude tags allows you to exclude something but it doesn't work.我读到使用exclude标签可以排除某些内容,但它不起作用。 Maybe this only refers to local files/folders?也许这仅指本地文件/文件夹? The maven folder is not part of the source, it's just added by Maven . maven文件夹不是源的一部分,它只是由Maven添加的。

This answer kind of works but uses a different artifact hence a 2nd JAR is generated when I paste it into my pom.xml .这个答案是有效的,但使用了不同的工件,因此当我将它粘贴到我的pom.xml时会生成第二个JAR I want to use my current build code and exclude the maven folder like described above.我想使用我当前的构建代码并排除如上所述的maven文件夹。 How can it be done using maven build rules?如何使用maven构建规则来完成?

The maven-jar-plugin uses the maven-archiver to handle packaging. maven-jar-plugin使用maven-archiver来处理打包。 It provides the configuration addMavenDescriptor , which is true by default.它提供了配置addMavenDescriptor ,默认为true Setting it to false should remove the META-INF/maven directory.将其设置为false应删除META-INF/maven目录。

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

You can find the reference here .您可以在此处找到参考。

You can use maven shade plugin to create the jar and exclude the maven folder using following configurations in you pom.xml file:您可以使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM