6

I use Maven to build a JAR. When I check the JAR, I see a maven folder inside the META-INF folder. I want it to be excluded from the build. My current build code in the pom.xml looks like this:

<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. Maybe this only refers to local files/folders? The maven folder is not part of the source, it's just added by 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. I want to use my current build code and exclude the maven folder like described above. How can it be done using maven build rules?

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185

2 Answers2

9

The maven-jar-plugin uses the maven-archiver to handle packaging. It provides the configuration addMavenDescriptor, which is true by default. Setting it to false should remove the META-INF/maven directory.

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

You can find the reference here.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
f1l2
  • 491
  • 1
  • 6
  • 16
  • Does not work for me. All was built from clean, but it is still there. `META-INF/maven/**` works for me. – Wortig Apr 16 '22 at 08:43
1

You can use maven shade plugin to create the jar and exclude the maven folder using following configurations in you pom.xml file:

<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>
ron190
  • 1,032
  • 1
  • 17
  • 29
Manas
  • 888
  • 10
  • 20