简体   繁体   中英

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

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.

My maven descriptor looks like the following

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

You can use filters inside the maven-shade-plugin configuration to exclude everything that is under META-INF/maven for every artifact:

<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-plugin can be found here .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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