Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8527] Re-enable consumer POM, remove unwanted managed dependencies, developers, contributors, mailing lists and issue management (to discuss) #2058

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -36,7 +36,6 @@

import org.apache.maven.api.feature.Features;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.ModelBuilder;
import org.apache.maven.api.services.ModelBuilderException;
import org.apache.maven.internal.transformation.ConsumerPomArtifactTransformer;
import org.apache.maven.model.v4.MavenStaxWriter;
Expand Down Expand Up @@ -77,8 +76,7 @@ public void injectTransformedArtifacts(RepositorySystemSession session, MavenPro
// If there is no build POM there is no reason to inject artifacts for the consumer POM.
return;
}
boolean isModel40 = ModelBuilder.MODEL_VERSION_4_0_0.equals(project.getModelVersion());
if (Features.consumerPom(session.getUserProperties(), !isModel40)) {
if (Features.consumerPom(session.getUserProperties(), true)) {
Path buildDir =
project.getBuild() != null ? Paths.get(project.getBuild().getDirectory()) : null;
if (buildDir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.maven.api.ArtifactCoordinates;
import org.apache.maven.api.DependencyCoordinates;
import org.apache.maven.api.Node;
import org.apache.maven.api.PathScope;
import org.apache.maven.api.SessionData;
import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.DependencyManagement;
Expand Down Expand Up @@ -77,20 +83,88 @@ public Model build(RepositorySystemSession session, MavenProject project, Path s

protected Model buildPom(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuilderException {
ModelBuilderResult result = buildModel(session, project, src);
ModelBuilderResult result = buildModel(session, src);
Model model = result.getRawModel();
return transform(model, project);
}

protected Model buildNonPom(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuilderException {
ModelBuilderResult result = buildModel(session, project, src);
Model model = result.getEffectiveModel();
Model model = buildEffectiveModel(session, src);
return transform(model, project);
}

private ModelBuilderResult buildModel(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuilderException {
private Model buildEffectiveModel(RepositorySystemSession session, Path src) throws ModelBuilderException {
InternalSession iSession = InternalSession.from(session);
ModelBuilderResult result = buildModel(session, src);
Model model = result.getEffectiveModel();

if (model.getDependencyManagement() != null
&& !model.getDependencyManagement().getDependencies().isEmpty()) {
ArtifactCoordinates artifact = iSession.createArtifactCoordinates(
model.getGroupId(), model.getArtifactId(), model.getVersion(), null);
DependencyCoordinates dependency = iSession.createDependencyCoordinates(artifact);
Node node = iSession.collectDependencies(dependency, PathScope.TEST_RUNTIME);

Map<String, Node> nodes =
node.stream().collect(Collectors.toMap(n -> getDependencyKey(n.getDependency()), n -> n));
Map<String, Dependency> directDeps = model.getDependencies().stream()
.collect(Collectors.toMap(d -> getDependencyKey(d), d -> d, this::merge, LinkedHashMap::new));
Map<String, Dependency> managedDeps = model.getDependencyManagement().getDependencies().stream()
.filter(d -> nodes.containsKey(getDependencyKey(d)))
.collect(Collectors.toMap(d -> getDependencyKey(d), d -> d, this::merge, LinkedHashMap::new));

// for each managed dep in the model:
// * if there is no corresponding node in the tree, discard the managed dep
// * if there's a direct dependency, apply the managed dependency to it and discard the managed dep
// * else keep the managed dep
managedDeps.keySet().retainAll(nodes.keySet());

directDeps.replaceAll((key, dep) -> {
var managedDep = managedDeps.get(key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dep --> dependency here and elsewhere. In general, avoiding abbreviations in variable names and elsewhere improves readability.

if (managedDep != null) {
if (dep.getVersion() == null && managedDep.getVersion() != null) {
dep = dep.withVersion(managedDep.getVersion());
}
if (dep.getScope() == null && managedDep.getScope() != null) {
dep = dep.withScope(managedDep.getScope());
}
if (dep.getOptional() == null && managedDep.getOptional() != null) {
dep = dep.withOptional(managedDep.getOptional());
}
if (dep.getExclusions().isEmpty()
&& !managedDep.getExclusions().isEmpty()) {
dep = dep.withExclusions(managedDep.getExclusions());
}
}
return dep;
});
managedDeps.keySet().removeAll(directDeps.keySet());

model = model.withDependencyManagement(
managedDeps.isEmpty()
? null
: model.getDependencyManagement().withDependencies(managedDeps.values()))
.withDependencies(directDeps.isEmpty() ? null : directDeps.values());
}

return model;
}

private Dependency merge(Dependency dep1, Dependency dep2) {
throw new IllegalArgumentException("Duplicate dependency: " + dep1);
}

private static String getDependencyKey(org.apache.maven.api.Dependency d) {
return d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getType() + ":" + d.getClassifier();
}

private static String getDependencyKey(Dependency d) {
return d.getGroupId() + ":" + d.getArtifactId() + ":" + (d.getType() != null ? d.getType() : "") + ":"
+ (d.getClassifier() != null ? d.getClassifier() : "");
}

private ModelBuilderResult buildModel(RepositorySystemSession session, Path src) throws ModelBuilderException {
InternalSession iSession = InternalSession.from(session);
ModelBuilderRequest.ModelBuilderRequestBuilder request = ModelBuilderRequest.builder();
request.requestType(ModelBuilderRequest.RequestType.BUILD_CONSUMER);
Expand All @@ -102,7 +176,8 @@ private ModelBuilderResult buildModel(RepositorySystemSession session, MavenProj
request.lifecycleBindingsInjector(lifecycleBindingsInjector::injectLifecycleBindings);
ModelBuilder.ModelBuilderSession mbSession =
iSession.getData().get(SessionData.key(ModelBuilder.ModelBuilderSession.class));
return mbSession.build(request.build());
ModelBuilderResult result = mbSession.build(request.build());
return result;
}

static Model transform(Model model, MavenProject project) {
Expand Down Expand Up @@ -148,12 +223,16 @@ static Model transform(Model model, MavenProject project) {
model = model.withModelVersion(modelVersion);
} else {
Model.Builder builder = prune(
Model.newBuilder(model, true)
.preserveModelVersion(false)
.root(false)
.parent(null)
.build(null),
model);
Model.newBuilder(model, true)
.preserveModelVersion(false)
.root(false)
.parent(null)
.build(null),
model)
.developers(null)
.contributors(null)
.mailingLists(null)
.issueManagement(null);
builder.profiles(prune(model.getProfiles()));

model = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.impl.DefaultRepositoryFactory;
import org.apache.maven.impl.InternalSession;
import org.apache.maven.impl.model.DefaultInterpolator;
import org.apache.maven.impl.resolver.scopes.Maven4ScopeManagerConfiguration;
import org.apache.maven.internal.impl.DefaultSession;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.testing.PlexusTest;
Expand All @@ -40,6 +41,7 @@
import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
import org.eclipse.aether.internal.impl.scope.ScopeManagerImpl;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.transfer.TransferListener;
Expand Down Expand Up @@ -67,8 +69,9 @@ protected PlexusContainer getContainer() {
return container;
}

public static RepositorySystemSession newMavenRepositorySystemSession(RepositorySystem system) {
public RepositorySystemSession newMavenRepositorySystemSession(RepositorySystem system) {
DefaultRepositorySystemSession rsession = new DefaultRepositorySystemSession(h -> false);
rsession.setScopeManager(new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE));

LocalRepository localRepo = new LocalRepository("target/local-repo");
rsession.setLocalRepositoryManager(system.newLocalRepositoryManager(rsession, localRepo));
Expand All @@ -78,21 +81,20 @@ public static RepositorySystemSession newMavenRepositorySystemSession(Repository

DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest();
MavenSession mavenSession = new MavenSession(rsession, request, new DefaultMavenExecutionResult());
DefaultSession session = new DefaultSession(
mavenSession,
null,
null,
null,
new SimpleLookup(List.of(
new DefaultRepositoryFactory(new DefaultRemoteRepositoryManager(
new DefaultUpdatePolicyAnalyzer(), new DefaultChecksumPolicyProvider())),
new DefaultInterpolator())),
null);
DefaultSession session =
new DefaultSession(mavenSession, null, null, null, new SimpleLookup(getSessionServices()), null);
InternalSession.associate(rsession, session);

return rsession;
}

protected List<Object> getSessionServices() {
return List.of(
new DefaultRepositoryFactory(new DefaultRemoteRepositoryManager(
new DefaultUpdatePolicyAnalyzer(), new DefaultChecksumPolicyProvider())),
new DefaultInterpolator());
}

public static RemoteRepository newTestRepository() throws MalformedURLException {
return new RemoteRepository.Builder(
"repo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,38 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.apache.maven.api.DependencyCoordinates;
import org.apache.maven.api.Node;
import org.apache.maven.api.PathScope;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Session;
import org.apache.maven.api.SessionData;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.DependencyResolver;
import org.apache.maven.api.services.DependencyResolverResult;
import org.apache.maven.api.services.ModelBuilder;
import org.apache.maven.api.services.ModelBuilderRequest;
import org.apache.maven.api.services.ModelSource;
import org.apache.maven.api.services.model.ModelResolver;
import org.apache.maven.api.services.model.ModelResolverException;
import org.apache.maven.di.Injector;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.impl.DefaultArtifactCoordinatesFactory;
import org.apache.maven.impl.DefaultDependencyCoordinatesFactory;
import org.apache.maven.impl.DefaultModelVersionParser;
import org.apache.maven.impl.DefaultVersionParser;
import org.apache.maven.impl.InternalSession;
import org.apache.maven.impl.resolver.MavenVersionScheme;
import org.apache.maven.internal.impl.InternalMavenSession;
import org.apache.maven.internal.transformation.AbstractRepositoryTestCase;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -63,6 +75,30 @@ void setupTransformerContext() throws Exception {
iSession.getData().set(SessionData.key(ModelResolver.class), new MyModelResolver());
}

@Override
protected List<Object> getSessionServices() {
List<Object> services = new ArrayList<>(super.getSessionServices());

DependencyResolver dependencyResolver = Mockito.mock(DependencyResolver.class);
DependencyResolverResult resolverResult = Mockito.mock(DependencyResolverResult.class);
Mockito.when(dependencyResolver.collect(
Mockito.any(Session.class),
Mockito.any(DependencyCoordinates.class),
Mockito.any(PathScope.class)))
.thenReturn(resolverResult);
Node node = Mockito.mock(Node.class);
Mockito.when(resolverResult.getRoot()).thenReturn(node);
Node child = Mockito.mock(Node.class);
Mockito.when(node.getChildren()).thenReturn(List.of(child));

services.addAll(List.of(
new DefaultArtifactCoordinatesFactory(),
new DefaultDependencyCoordinatesFactory(),
new DefaultVersionParser(new DefaultModelVersionParser(new MavenVersionScheme())),
dependencyResolver));
return services;
}

@Test
void testTrivialConsumer() throws Exception {
InternalMavenSession.from(InternalSession.from(session))
Expand Down
Loading