[MNG-8026] Maven drives regarding scopes (#1392)

Maven should be fully in control regarding dependency scope definitions and their interpretations (and transformations). 
This PR makes following changes:
* no change in behaviour (all works as before), but
* maven codebase contains now all the code related to scopes
* no resolver code is in use that was deprecated (as it was wrong to have it in resolver), see issue [MRESOLVER-471](https://issues.apache.org/jira/browse/MRESOLVER-471)
* this also stops scope definitions duplication as well, they are now defined in one single project: Maven

---

https://issues.apache.org/jira/browse/MNG-8026
This commit is contained in:
Tamas Cservenak 2024-01-25 14:58:24 +01:00 committed by GitHub
parent f2595c83d9
commit 20f7cfc361
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 479 additions and 55 deletions

View File

@ -42,6 +42,9 @@ import org.apache.maven.execution.MavenSession;
import org.apache.maven.internal.impl.DefaultLookup; import org.apache.maven.internal.impl.DefaultLookup;
import org.apache.maven.internal.impl.DefaultSessionFactory; import org.apache.maven.internal.impl.DefaultSessionFactory;
import org.apache.maven.plugin.LegacySupport; import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.repository.internal.scopes.MavenDependencyContextRefiner;
import org.apache.maven.repository.internal.scopes.MavenScopeDeriver;
import org.apache.maven.repository.internal.scopes.MavenScopeSelector;
import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory; import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
import org.apache.maven.rtinfo.RuntimeInformation; import org.apache.maven.rtinfo.RuntimeInformation;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
@ -62,9 +65,6 @@ import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
import org.eclipse.aether.util.graph.selector.ScopeDependencySelector; import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer; import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
import org.eclipse.aether.util.graph.transformer.ConflictResolver; import org.eclipse.aether.util.graph.transformer.ConflictResolver;
import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser; import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
@ -314,9 +314,9 @@ public abstract class AbstractArtifactComponentTestCase // extends PlexusTestCas
session.setDependencySelector(depFilter); session.setDependencySelector(depFilter);
DependencyGraphTransformer transformer = new ConflictResolver( DependencyGraphTransformer transformer = new ConflictResolver(
new NearestVersionSelector(), new JavaScopeSelector(), new NearestVersionSelector(), new MavenScopeSelector(),
new SimpleOptionalitySelector(), new JavaScopeDeriver()); new SimpleOptionalitySelector(), new MavenScopeDeriver());
transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner()); transformer = new ChainedDependencyGraphTransformer(transformer, new MavenDependencyContextRefiner());
session.setDependencyGraphTransformer(transformer); session.setDependencyGraphTransformer(transformer);
LocalRepository localRepo = new LocalRepository(localRepository().getBasedir()); LocalRepository localRepo = new LocalRepository(localRepository().getBasedir());

View File

@ -36,7 +36,7 @@ import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.internal.xml.XmlNodeImpl; import org.apache.maven.internal.xml.XmlNodeImpl;
import org.apache.maven.internal.xml.XmlPlexusConfiguration; import org.apache.maven.internal.xml.XmlPlexusConfiguration;
import org.apache.maven.model.ModelBase; import org.apache.maven.model.ModelBase;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.apache.maven.repository.internal.MavenSessionBuilderSupplier;
import org.apache.maven.resolver.RepositorySystemSessionFactory; import org.apache.maven.resolver.RepositorySystemSessionFactory;
import org.apache.maven.rtinfo.RuntimeInformation; import org.apache.maven.rtinfo.RuntimeInformation;
import org.apache.maven.settings.Mirror; import org.apache.maven.settings.Mirror;
@ -220,8 +220,8 @@ class DefaultRepositorySystemSessionFactory implements RepositorySystemSessionFa
requestExtender.extend(request); requestExtender.extend(request);
} }
SessionBuilder sessionBuilder = MavenRepositorySystemUtils.newSession( SessionBuilder sessionBuilder = new MavenSessionBuilderSupplier(repoSystem).get();
repoSystem.createSessionBuilder(), new TypeRegistryAdapter(typeRegistry)); sessionBuilder.setArtifactTypeRegistry(new TypeRegistryAdapter(typeRegistry));
sessionBuilder.setCache(request.getRepositoryCache()); sessionBuilder.setCache(request.getRepositoryCache());
// this map is read ONLY to get config from (profiles + env + system + user) // this map is read ONLY to get config from (profiles + env + system + user)

View File

@ -32,6 +32,7 @@ import org.apache.maven.RepositoryUtils;
import org.apache.maven.model.Dependency; import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginResolutionException; import org.apache.maven.plugin.PluginResolutionException;
import org.apache.maven.repository.internal.scopes.MavenDependencyScopes;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession;
@ -52,7 +53,6 @@ import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.DependencyRequest; import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException; import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.resolution.DependencyResult; import org.eclipse.aether.resolution.DependencyResult;
import org.eclipse.aether.util.artifact.JavaScopes;
import org.eclipse.aether.util.filter.AndDependencyFilter; import org.eclipse.aether.util.filter.AndDependencyFilter;
import org.eclipse.aether.util.filter.ScopeDependencyFilter; import org.eclipse.aether.util.filter.ScopeDependencyFilter;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
@ -211,8 +211,8 @@ public class DefaultPluginDependenciesResolver implements PluginDependenciesReso
for (Dependency dependency : plugin.getDependencies()) { for (Dependency dependency : plugin.getDependencies()) {
org.eclipse.aether.graph.Dependency pluginDep = org.eclipse.aether.graph.Dependency pluginDep =
RepositoryUtils.toDependency(dependency, session.getArtifactTypeRegistry()); RepositoryUtils.toDependency(dependency, session.getArtifactTypeRegistry());
if (!JavaScopes.SYSTEM.equals(pluginDep.getScope())) { if (!MavenDependencyScopes.SYSTEM.equals(pluginDep.getScope())) {
pluginDep = pluginDep.setScope(JavaScopes.RUNTIME); pluginDep = pluginDep.setScope(MavenDependencyScopes.RUNTIME);
} }
request.addDependency(pluginDep); request.addDependency(pluginDep);
} }

View File

@ -23,10 +23,10 @@ import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.apache.maven.plugin.PluginValidationManager; import org.apache.maven.plugin.PluginValidationManager;
import org.apache.maven.repository.internal.scopes.MavenDependencyScopes;
import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.resolution.ArtifactDescriptorResult; import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.util.artifact.JavaScopes;
/** /**
* Detects Maven3 plugins using maven-compat Maven2 compatibility layer. * Detects Maven3 plugins using maven-compat Maven2 compatibility layer.
@ -50,7 +50,7 @@ class Maven3CompatDependenciesValidator extends AbstractMavenPluginDependenciesV
for (org.eclipse.aether.graph.Dependency dependency : artifactDescriptorResult.getDependencies()) { for (org.eclipse.aether.graph.Dependency dependency : artifactDescriptorResult.getDependencies()) {
if ("org.apache.maven".equals(dependency.getArtifact().getGroupId()) if ("org.apache.maven".equals(dependency.getArtifact().getGroupId())
&& "maven-compat".equals(dependency.getArtifact().getArtifactId()) && "maven-compat".equals(dependency.getArtifact().getArtifactId())
&& !JavaScopes.TEST.equals(dependency.getScope())) { && !MavenDependencyScopes.TEST.equals(dependency.getScope())) {
pluginValidationManager.reportPluginValidationIssue( pluginValidationManager.reportPluginValidationIssue(
PluginValidationManager.IssueLocality.EXTERNAL, PluginValidationManager.IssueLocality.EXTERNAL,
session, session,

View File

@ -26,10 +26,10 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.maven.plugin.PluginValidationManager; import org.apache.maven.plugin.PluginValidationManager;
import org.apache.maven.repository.internal.scopes.MavenDependencyScopes;
import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.resolution.ArtifactDescriptorResult; import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.util.artifact.JavaScopes;
/** /**
* Detects Maven3 dependencies scope. * Detects Maven3 dependencies scope.
@ -51,7 +51,8 @@ class MavenScopeDependenciesValidator extends AbstractMavenPluginDependenciesVal
Artifact pluginArtifact, Artifact pluginArtifact,
ArtifactDescriptorResult artifactDescriptorResult) { ArtifactDescriptorResult artifactDescriptorResult) {
Set<String> mavenArtifacts = artifactDescriptorResult.getDependencies().stream() Set<String> mavenArtifacts = artifactDescriptorResult.getDependencies().stream()
.filter(d -> !JavaScopes.PROVIDED.equals(d.getScope()) && !JavaScopes.TEST.equals(d.getScope())) .filter(d -> !MavenDependencyScopes.PROVIDED.equals(d.getScope())
&& !MavenDependencyScopes.TEST.equals(d.getScope()))
.map(org.eclipse.aether.graph.Dependency::getArtifact) .map(org.eclipse.aether.graph.Dependency::getArtifact)
.filter(a -> "org.apache.maven".equals(a.getGroupId())) .filter(a -> "org.apache.maven".equals(a.getGroupId()))
.filter(a -> !DefaultPluginValidationManager.EXPECTED_PROVIDED_SCOPE_EXCLUSIONS_GA.contains( .filter(a -> !DefaultPluginValidationManager.EXPECTED_PROVIDED_SCOPE_EXCLUSIONS_GA.contains(

View File

@ -33,6 +33,7 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency; import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Exclusion; import org.apache.maven.model.Exclusion;
import org.apache.maven.repository.internal.scopes.MavenDependencyScopes;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession;
@ -47,7 +48,6 @@ import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.resolution.ArtifactResult; import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest; import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.util.artifact.ArtifactIdUtils; import org.eclipse.aether.util.artifact.ArtifactIdUtils;
import org.eclipse.aether.util.artifact.JavaScopes;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -130,7 +130,7 @@ public class DefaultProjectDependenciesResolver implements ProjectDependenciesRe
Dependency dependency = dependencies.get(key); Dependency dependency = dependencies.get(key);
Collection<Exclusion> exclusions = dependency != null ? dependency.getExclusions() : null; Collection<Exclusion> exclusions = dependency != null ? dependency.getExclusions() : null;
org.eclipse.aether.graph.Dependency dep = RepositoryUtils.toDependency(artifact, exclusions); org.eclipse.aether.graph.Dependency dep = RepositoryUtils.toDependency(artifact, exclusions);
if (!JavaScopes.SYSTEM.equals(dep.getScope()) if (!MavenDependencyScopes.SYSTEM.equals(dep.getScope())
&& dep.getArtifact().getFile() != null) { && dep.getArtifact().getFile() != null) {
// enable re-resolution // enable re-resolution
org.eclipse.aether.artifact.Artifact art = dep.getArtifact(); org.eclipse.aether.artifact.Artifact art = dep.getArtifact();

View File

@ -45,13 +45,12 @@ import org.apache.maven.model.RepositoryPolicy;
import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.apache.maven.repository.internal.MavenSessionBuilderSupplier;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.testing.PlexusTest; import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.LocalRepository;
import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
@ -64,7 +63,10 @@ public abstract class AbstractCoreMavenComponentTestCase {
protected PlexusContainer container; protected PlexusContainer container;
@Inject @Inject
protected MavenRepositorySystem repositorySystem; protected RepositorySystem repositorySystem;
@Inject
protected MavenRepositorySystem mavenRepositorySystem;
@Inject @Inject
protected org.apache.maven.project.ProjectBuilder projectBuilder; protected org.apache.maven.project.ProjectBuilder projectBuilder;
@ -160,9 +162,11 @@ public abstract class AbstractCoreMavenComponentTestCase {
protected void initRepoSession(ProjectBuildingRequest request) throws Exception { protected void initRepoSession(ProjectBuildingRequest request) throws Exception {
File localRepoDir = new File(request.getLocalRepository().getBasedir()); File localRepoDir = new File(request.getLocalRepository().getBasedir());
LocalRepository localRepo = new LocalRepository(localRepoDir); LocalRepository localRepo = new LocalRepository(localRepoDir, "simple");
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession(); RepositorySystemSession session = new MavenSessionBuilderSupplier(repositorySystem)
session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo)); .get()
.withLocalRepositories(localRepo)
.build();
request.setRepositorySession(session); request.setRepositorySession(session);
} }
@ -188,7 +192,7 @@ public abstract class AbstractCoreMavenComponentTestCase {
repository.setReleases(policy); repository.setReleases(policy);
repository.setSnapshots(policy); repository.setSnapshots(policy);
return Arrays.asList(repositorySystem.buildArtifactRepository(repository)); return Arrays.asList(mavenRepositorySystem.buildArtifactRepository(repository));
} }
protected List<ArtifactRepository> getPluginArtifactRepositories() throws InvalidRepositoryException { protected List<ArtifactRepository> getPluginArtifactRepositories() throws InvalidRepositoryException {
@ -198,7 +202,7 @@ public abstract class AbstractCoreMavenComponentTestCase {
protected ArtifactRepository getLocalRepository() throws InvalidRepositoryException { protected ArtifactRepository getLocalRepository() throws InvalidRepositoryException {
File repoDir = new File(getBasedir(), "target/local-repo").getAbsoluteFile(); File repoDir = new File(getBasedir(), "target/local-repo").getAbsoluteFile();
return repositorySystem.createLocalRepository(repoDir); return mavenRepositorySystem.createLocalRepository(repoDir);
} }
protected class ProjectBuilder { protected class ProjectBuilder {

View File

@ -24,7 +24,7 @@ import java.util.Collections;
import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.root.RootLocator; import org.apache.maven.model.root.RootLocator;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -37,7 +37,7 @@ public class DefaultSessionTest {
@Test @Test
void testRootDirectoryWithNull() { void testRootDirectoryWithNull() {
RepositorySystemSession rss = MavenRepositorySystemUtils.newSession(); RepositorySystemSession rss = new DefaultRepositorySystemSession(h -> false);
DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest();
MavenSession ms = new MavenSession(null, rss, mer, null); MavenSession ms = new MavenSession(null, rss, mer, null);
DefaultSession session = DefaultSession session =
@ -51,7 +51,7 @@ public class DefaultSessionTest {
@Test @Test
void testRootDirectory() { void testRootDirectory() {
RepositorySystemSession rss = MavenRepositorySystemUtils.newSession(); RepositorySystemSession rss = new DefaultRepositorySystemSession(h -> false);
DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest();
MavenSession ms = new MavenSession(null, rss, mer, null); MavenSession ms = new MavenSession(null, rss, mer, null);
ms.getRequest().setRootDirectory(Paths.get("myRootDirectory")); ms.getRequest().setRootDirectory(Paths.get("myRootDirectory"));

View File

@ -20,6 +20,7 @@ package org.apache.maven.internal.impl;
import javax.inject.Inject; import javax.inject.Inject;
import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -47,7 +48,7 @@ import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.scope.internal.MojoExecutionScope; import org.apache.maven.execution.scope.internal.MojoExecutionScope;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.apache.maven.repository.internal.MavenSessionBuilderSupplier;
import org.apache.maven.rtinfo.RuntimeInformation; import org.apache.maven.rtinfo.RuntimeInformation;
import org.apache.maven.session.scope.internal.SessionScope; import org.apache.maven.session.scope.internal.SessionScope;
import org.apache.maven.toolchain.DefaultToolchainManagerPrivate; import org.apache.maven.toolchain.DefaultToolchainManagerPrivate;
@ -105,7 +106,11 @@ class TestApi {
@BeforeEach @BeforeEach
void setup() { void setup() {
RepositorySystemSession rss = MavenRepositorySystemUtils.newSession(); // create session with any local repo, is redefined anyway below
RepositorySystemSession rss = new MavenSessionBuilderSupplier(repositorySystem)
.get()
.withLocalRepositoryBaseDirectories(new File("target"))
.build();
DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest();
DefaultMavenExecutionResult meres = new DefaultMavenExecutionResult(); DefaultMavenExecutionResult meres = new DefaultMavenExecutionResult();
MavenSession ms = new MavenSession(rss, mer, meres); MavenSession ms = new MavenSession(rss, mer, meres);

View File

@ -22,7 +22,6 @@ import javax.inject.Inject;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.testing.PlexusTest; import org.codehaus.plexus.testing.PlexusTest;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
@ -57,7 +56,7 @@ public abstract class AbstractRepositoryTestCase {
} }
public static RepositorySystemSession newMavenRepositorySystemSession(RepositorySystem system) { public static RepositorySystemSession newMavenRepositorySystemSession(RepositorySystem system) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession(); DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(h -> false);
LocalRepository localRepo = new LocalRepository("target/local-repo"); LocalRepository localRepo = new LocalRepository("target/local-repo");
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo)); session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));

View File

@ -30,7 +30,6 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.bridge.MavenRepositorySystem; import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.model.building.ModelBuildingException; import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelProblem; import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.testing.PlexusTest; import org.codehaus.plexus.testing.PlexusTest;
import org.eclipse.aether.DefaultRepositoryCache; import org.eclipse.aether.DefaultRepositoryCache;
@ -145,7 +144,7 @@ public abstract class AbstractMavenProjectTestCase {
protected void initRepoSession(ProjectBuildingRequest request) { protected void initRepoSession(ProjectBuildingRequest request) {
File localRepo = new File(request.getLocalRepository().getBasedir()); File localRepo = new File(request.getLocalRepository().getBasedir());
DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
repoSession.setCache(new DefaultRepositoryCache()); repoSession.setCache(new DefaultRepositoryCache());
repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo)); repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
request.setRepositorySession(repoSession); request.setRepositorySession(repoSession);

View File

@ -37,7 +37,6 @@ import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.ReportSet; import org.apache.maven.model.ReportSet;
import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.project.harness.PomTestWrapper; import org.apache.maven.project.harness.PomTestWrapper;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.codehaus.plexus.testing.PlexusTest; import org.codehaus.plexus.testing.PlexusTest;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
@ -1887,7 +1886,7 @@ class PomConstructionTest {
? ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 ? ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0
: ModelBuildingRequest.VALIDATION_LEVEL_STRICT); : ModelBuildingRequest.VALIDATION_LEVEL_STRICT);
DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
LocalRepository localRepo = LocalRepository localRepo =
new LocalRepository(config.getLocalRepository().getBasedir()); new LocalRepository(config.getLocalRepository().getBasedir());
repoSession.setLocalRepositoryManager( repoSession.setLocalRepositoryManager(

View File

@ -29,7 +29,6 @@ import org.apache.maven.model.Dependency;
import org.apache.maven.model.Parent; import org.apache.maven.model.Parent;
import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.model.resolution.UnresolvableModelException; import org.apache.maven.model.resolution.UnresolvableModelException;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.impl.RemoteRepositoryManager; import org.eclipse.aether.impl.RemoteRepositoryManager;
@ -191,7 +190,7 @@ class ProjectModelResolverTest extends AbstractMavenProjectTestCase {
private ModelResolver newModelResolver() throws Exception { private ModelResolver newModelResolver() throws Exception {
final File localRepo = new File(this.getLocalRepository().getBasedir()); final File localRepo = new File(this.getLocalRepository().getBasedir());
final DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); final DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo)); repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
return new ProjectModelResolver( return new ProjectModelResolver(

View File

@ -34,7 +34,6 @@ import org.apache.maven.project.DefaultProjectBuilder;
import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.harness.PomTestWrapper; import org.apache.maven.project.harness.PomTestWrapper;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.apache.maven.settings.v4.SettingsStaxReader; import org.apache.maven.settings.v4.SettingsStaxReader;
import org.codehaus.plexus.testing.PlexusTest; import org.codehaus.plexus.testing.PlexusTest;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
@ -111,7 +110,7 @@ class PomConstructionWithSettingsTest {
"local", localRepoUrl, new DefaultRepositoryLayout(), null, null)); "local", localRepoUrl, new DefaultRepositoryLayout(), null, null));
config.setActiveProfileIds(settings.getActiveProfiles()); config.setActiveProfileIds(settings.getActiveProfiles());
DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
LocalRepository localRepo = LocalRepository localRepo =
new LocalRepository(config.getLocalRepository().getBasedir()); new LocalRepository(config.getLocalRepository().getBasedir());
repoSession.setLocalRepositoryManager( repoSession.setLocalRepositoryManager(

View File

@ -18,6 +18,9 @@
*/ */
package org.apache.maven.repository.internal; package org.apache.maven.repository.internal;
import org.apache.maven.repository.internal.scopes.MavenDependencyContextRefiner;
import org.apache.maven.repository.internal.scopes.MavenScopeDeriver;
import org.apache.maven.repository.internal.scopes.MavenScopeSelector;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.SessionBuilder; import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
import org.eclipse.aether.artifact.ArtifactTypeRegistry; import org.eclipse.aether.artifact.ArtifactTypeRegistry;
@ -34,9 +37,6 @@ import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
import org.eclipse.aether.util.graph.selector.ScopeDependencySelector; import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer; import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
import org.eclipse.aether.util.graph.transformer.ConflictResolver; import org.eclipse.aether.util.graph.transformer.ConflictResolver;
import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser; import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
@ -49,7 +49,9 @@ import static java.util.Objects.requireNonNull;
* assist those clients that employ the repository system outside of an IoC container, Maven plugins should instead * assist those clients that employ the repository system outside of an IoC container, Maven plugins should instead
* always use regular dependency injection to acquire the repository system. * always use regular dependency injection to acquire the repository system.
* *
* @deprecated See {@link MavenSessionBuilderSupplier}
*/ */
@Deprecated
public final class MavenRepositorySystemUtils { public final class MavenRepositorySystemUtils {
private MavenRepositorySystemUtils() { private MavenRepositorySystemUtils() {
@ -78,9 +80,9 @@ public final class MavenRepositorySystemUtils {
session.setDependencySelector(depFilter); session.setDependencySelector(depFilter);
DependencyGraphTransformer transformer = new ConflictResolver( DependencyGraphTransformer transformer = new ConflictResolver(
new NearestVersionSelector(), new JavaScopeSelector(), new NearestVersionSelector(), new MavenScopeSelector(),
new SimpleOptionalitySelector(), new JavaScopeDeriver()); new SimpleOptionalitySelector(), new MavenScopeDeriver());
transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner()); transformer = new ChainedDependencyGraphTransformer(transformer, new MavenDependencyContextRefiner());
session.setDependencyGraphTransformer(transformer); session.setDependencyGraphTransformer(transformer);
session.setArtifactTypeRegistry(newArtifactTypeRegistry()); session.setArtifactTypeRegistry(newArtifactTypeRegistry());
@ -138,9 +140,9 @@ public final class MavenRepositorySystemUtils {
session.setDependencySelector(depFilter); session.setDependencySelector(depFilter);
DependencyGraphTransformer transformer = new ConflictResolver( DependencyGraphTransformer transformer = new ConflictResolver(
new NearestVersionSelector(), new JavaScopeSelector(), new NearestVersionSelector(), new MavenScopeSelector(),
new SimpleOptionalitySelector(), new JavaScopeDeriver()); new SimpleOptionalitySelector(), new MavenScopeDeriver());
transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner()); transformer = new ChainedDependencyGraphTransformer(transformer, new MavenDependencyContextRefiner());
session.setDependencyGraphTransformer(transformer); session.setDependencyGraphTransformer(transformer);
session.setArtifactTypeRegistry(artifactTypeRegistry); session.setArtifactTypeRegistry(artifactTypeRegistry);

View File

@ -0,0 +1,136 @@
/*
* 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;
import java.util.function.Supplier;
import org.apache.maven.repository.internal.scopes.MavenDependencyContextRefiner;
import org.apache.maven.repository.internal.scopes.MavenScopeDeriver;
import org.apache.maven.repository.internal.scopes.MavenScopeSelector;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.artifact.DefaultArtifactType;
import org.eclipse.aether.collection.DependencyGraphTransformer;
import org.eclipse.aether.collection.DependencyManager;
import org.eclipse.aether.collection.DependencySelector;
import org.eclipse.aether.collection.DependencyTraverser;
import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
import org.eclipse.aether.util.graph.selector.AndDependencySelector;
import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;
import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
import static java.util.Objects.requireNonNull;
/**
* A simple {@link Supplier} of {@link SessionBuilder} instances, that on each call supplies newly
* constructed instance. To create session out of builder, use {@link SessionBuilder#build()}. For proper closing
* of sessions, use {@link CloseableSession#close()} method on built instance(s).
* <p>
* Extend this class and override methods to customize, if needed.
*
* @since 4.0.0
*/
public class MavenSessionBuilderSupplier implements Supplier<SessionBuilder> {
protected final RepositorySystem repositorySystem;
public MavenSessionBuilderSupplier(RepositorySystem repositorySystem) {
this.repositorySystem = requireNonNull(repositorySystem);
}
protected DependencyTraverser getDependencyTraverser() {
return new FatArtifactTraverser();
}
protected DependencyManager getDependencyManager() {
return new ClassicDependencyManager(true); // same default as in Maven4
}
protected DependencySelector getDependencySelector() {
return new AndDependencySelector(
new ScopeDependencySelector("test", "provided"),
new OptionalDependencySelector(),
new ExclusionDependencySelector());
}
protected DependencyGraphTransformer getDependencyGraphTransformer() {
return new ChainedDependencyGraphTransformer(
new ConflictResolver(
new NearestVersionSelector(), new MavenScopeSelector(),
new SimpleOptionalitySelector(), new MavenScopeDeriver()),
new MavenDependencyContextRefiner());
}
protected ArtifactTypeRegistry getArtifactTypeRegistry() {
DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
stereotypes.add(new DefaultArtifactType("pom"));
stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
return stereotypes;
}
protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
return new SimpleArtifactDescriptorPolicy(true, true);
}
protected void configureSessionBuilder(SessionBuilder session) {
session.setDependencyTraverser(getDependencyTraverser());
session.setDependencyManager(getDependencyManager());
session.setDependencySelector(getDependencySelector());
session.setDependencyGraphTransformer(getDependencyGraphTransformer());
session.setArtifactTypeRegistry(getArtifactTypeRegistry());
session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
}
/**
* Creates a new Maven-like repository system session by initializing the session with values typical for
* Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
* graphs, most other settings remain at their generic default value. Use the various setters to further configure
* the session with authentication, mirror, proxy and other information required for your environment. At least,
* local repository manager needs to be configured to make session be able to create session instance.
*
* @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must
* be set on builder to make it able to create session instances.
*/
@Override
public SessionBuilder get() {
SessionBuilder builder = repositorySystem.createSessionBuilder();
configureSessionBuilder(builder);
return builder;
}
}

View File

@ -0,0 +1,81 @@
/*
* 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.scopes;
import org.eclipse.aether.RepositoryException;
import org.eclipse.aether.collection.DependencyGraphTransformationContext;
import org.eclipse.aether.collection.DependencyGraphTransformer;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import static java.util.Objects.requireNonNull;
/**
* A dependency graph transformer that refines the request context for nodes that belong to the "project" context by
* appending the buildpath type to which the node belongs. For instance, a compile-time project dependency will be
* assigned the request context "project/compile".
*
* @see DependencyNode#getRequestContext()
*
* @since 4.0.0
*/
public final class MavenDependencyContextRefiner implements DependencyGraphTransformer {
public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
throws RepositoryException {
requireNonNull(node, "node cannot be null");
requireNonNull(context, "context cannot be null");
String ctx = node.getRequestContext();
if ("project".equals(ctx)) {
String scope = getBuildpathScope(node);
if (scope != null) {
ctx += '/' + scope;
node.setRequestContext(ctx);
}
}
for (DependencyNode child : node.getChildren()) {
transformGraph(child, context);
}
return node;
}
private String getBuildpathScope(DependencyNode node) {
Dependency dependency = node.getDependency();
if (dependency == null) {
return null;
}
String scope = dependency.getScope();
if (MavenDependencyScopes.COMPILE.equals(scope)
|| MavenDependencyScopes.SYSTEM.equals(scope)
|| MavenDependencyScopes.PROVIDED.equals(scope)) {
return MavenDependencyScopes.COMPILE;
} else if (MavenDependencyScopes.RUNTIME.equals(scope)) {
return MavenDependencyScopes.RUNTIME;
} else if (MavenDependencyScopes.TEST.equals(scope)) {
return MavenDependencyScopes.TEST;
}
return null;
}
}

View File

@ -0,0 +1,53 @@
/*
* 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.scopes;
/**
* The dependency scopes used for Java dependencies in Maven. This class defines labels only, that are doing pass-thru
* over Resolver.
*
* @since 4.0.0
*/
public final class MavenDependencyScopes {
/**
* Important: keep this label in sync with Resolver.
*
* TODO: once Resolver 2.0.0-alpha-7 is out, use org.eclipse.aether.util.artifact.DependencyScopes#SYSTEM
*/
public static final String SYSTEM = "system";
public static final String COMPILE_ONLY = "compile-only";
public static final String COMPILE = "compile";
public static final String PROVIDED = "provided";
public static final String RUNTIME = "runtime";
public static final String TEST_ONLY = "test-only";
public static final String TEST = "test";
public static final String TEST_RUNTIME = "test-runtime";
private MavenDependencyScopes() {
// hide constructor
}
}

View File

@ -0,0 +1,62 @@
/*
* 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.scopes;
import org.eclipse.aether.RepositoryException;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;
import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeContext;
import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeDeriver;
/**
* A scope deriver for use with {@link ConflictResolver} that supports the scopes from {@link MavenDependencyScopes}.
*
* @since 4.0.0
*/
public final class MavenScopeDeriver extends ScopeDeriver {
/**
* Creates a new instance of this scope deriver.
*/
public MavenScopeDeriver() {}
@Override
public void deriveScope(ScopeContext context) throws RepositoryException {
context.setDerivedScope(getDerivedScope(context.getParentScope(), context.getChildScope()));
}
private String getDerivedScope(String parentScope, String childScope) {
String derivedScope;
if (MavenDependencyScopes.SYSTEM.equals(childScope) || MavenDependencyScopes.TEST.equals(childScope)) {
derivedScope = childScope;
} else if (parentScope == null || parentScope.isEmpty() || MavenDependencyScopes.COMPILE.equals(parentScope)) {
derivedScope = childScope;
} else if (MavenDependencyScopes.TEST.equals(parentScope)
|| MavenDependencyScopes.RUNTIME.equals(parentScope)) {
derivedScope = parentScope;
} else if (MavenDependencyScopes.SYSTEM.equals(parentScope)
|| MavenDependencyScopes.PROVIDED.equals(parentScope)) {
derivedScope = MavenDependencyScopes.PROVIDED;
} else {
derivedScope = MavenDependencyScopes.RUNTIME;
}
return derivedScope;
}
}

View File

@ -0,0 +1,86 @@
/*
* 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.scopes;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.aether.RepositoryException;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;
import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictContext;
import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictItem;
import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeSelector;
/**
* A scope selector for use with {@link ConflictResolver} that supports the scopes from {@link MavenDependencyScopes}.
* In general, this selector picks the widest scope present among conflicting dependencies where e.g. "compile" is
* wider than "runtime" which is wider than "test". If however a direct dependency is involved, its scope is selected.
*
* @since 4.0.0
*/
public final class MavenScopeSelector extends ScopeSelector {
/**
* Creates a new instance of this scope selector.
*/
public MavenScopeSelector() {}
@Override
public void selectScope(ConflictContext context) throws RepositoryException {
String scope = context.getWinner().getDependency().getScope();
if (!MavenDependencyScopes.SYSTEM.equals(scope)) {
scope = chooseEffectiveScope(context.getItems());
}
context.setScope(scope);
}
private String chooseEffectiveScope(Collection<ConflictItem> items) {
Set<String> scopes = new HashSet<>();
for (ConflictItem item : items) {
if (item.getDepth() <= 1) {
return item.getDependency().getScope();
}
scopes.addAll(item.getScopes());
}
return chooseEffectiveScope(scopes);
}
private String chooseEffectiveScope(Set<String> scopes) {
if (scopes.size() > 1) {
scopes.remove(MavenDependencyScopes.SYSTEM);
}
String effectiveScope = "";
if (scopes.size() == 1) {
effectiveScope = scopes.iterator().next();
} else if (scopes.contains(MavenDependencyScopes.COMPILE)) {
effectiveScope = MavenDependencyScopes.COMPILE;
} else if (scopes.contains(MavenDependencyScopes.RUNTIME)) {
effectiveScope = MavenDependencyScopes.RUNTIME;
} else if (scopes.contains(MavenDependencyScopes.PROVIDED)) {
effectiveScope = MavenDependencyScopes.PROVIDED;
} else if (scopes.contains(MavenDependencyScopes.TEST)) {
effectiveScope = MavenDependencyScopes.TEST;
}
return effectiveScope;
}
}

View File

@ -20,6 +20,7 @@ package org.apache.maven.repository.internal;
import javax.inject.Inject; import javax.inject.Inject;
import java.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import org.apache.maven.repository.internal.util.ConsoleRepositoryListener; import org.apache.maven.repository.internal.util.ConsoleRepositoryListener;
@ -55,10 +56,8 @@ public abstract class AbstractRepositoryTestCase {
} }
public static RepositorySystemSession newMavenRepositorySystemSession(RepositorySystem system) { public static RepositorySystemSession newMavenRepositorySystemSession(RepositorySystem system) {
SessionBuilder session = MavenRepositorySystemUtils.newSession( SessionBuilder session = new MavenSessionBuilderSupplier(system).get();
system.createSessionBuilder(), MavenRepositorySystemUtils.newArtifactTypeRegistry()); session.withLocalRepositories(new LocalRepository(new File("target/local-repo"), "simple"));
session.withLocalRepositories(new LocalRepository("target/local-repo"));
session.setTransferListener(new ConsoleTransferListener()); session.setTransferListener(new ConsoleTransferListener());
session.setRepositoryListener(new ConsoleRepositoryListener()); session.setRepositoryListener(new ConsoleRepositoryListener());