Coder_sLaY Coder_sLaY - 1 year ago 122
Java Question

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.

Answer Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download