Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.maven.repository.internal.artifact.FatArtifactTraverser;
import org.apache.maven.repository.internal.scopes.Maven4ScopeManagerConfiguration;
import org.apache.maven.repository.internal.type.DefaultTypeProvider;
import org.apache.maven.repository.internal.type.TypeCollector;
import org.apache.maven.repository.internal.type.TypeDeriver;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
Expand Down Expand Up @@ -112,6 +113,7 @@ protected DependencySelector getDependencySelector() {

protected DependencyGraphTransformer getDependencyGraphTransformer() {
return new ChainedDependencyGraphTransformer(
new TypeCollector(),
new ConflictResolver(
new ConfigurableVersionSelector(), new ManagedScopeSelector(getScopeManager()),
new SimpleOptionalitySelector(), new ManagedScopeDeriver(getScopeManager())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public final class MavenArtifactProperties {
*/
public static final String CONSTITUTES_BUILD_PATH = "constitutesBuildPath";

/**
* When an artifact is both a regular dependency and a transitive dependency
* of a processor, this property records the derived processor type ID.
*
* @since 4.0.0
*/
public static final String PROCESSOR_TYPE = "maven.processor.type";

/**
* The (expected) path to the artifact on the local filesystem. An artifact which has this property set is assumed
* to be not present in any regular repository and likewise has no artifact descriptor. Artifact resolution will
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.repository.internal.type;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.maven.api.Type;
import org.eclipse.aether.RepositoryException;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.collection.DependencyGraphTransformationContext;
import org.eclipse.aether.collection.DependencyGraphTransformer;
import org.eclipse.aether.graph.DependencyNode;

/**
* Collects processor type information from the dependency graph BEFORE conflict resolution.
*
* @since 4.0.0
* @deprecated since 4.0.0, use {@code maven-api-impl} jar instead
* @see TypeDeriver
*/
@Deprecated(since = "4.0.0")
public class TypeCollector implements DependencyGraphTransformer {

public static final Object CONTEXT_KEY = TypeCollector.class.getName() + ".processorTypes";

static final Set<String> PROCESSOR_TYPE_IDS =
Set.of(Type.PROCESSOR, Type.CLASSPATH_PROCESSOR, Type.MODULAR_PROCESSOR);

private static final Map<String, String> DERIVE_MAP = Map.of(
Type.JAR, Type.PROCESSOR,
Type.CLASSPATH_JAR, Type.CLASSPATH_PROCESSOR,
Type.MODULAR_JAR, Type.MODULAR_PROCESSOR);

@Override
public DependencyNode transformGraph(DependencyNode root, DependencyGraphTransformationContext context)
throws RepositoryException {
Map<String, String> processorTypes = null;
for (DependencyNode child : root.getChildren()) {
if (child.getArtifact() == null) {
continue;
}
String childType = child.getArtifact().getProperty(ArtifactProperties.TYPE, "");
if (!PROCESSOR_TYPE_IDS.contains(childType)) {
continue;
}
for (DependencyNode transitive : child.getChildren()) {
if (transitive.getArtifact() == null) {
continue;
}
String transitiveType = transitive.getArtifact().getProperty(ArtifactProperties.TYPE, "");
String derived = DERIVE_MAP.get(transitiveType);
if (derived != null) {
if (processorTypes == null) {
processorTypes = new HashMap<>();
}
processorTypes.put(conflictKey(transitive), derived);
}
}
}
if (processorTypes != null) {
context.put(CONTEXT_KEY, processorTypes);
}
return root;
}

/**
* Builds a unique key for an artifact based on the same identity components
* used by conflict resolution: groupId, artifactId, extension, and classifier.
*/
static String conflictKey(DependencyNode node) {
var a = node.getArtifact();
return a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getExtension() + ':' + a.getClassifier();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Set;

import org.apache.maven.api.Type;
import org.apache.maven.repository.internal.artifact.MavenArtifactProperties;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.artifact.ArtifactType;
Expand Down Expand Up @@ -61,6 +62,11 @@ public DependencyNode transformGraph(DependencyNode root, DependencyGraphTransfo
logger.debug("TYPES: Before transform:\n {}", sb);
}
root.accept(new TypeDeriverVisitor(context.getSession().getArtifactTypeRegistry()));
@SuppressWarnings("unchecked")
Map<String, String> collectedProcessorTypes = (Map<String, String>) context.get(TypeCollector.CONTEXT_KEY);
if (collectedProcessorTypes != null) {
root.accept(new ProcessorTypeMerger(collectedProcessorTypes));
}
if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
root.accept(new DependencyGraphDumper(
Expand Down Expand Up @@ -144,4 +150,35 @@ private ArtifactType derive(ArtifactType parentType, ArtifactType currentType) {
return result;
}
}

private static class ProcessorTypeMerger implements DependencyVisitor {
private final Map<String, String> collectedProcessorTypes;

ProcessorTypeMerger(Map<String, String> collectedProcessorTypes) {
this.collectedProcessorTypes = collectedProcessorTypes;
}

@Override
public boolean visitEnter(DependencyNode node) {
if (node.getArtifact() != null) {
String currentType = node.getArtifact().getProperty(ArtifactProperties.TYPE, "");
if (!TypeCollector.PROCESSOR_TYPE_IDS.contains(currentType)) {
String key = TypeCollector.conflictKey(node);
String processorType = collectedProcessorTypes.get(key);
if (processorType != null) {
Artifact artifact = node.getArtifact();
Map<String, String> props = new HashMap<>(artifact.getProperties());
props.put(MavenArtifactProperties.PROCESSOR_TYPE, processorType);
node.setArtifact(artifact.setProperties(props));
}
}
}
return true;
}

@Override
public boolean visitLeave(DependencyNode node) {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import org.apache.maven.api.JavaPathType;
import org.apache.maven.api.Node;
import org.apache.maven.api.PathType;
import org.apache.maven.api.Type;
import org.apache.maven.api.services.DependencyResolverException;
import org.apache.maven.api.services.DependencyResolverRequest;
import org.apache.maven.api.services.DependencyResolverResult;
import org.apache.maven.impl.resolver.artifact.MavenArtifactProperties;

/**
* The result of collecting dependencies with a dependency resolver.
Expand Down Expand Up @@ -320,6 +322,52 @@ void addDependency(Node node, Dependency dep, Predicate<PathType> filter, Path p
}
}
addPathElement(cache.selectPathType(pathTypes, filter, path).orElse(PathType.UNRESOLVED), path);
// If the artifact is also needed on a processor path (because it's a transitive dep
// of a processor AND a direct dep with a different type), add it to the processor path too.
addProcessorPathIfNeeded(node, filter, path);
}

/**
* Checks if the artifact has a {@link MavenArtifactProperties#PROCESSOR_TYPE} property
* and, if so, also adds it to the corresponding processor path. This handles the case
* where an artifact is both a regular dependency (e.g., modular-jar on --module-path)
* and a transitive dependency of a processor (needs --processor-module-path).
*/
private void addProcessorPathIfNeeded(Node node, Predicate<PathType> filter, Path path) throws IOException {
if (!(node instanceof AbstractNode abstractNode)) {
return;
}
org.eclipse.aether.artifact.Artifact aetherArtifact =
abstractNode.getDependencyNode().getArtifact();
if (aetherArtifact == null) {
return;
}
String processorType = aetherArtifact.getProperty(MavenArtifactProperties.PROCESSOR_TYPE, null);
if (processorType == null) {
return;
}
Set<PathType> processorPathTypes = processorPathTypesFor(processorType);
if (processorPathTypes != null) {
cache.selectPathType(processorPathTypes, filter, path).ifPresent(pt -> addPathElement(pt, path));
}
}

// Path type sets for processor types — must stay in sync with DefaultTypeProvider
private static final Set<PathType> PROCESSOR_PATH_TYPES =
Set.of(JavaPathType.PROCESSOR_CLASSES, JavaPathType.PROCESSOR_MODULES);
private static final Set<PathType> CLASSPATH_PROCESSOR_PATH_TYPES = Set.of(JavaPathType.PROCESSOR_CLASSES);
private static final Set<PathType> MODULAR_PROCESSOR_PATH_TYPES = Set.of(JavaPathType.PROCESSOR_MODULES);

/**
* Maps a processor type ID to its corresponding path types.
*/
private static Set<PathType> processorPathTypesFor(String processorType) {
return switch (processorType) {
case Type.PROCESSOR -> PROCESSOR_PATH_TYPES;
case Type.CLASSPATH_PROCESSOR -> CLASSPATH_PROCESSOR_PATH_TYPES;
case Type.MODULAR_PROCESSOR -> MODULAR_PROCESSOR_PATH_TYPES;
default -> null;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.maven.impl.resolver.scopes.Maven3ScopeManagerConfiguration;
import org.apache.maven.impl.resolver.scopes.Maven4ScopeManagerConfiguration;
import org.apache.maven.impl.resolver.type.DefaultTypeProvider;
import org.apache.maven.impl.resolver.type.TypeCollector;
import org.apache.maven.impl.resolver.type.TypeDeriver;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
Expand Down Expand Up @@ -108,6 +109,7 @@ protected DependencySelector getDependencySelector() {

protected DependencyGraphTransformer getDependencyGraphTransformer() {
return new ChainedDependencyGraphTransformer(
new TypeCollector(),
new ConflictResolver(
new ConfigurableVersionSelector(), new ManagedScopeSelector(getScopeManager()),
new SimpleOptionalitySelector(), new ManagedScopeDeriver(getScopeManager())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public final class MavenArtifactProperties {
*/
public static final String CONSTITUTES_BUILD_PATH = "constitutesBuildPath";

/**
* When an artifact is both a regular dependency (e.g., modular-jar) and a transitive dependency
* of a processor, this property records the derived processor type ID (e.g., "modular-processor").
* This allows the artifact to be placed on both the module-path and the processor-module-path.
*
* @since 4.0.0
* @see org.apache.maven.impl.resolver.type.TypeCollector
*/
public static final String PROCESSOR_TYPE = "maven.processor.type";

/**
* The (expected) path to the artifact on the local filesystem. An artifact which has this property set is assumed
* to be not present in any regular repository and likewise has no artifact descriptor. Artifact resolution will
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.impl.resolver.type;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.maven.api.Type;
import org.eclipse.aether.RepositoryException;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.collection.DependencyGraphTransformationContext;
import org.eclipse.aether.collection.DependencyGraphTransformer;
import org.eclipse.aether.graph.DependencyNode;

/**
* Collects processor type information from the dependency graph BEFORE conflict resolution.
* <p>
* For each direct dependency that is a processor type, this transformer records which of
* its children (transitive deps) would need processor path types. This information is stored
* in the transformation context so that {@link TypeDeriver} (which runs after conflict resolution)
* can apply processor path types even to nodes whose transitive processor occurrence
* was eliminated by conflict resolution.
* <p>
* Without this collector, the following scenario fails:
* <pre>
* root
* ├── shared-lib:1.0 (type=modular-jar) → --module-path
* └── my-processor:1.0 (type=modular-processor)
* └── shared-lib:1.0 (type=jar) → should go to --processor-module-path
* </pre>
* ConflictResolver removes the transitive shared-lib (same GA, loser), so TypeDeriver
* never sees it under the processor. This collector preserves that information.
*
* @since 4.0.0
* @see TypeDeriver
*/
public class TypeCollector implements DependencyGraphTransformer {

/**
* Context key under which the collected processor type map is stored.
* The value is a {@code Map<String, String>} mapping artifact conflict keys
* (groupId:artifactId:extension:classifier) to derived processor type IDs.
*/
public static final Object CONTEXT_KEY = TypeCollector.class.getName() + ".processorTypes";

static final Set<String> PROCESSOR_TYPE_IDS =
Set.of(Type.PROCESSOR, Type.CLASSPATH_PROCESSOR, Type.MODULAR_PROCESSOR);

private static final Map<String, String> DERIVE_MAP = Map.of(
Type.JAR, Type.PROCESSOR,
Type.CLASSPATH_JAR, Type.CLASSPATH_PROCESSOR,
Type.MODULAR_JAR, Type.MODULAR_PROCESSOR);

@Override
public DependencyNode transformGraph(DependencyNode root, DependencyGraphTransformationContext context)
throws RepositoryException {
Map<String, String> processorTypes = null;
for (DependencyNode child : root.getChildren()) {
if (child.getArtifact() == null) {
continue;
}
String childType = child.getArtifact().getProperty(ArtifactProperties.TYPE, "");
if (!PROCESSOR_TYPE_IDS.contains(childType)) {
continue;
}
// This direct dep is a processor — record its children's derived types
for (DependencyNode transitive : child.getChildren()) {
if (transitive.getArtifact() == null) {
continue;
}
String transitiveType = transitive.getArtifact().getProperty(ArtifactProperties.TYPE, "");
String derived = DERIVE_MAP.get(transitiveType);
if (derived != null) {
if (processorTypes == null) {
processorTypes = new HashMap<>();
}
processorTypes.put(conflictKey(transitive), derived);
}
}
}
if (processorTypes != null) {
context.put(CONTEXT_KEY, processorTypes);
}
return root;
}

/**
* Builds a unique key for an artifact based on the same identity components
* used by conflict resolution: groupId, artifactId, extension, and classifier.
*/
static String conflictKey(DependencyNode node) {
var a = node.getArtifact();
return a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getExtension() + ':' + a.getClassifier();
}
}
Loading