【问题标题】:Maven: Exclude "META-INF/maven" folder from JARMaven:从 JAR 中排除“META-INF/maven”文件夹
【发布时间】:2017-10-26 17:03:17
【问题描述】:

我使用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 添加的。

This 回答类型的作品,但使用不同的工件,因此当我将其粘贴到我的 pom.xml 时会生成第二个 JAR。我想使用我当前的构建代码并排除上述maven 文件夹。如何使用maven构建规则来完成?

【问题讨论】:

    标签: java maven jar


    【解决方案1】:

    maven-jar-plugin 使用maven-archiver 处理包装。它提供了配置addMavenDescriptor,默认为true。将其设置为 false 应该会删除 META-INF/maven 目录。

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

    您可以找到参考here

    【讨论】:

    • 对我不起作用。一切都建立在干净的基础上,但它仍然存在。 &lt;exclude&gt;META-INF/maven/**&lt;/exclude&gt; 为我工作。
    【解决方案2】:

    您可以使用 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>
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2016-12-14
      • 2021-08-10
      • 2010-11-20
      • 2016-09-12
      • 2021-11-24
      • 2013-07-06
      • 2012-05-06
      • 2020-11-03
      相关资源
      最近更新 更多