0

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.

  • 1
    How do you create a fat jar? Which plugin do you use? Can you provide a relevat snippet from your pom.xml? – Mark Bramnik Aug 09 '21 at 10:43
  • Maybe [this](https://stackoverflow.com/questions/28458058/maven-shade-plugin-exclude-a-dependency-and-all-its-transitive-dependencies) answers it? – g00se Aug 09 '21 at 10:45
  • @MarkBramnik I added few more details. – Mukesh_Mike Aug 09 '21 at 11:27
  • @g00se I added few more details – Mukesh_Mike Aug 09 '21 at 11:28
  • The scope: provided is the correct one. System scope dependency are deprecated apart from making things more complicated than needed. And best practice is to put such jar files into a repository manager in particular if you are building from Jenkins...Furthermore the question is why you use maven-assembly-plugin as well as maven-shade-plugin ? – khmarbaise Aug 09 '21 at 12:55

1 Answers1

1

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.

Allen D. Ball
  • 1,916
  • 1
  • 9
  • 16