java 从生成的 jar 文件中排除 META-INF/maven 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32887455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:56:05  来源:igfitidea点击:

Exclude META-INF/maven folder from the generated jar file

javamavenmaven-shade-plugin

提问by Coder_sLaY

I am trying to create a jar file which has all the necessary classes extracted within the jar. But for few dependent jar like log4j, it creates some folders inside META-INF/maven/*. I have a limitation that the server in which I will be placing the generated jar file will not have Internet connectivity. So if there is any content in this META-INF/maven/*folder then it gives me an error.

我正在尝试创建一个 jar 文件,其中包含在 jar 中提取的所有必要类。但是对于像log4j这样的少数依赖 jar ,它会在META-INF/maven/*.我有一个限制,我将在其中放置生成的 jar 文件的服务器没有 Internet 连接。所以如果这个META-INF/maven/*文件夹中有任何内容,那么它会给我一个错误。

My maven descriptor looks like the following

我的 Maven 描述符如下所示

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <minimizeJar>true</minimizeJar>
                <finalName>myclient</finalName>
            </configuration>
        </plugin>
    </plugins>
</build>

I am able to extract the required class files in the generated jar but the maven folder is still getting generated under META-INF. I have to manually delete the folder to make everything work. Please advice on how to automate this removal of maven folder from the generated jar file.

我能够在生成的 jar 中提取所需的类文件,但 maven 文件夹仍在META-INF.我必须手动删除文件夹才能使一切正常。请就如何从生成的 jar 文件中自动删除 maven 文件夹提供建议。

回答by Tunaki

You can use filtersinside the maven-shade-pluginconfiguration to exclude everything that is under META-INF/mavenfor every artifact:

您可以在配置中使用过滤器maven-shade-plugin来排除META-INF/maven每个工件下面的所有内容:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/maven/**</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

A solution for the maven-jar-plugincan be found here.

maven-jar-plugin可以在此处找到解决方案。

回答by CuriousDev

Simple add this to either it is a jar,war,ear plugin

简单地将其添加到 jar、war、ear 插件中

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