Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven dependency plugin - exclude directories when unpackaging jar file

Tags:

java

maven

jar

I am trying to un-package a jar file using the maven dependency plugin. But I only want one file inside the jar file and want to exclude the META-INF directory that's inside the jar. How would I do this?

This is what I have so far:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>...</groupId>
                        <artifactId>...</artifactId>
                        <version>...</version>
                        <type>jar</type>
                        <outputDirectory>${project.basedir}/src/test/</outputDirectory>
                        <excludes>META-INF</excludes>
                    </artifactItem>
                </artifactItems>
                <excludes>META-INF</excludes>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 720
Kingamere Avatar asked Oct 30 '17 20:10

Kingamere


People also ask

Can we exclude a class from maven dependency?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.

How do I exclude a jar from pom?

We can have multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependencies we want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml.

What is maven unpack?

org.apache.maven.plugins:maven-dependency-plugin:3.3.0:unpack. Description: Goal that retrieves a list of artifacts from the repository and unpacks them in a defined location.

Do JARs contain dependencies?

Most JARs, no matter whether they contain an executable program or a library, require other libraries to run. Those dependencies are usually contained in JARs as well. So how can you make sure that your JAR has all required JARs in its CLASSPATH?


1 Answers

Found the solution.

<artifactItem>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <type>jar</type>
    <outputDirectory>${project.basedir}/src/test/</outputDirectory>
    <excludes>META-INF/</excludes>
</artifactItem>

Just add a forward slash: / after the directory name.

like image 92
Kingamere Avatar answered Oct 12 '22 16:10

Kingamere