Here's a case I ran into recently.
com.googlecode.objectify:objectify:6.0.3 declares an optional
dependency on jackson-core:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>


Objectify also has a non-optional dependency on
google-cloud-datastore. google-cloud-datastore declares a non-optional
dependency on jackson-core:

  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
    </dependency>

Maven handles this and adds com.fasterxml.jackson.core:jackson-core to
the classpath as it should.

However the following Maven Resolver based code failed to add
com.fasterxml.jackson.core:jackson-core to the classpath:

    DependencyFilter filter =
        new 
AndDependencyFilter(DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME),
            new NonOptionalDependencyFilter());

    // todo we'd prefer not to depend on m2e here

    String coords = groupId + ":" + artifactId + ":" + version;
    Artifact artifact = new DefaultArtifact(coords);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, JavaScopes.RUNTIME));
    collectRequest.setRepositories(centralRepository(system));
    DependencyRequest request = new DependencyRequest(collectRequest, filter);

    // ensure checksum errors result in failure
    DefaultRepositorySystemSession session =
        new DefaultRepositorySystemSession(context.getRepositorySession());
    session.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_FAIL);

    try {
      List<ArtifactResult> artifacts =
system.resolveDependencies(session, request).getArtifactResults();

That is, this code excludes jackson-core, apparently because it sees
the optional artifact higher in the tree rather than the non-optional
one further down. How should this code be written so that it does what
Maven it self does? That is, recognize that even though the first
artifact is optional, there's also a non-optional copy elsewhere in
the dependency graph that needs to be considered?

Relevant:

https://github.com/GoogleCloudPlatform/google-cloud-eclipse/blob/773652f45654633371924c36d21f4756d97126d4/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/DependencyResolver.java

https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/3428


-- 
Elliotte Rusty Harold
elh...@ibiblio.org

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@maven.apache.org
For additional commands, e-mail: dev-h...@maven.apache.org

Reply via email to