Skip to content

Commit d51a7ab

Browse files
Preserve dependency scope in plugin artifacts
What changed: - Added `RepositoryUtils.toArtifact(ArtifactResult)` to convert resolved artifacts and copy dependency metadata from the resolution request. - Set Maven `Artifact` scope and optional flag from the resolved dependency node instead of converting only the raw Aether artifact. - Updated `DefaultMavenPluginManager` to use the new conversion path for plugin realm and extension artifact resolution. Why: - `PluginDescriptor.getArtifacts()` returned artifacts with `null` scope in Maven 3.10.0-rc-1. - Restoring scope/optional metadata brings behavior back in line with 3.9.x and avoids regressions in plugins that depend on artifact scope. fix #12497
1 parent e002e45 commit d51a7ab

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

maven-core/src/main/java/org/apache/maven/RepositoryUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import org.eclipse.aether.repository.RepositoryPolicy;
5151
import org.eclipse.aether.repository.WorkspaceReader;
5252
import org.eclipse.aether.repository.WorkspaceRepository;
53+
import org.eclipse.aether.resolution.ArtifactRequest;
54+
import org.eclipse.aether.resolution.ArtifactResult;
5355
import org.eclipse.aether.util.repository.AuthenticationBuilder;
5456

5557
/**
@@ -156,6 +158,27 @@ public static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
156158
return result;
157159
}
158160

161+
/**
162+
* @since 3.10.0
163+
*/
164+
public static org.apache.maven.artifact.Artifact toArtifact(ArtifactResult artifactResult) {
165+
166+
if (artifactResult == null || artifactResult.getArtifact() == null) {
167+
return null;
168+
}
169+
170+
org.apache.maven.artifact.Artifact artifact = toArtifact(artifactResult.getArtifact());
171+
Optional.ofNullable(artifactResult.getRequest())
172+
.map(ArtifactRequest::getDependencyNode)
173+
.map(DependencyNode::getDependency)
174+
.ifPresent(d -> {
175+
artifact.setScope(d.getScope());
176+
artifact.setOptional(d.isOptional());
177+
} );
178+
179+
return artifact;
180+
}
181+
159182
public static Dependency toDependency(
160183
org.apache.maven.artifact.Artifact artifact, Collection<org.apache.maven.model.Exclusion> exclusions) {
161184
if (artifact == null) {

maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private void createPluginRealm(
396396

397397
pluginArtifacts = result.getArtifactResults().stream()
398398
.filter(ArtifactResult::isResolved)
399-
.map(r -> RepositoryUtils.toArtifact(r.getArtifact()))
399+
.map(RepositoryUtils::toArtifact)
400400
.collect(Collectors.toList());
401401

402402
pluginRealm = classRealmManager.createPluginRealm(
@@ -831,7 +831,7 @@ private List<Artifact> resolveExtensionArtifacts(
831831
pluginDependenciesResolver.resolvePluginAndFlatten(extensionPlugin, null, null, repositories, session);
832832
return result.getArtifactResults().stream()
833833
.filter(ArtifactResult::isResolved)
834-
.map(r -> RepositoryUtils.toArtifact(r.getArtifact()))
834+
.map(RepositoryUtils::toArtifact)
835835
.collect(Collectors.toList());
836836
}
837837
}

0 commit comments

Comments
 (0)