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 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

ClassNotFoundException for maven provided dependencies

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 Dev

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

From Dev

How to Exclude Certain Jar Dependencies while creating jar without using maven?

From Dev

Building jar with dependencies maven

From Dev

How to include maven dependencies in a jar file?

From Dev

Should jars have "provided" dependencies?

From Dev

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

From Dev

How do I filter resources provided by dependencies in Maven?

From Dev

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

From Dev

How to package a jar and all dependencies within a new jar with maven

From Dev

How to exclude logs generated by maven dependencies in slf4j?

From Dev

How to exclude logs generated by maven dependencies in slf4j?

From Dev

Maven not adding all dependencies to jar

From Dev

Using Maven in Ant for jar dependencies

From Dev

executable jar with dependencies using maven

From Dev

Maven generate jar dependencies then war

From Dev

Maven jar without dependencies by default

From Dev

Why aren't 'provided' Maven dependencies 'transitive'?

From Dev

Maven - import transitively provided dependencies into project

From Dev

Exclude dependencies

From Java

How can I create an executable JAR with dependencies using Maven?

From Dev

Intellij Java 2016 & Maven : how to embed dependencies in JAR?

From Dev

Intellij Java 2016 & Maven : how to embed dependencies in JAR?

Related Related

  1. 1

    Maven - How to exclude dependencies from generated JAR

  2. 2

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

  3. 3

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

  4. 4

    ClassNotFoundException for maven provided dependencies

  5. 5

    Maven provided dependencies

  6. 6

    How to copy provided Maven dependencies into a chosen directory?

  7. 7

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

  8. 8

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

  9. 9

    How to Exclude Certain Jar Dependencies while creating jar without using maven?

  10. 10

    Building jar with dependencies maven

  11. 11

    How to include maven dependencies in a jar file?

  12. 12

    Should jars have "provided" dependencies?

  13. 13

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

  14. 14

    How do I filter resources provided by dependencies in Maven?

  15. 15

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

  16. 16

    How to package a jar and all dependencies within a new jar with maven

  17. 17

    How to exclude logs generated by maven dependencies in slf4j?

  18. 18

    How to exclude logs generated by maven dependencies in slf4j?

  19. 19

    Maven not adding all dependencies to jar

  20. 20

    Using Maven in Ant for jar dependencies

  21. 21

    executable jar with dependencies using maven

  22. 22

    Maven generate jar dependencies then war

  23. 23

    Maven jar without dependencies by default

  24. 24

    Why aren't 'provided' Maven dependencies 'transitive'?

  25. 25

    Maven - import transitively provided dependencies into project

  26. 26

    Exclude dependencies

  27. 27

    How can I create an executable JAR with dependencies using Maven?

  28. 28

    Intellij Java 2016 & Maven : how to embed dependencies in JAR?

  29. 29

    Intellij Java 2016 & Maven : how to embed dependencies in JAR?

HotTag

Archive