Mukesh_Mike
Mukesh_Mike

Reputation: 33

Add an external jar as a dependency to maven POM for compiling, but exclude it while creating a fat jar

While running mvn install i need to include an external jar(just for compiling). HOwever i am also creating a fat/uber jar, due to this, the external dependency gets added to the fat jar, I dont want to add the external jar in fat jar. Please suggest a way out ?

I am adding external dependency as:

<dependency>
            <groupId>a.b.c</groupId>
            <artifactId>xyz</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/library.jar</systemPath>
</dependency>

and creating fat as:

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
</plugins>

But the external jar(library.jar) is not required in the fat jar.

How can I exclude it from fat jar, but still keep it while doing mvn install ? Please note that adding the jar in the local repo is not an option for me, since i am doing this build from jenkins.

Upvotes: 1

Views: 517

Answers (1)

Allen D. Ball
Allen D. Ball

Reputation: 2026

It looks like the generated artifact of maven-shade-plugin will overwrite the output of maven-assembly-plugin so, unless there is something missing from the snippet, you should be able to remove the maven-assembly-plugin.

To filter the specific JAR from your shaded JAR, add:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        ...
        <configuration>
          <artifactSet>
            <excludes>
              <exclude>a.b.c:xyz</exclude>
            </excludes>
          </artifactSet>
        </configuration>
        ...
      </plugin>

to your maven-shade-plugin configuration.

Upvotes: 1

Related Questions