Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a direct dependency of a Maven Plugin

Tags:

java

maven

jaxb

I want to exclude a direct dependency of a Maven plugin and the approach described in this answer does not work (as indicated by this comment).

As a particular example:

<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.2</version>
            <!-- more config -->
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.13.2</version>
                    <exclusions>
                        <exclusion>
                            <groupId>javax.xml.bind</groupId>
                            <artifactId>jaxb-api</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

I still see javax.xml.bind:jaxb-api in the list of dependencies (with mvn ... -X). What am I doing wrong?

(In case someone has an idea for how to replace the dependency on that artifact with the JDK 9 equivalent for that API [as seems to happen on Java 8, where "JAXB API os loaded from the [jar:...jre/lib/rt.jar]"], I'm happy to open a new issue for that.)

Update

Running out of ideas and this being an experiment anyways, I excluded the dependency by editing the plugin's pom.xml in my local repository. Now mvn ... -X shows that there is also an indirect dependency (in this case by org.jvnet.jaxb2.maven2:maven-jaxb22-plugin) that I can successfully exclude with the mechanism above. Just using both excludes, from maven-jaxb2-plugin and maven-jaxb22-plugin, does not do the trick. This indicates that exclusion works in general but apparently not on a plugin's direct dependency.

(By the way, this indeed lead to "Java JAXB API is loaded from the [jrt:/java.xml.bind]", which was my goal.)

like image 591
Nicolai Parlog Avatar asked Apr 26 '17 09:04

Nicolai Parlog


People also ask

Can we exclude dependency from plugin?

We can exclude the dependencies that are not declared but used or the dependencies that are used but not declared using maven plugins.

How do you exclude a transitive dependency in Maven?

There we can exclude all transitive dependencies without specifying groupId and artifactId of the dependencies. So need to use astric(*) character as groupid and artifactid of the dependency. This wildcard transitive dependencies ignoring is available with maven 3.2.

Is there a way to exclude Maven dependency globally?

Conclusion – By using the <scope>provided</scope> for a dependency you can globally exclude the dependency from getting packaged in the application lib instead it's loaded by the server classloader thus avoiding any LinkageError or ClassCastExceptions from arising due to jar conflicts.


2 Answers

Up until now there hasn't been any reason to do this, but this seems like a valid one. Most clean solution I can think of is allowing to override the scope with "none" for plugin dependencies.

I've created MNG-6222 for it, not sure if we'll fix this for a Maven3, but it makes sense to do it at least for the next major.

like image 108
Robert Scholte Avatar answered Sep 20 '22 12:09

Robert Scholte


I had a similar situation with maven-linkcheck-plugin in the end I did a more brute force approach to remove the doxia-linkcheck dependency and make it use my fork by forking maven-linkcheck-plugin and creating my own with the proper dependencies.

like image 42
Archimedes Trajano Avatar answered Sep 21 '22 12:09

Archimedes Trajano