Maven: How to have jar-with-dependencies exclude "provided" dependencies?

SkyWalker

I have a Maven Scala project that will be deployed on some container and therefore mark several of the dependencies with scope provided meaning those dependencies will be used for compiling but not taken into account for transitive resolution as they are "provided at runtime". However, when I run the following command, it produces the intended jar with dependencies but also including those dependencies that were marked as provided.

mvn clean install assembly:assembly -DdescriptorId=jar-with-dependencies -DskipTests

I tried existing answers to this problem e.g. Excluding “provided” dependencies from Maven assembly but for some reason produces an incorrect choice of dependencies and even missing the main code. In this OP I'd like to find a cleaner, more up to date solution to this problem ... is there one?

Jon Sampson

You may be better off with a different maven plugin. See Difference between maven plugins ( assembly-plugins , jar-plugins , shaded-plugins. Shade would probably suit you best in this case. What you are looking to create is referred to an uber-jar.

Regarding Shade, from the Maven website:

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.

The goals for the Shade Plugin are bound to the package phase in the build lifecycle.

Configuring Your Shade Plugin:

<project>
...
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.0.0</version>
      <configuration>
        <!-- put your configurations here -->
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
...
</project>

Note that the default implementation replaces your project's artifact with the shade version. Need both? Look here: Attaching the Shaded Artifact

Merging several jars at once is not necessarily utter simplicity and so Shade has the concept of Resource Transformers (link also has more samples).

Aggregating classes/resources from several artifacts into one uber JAR is straight forward as long as there is no overlap. Otherwise, some kind of logic to merge resources from several JARs is required. This is where resource transformers kick in.

The project site is actually quite good. There are lots of varied examples.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Maven - How to exclude dependencies from generated JAR

From Java

How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

From Dev

How to exclude transitive dependencies with scope provided with maven-assembly-plugin?

From Dev

How to create maven uber jar which includes dependencies with scope provided

From Dev

How to "exclude" dependencies embedded in an uber-jar using Maven 3?

From Dev

Maven provided dependencies

From Dev

How to copy provided Maven dependencies into a chosen directory?

From Dev

Maven assembly plugin: how to include provided dependencies of transitive dependencies

From Java

How can I run code locally with dependencies and exclude them when creating a Jar with Maven

From Java

Maven jar with dependencies?

From Java

Including dependencies in a jar with Maven

From Dev

Building jar with dependencies maven

From Dev

maven project and glassfish provided dependencies

From Java

How To: Eclipse Maven install build jar with dependencies

From Java

How to include maven dependencies in a jar file?

From Java

Should jars have "provided" dependencies?

From Java

Maven: How are artifacts built if they have their own dependencies?

From Java

how to exclude gradle dependencies

From Dev

How do I filter resources provided by dependencies in Maven?

From

In an Android Gradle build, how to exclude dependencies from an included jar file?

From Dev

How to exclude logs generated by maven dependencies in slf4j?

From Java

Maven failing to download jar dependencies

From Dev

Maven shade plugin - Jar and dependencies

From Dev

Maven not adding all dependencies to jar

From Java

Maven deploy jar with dependencies to repo

From Java

Maven: Add local dependencies to jar

From Dev

The maven dependencies are not added to the jar in eclipse

From Dev

Maven jar without dependencies by default

From Dev

jar download with dependencies from maven

Related Related

  1. 1

    Maven - How to exclude dependencies from generated JAR

  2. 2

    How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

  3. 3

    How to exclude transitive dependencies with scope provided with maven-assembly-plugin?

  4. 4

    How to create maven uber jar which includes dependencies with scope provided

  5. 5

    How to "exclude" dependencies embedded in an uber-jar using Maven 3?

  6. 6

    Maven provided dependencies

  7. 7

    How to copy provided Maven dependencies into a chosen directory?

  8. 8

    Maven assembly plugin: how to include provided dependencies of transitive dependencies

  9. 9

    How can I run code locally with dependencies and exclude them when creating a Jar with Maven

  10. 10

    Maven jar with dependencies?

  11. 11

    Including dependencies in a jar with Maven

  12. 12

    Building jar with dependencies maven

  13. 13

    maven project and glassfish provided dependencies

  14. 14

    How To: Eclipse Maven install build jar with dependencies

  15. 15

    How to include maven dependencies in a jar file?

  16. 16

    Should jars have "provided" dependencies?

  17. 17

    Maven: How are artifacts built if they have their own dependencies?

  18. 18

    how to exclude gradle dependencies

  19. 19

    How do I filter resources provided by dependencies in Maven?

  20. 20

    In an Android Gradle build, how to exclude dependencies from an included jar file?

  21. 21

    How to exclude logs generated by maven dependencies in slf4j?

  22. 22

    Maven failing to download jar dependencies

  23. 23

    Maven shade plugin - Jar and dependencies

  24. 24

    Maven not adding all dependencies to jar

  25. 25

    Maven deploy jar with dependencies to repo

  26. 26

    Maven: Add local dependencies to jar

  27. 27

    The maven dependencies are not added to the jar in eclipse

  28. 28

    Maven jar without dependencies by default

  29. 29

    jar download with dependencies from maven

HotTag

Archive