简体   繁体   中英

Optional Dependency for Maven when artifact does no exist

I do not know if it is possible for maven to act in this way :

  1. An artifact does not exist or is not accessible

  2. Mark this artifact as optional

...I am trying to generate a dependency does not exist and mark it as optional and make a buil with Maven in Eclipse ... but it seems that it does not like ..:

    <dependency> 
        <groupId>com.x.y</groupId>
        <artifactId>myOptionalArtifact</artifactId>     
        <optional>true</optional>   
    </dependency>

..... but Maven crashes.... :

BUILD FAILURE Could not find artifact com.xy:myOptionalArtifact:jar:1.0

I'm starting to guess that the optional tag can NOT be used in this sense ???

If you really want to remove a dependency from the dependency tree, you need to figure out all transitive place where it is used and add exclusions to the respective dependencies.

Optional or provided won't help. The artifact remains on the classpath and therefore, Maven tries to load it.

Maven treats all dependencies you list in the <project><dependencies> section of your POM as required for your project to build; therefore, any failure to find one of those dependencies fails the build, and this is expected behavior.

The <optional> tag you reference is used for when your project is a dependency of another project, indicating to Maven that the optional dependency should be automatically excluded when resolving the transitive dependencies in your main project.

From https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html :

Optional dependencies - If project Y depends on project Z, the owner of project can mark project Z as an optional dependency, using the "optional" element. When project X depends on project Y, X will depend only on Y and not on Y's optional dependency Z. The owner of project X may then explicitly add a dependency on Z, at her option. (It may be helpful to think of optional dependencies as "excluded by default.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM