Skip to content

Commit

Permalink
Upgrade to Maven 4.0.0-alpha-9
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Nov 28, 2023
1 parent 47fde33 commit 90134a1
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 52 deletions.
9 changes: 9 additions & 0 deletions maven-plugin-testing-harness/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,17 @@ under the License.
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<!-- newer version is required by plexus-testing -->
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@

String goal();

String pom();

boolean empty() default false;
String pom() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -42,22 +44,29 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.inject.Provider;
import com.google.inject.internal.ProviderMethodsModule;
import org.apache.maven.api.MojoExecution;
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.Mojo;
import org.apache.maven.api.plugin.testing.stubs.SessionStub;
import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.configuration.internal.EnhancedComponentConfigurator;
import org.apache.maven.execution.scope.internal.MojoExecutionScope;
import org.apache.maven.internal.impl.DefaultLog;
import org.apache.maven.internal.impl.DefaultMojoExecution;
import org.apache.maven.internal.impl.InternalSession;
import org.apache.maven.internal.xml.XmlNodeImpl;
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
import org.apache.maven.session.scope.internal.SessionScope;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.configurator.ComponentConfigurator;
Expand All @@ -69,7 +78,6 @@
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.testing.PlexusExtension;
import org.codehaus.plexus.util.InterpolationFilterReader;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.Xpp3Dom;
Expand All @@ -89,6 +97,11 @@
* @see MojoParameter
*/
public class MojoExtension extends PlexusExtension implements ParameterResolver {
@Override
protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
super.customizeContainerConfiguration(containerConfiguration);
containerConfiguration.setJSR250Lifecycle(true);
}

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Expand Down Expand Up @@ -138,6 +151,28 @@ public void beforeEach(ExtensionContext context) throws Exception {

getContainer().addComponent(getContainer(), PlexusContainer.class.getName());

InternalSession s = null;
try {
s = getContainer().lookup(InternalSession.class);
} catch (Exception e) {
// ignore
}

SessionScope sessionScope = getContainer().lookup(SessionScope.class);
sessionScope.enter();
sessionScope.seed(Session.class, (Provider<Session>) () -> {
try {
return getContainer().lookup(InternalSession.class);
} catch (ComponentLookupException e) {
throw new RuntimeException(e);
}
});
sessionScope.seed(InternalSession.class, (Provider<InternalSession>)
() -> SessionStub.getMockSession(MojoExtension.getBasedir()));

MojoExecutionScope mojoExecutionScope = getContainer().lookup(MojoExecutionScope.class);
mojoExecutionScope.enter();

((DefaultPlexusContainer) getContainer()).addPlexusInjector(Collections.emptyList(), binder -> {
binder.install(ProviderMethodsModule.forObject(context.getRequiredTestInstance()));
binder.requestInjection(context.getRequiredTestInstance());
Expand Down Expand Up @@ -176,23 +211,28 @@ private Mojo lookupMojo(
String goal = injectMojo.goal();
String pom = injectMojo.pom();
String[] coord = mojoCoordinates(goal);
Xpp3Dom pomDom;
if (pom.startsWith("file:")) {
Path path = Paths.get(getBasedir()).resolve(pom.substring("file:".length()));
pomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(path.toFile()));
} else if (pom.startsWith("classpath:")) {
URL url = holder.getResource(pom.substring("classpath:".length()));
if (url == null) {
throw new IllegalStateException("Unable to find pom on classpath: " + pom);
XmlNode pluginConfiguration;
if (pom != null && !pom.isEmpty()) {
Xpp3Dom pomDom;
if (pom.startsWith("file:")) {
Path path = Paths.get(getBasedir()).resolve(pom.substring("file:".length()));
pomDom = Xpp3DomBuilder.build(Files.newBufferedReader(path));
} else if (pom.startsWith("classpath:")) {
URL url = holder.getResource(pom.substring("classpath:".length()));
if (url == null) {
throw new IllegalStateException("Unable to find pom on classpath: " + pom);
}
pomDom = Xpp3DomBuilder.build(new InputStreamReader(url.openStream()));
} else if (pom.contains("<project>")) {
pomDom = Xpp3DomBuilder.build(new StringReader(pom));
} else {
Path path = Paths.get(getBasedir()).resolve(pom);
pomDom = Xpp3DomBuilder.build(Files.newBufferedReader(path));
}
pomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(url.openStream()));
} else if (pom.contains("<project>")) {
pomDom = Xpp3DomBuilder.build(new StringReader(pom));
pluginConfiguration = extractPluginConfiguration(coord[1], pomDom);
} else {
Path path = Paths.get(getBasedir()).resolve(pom);
pomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(path.toFile()));
pluginConfiguration = new XmlNodeImpl("config");
}
XmlNode pluginConfiguration = extractPluginConfiguration(coord[1], pomDom);
if (!mojoParameters.isEmpty()) {
List<XmlNode> children = mojoParameters.stream()
.map(mp -> new XmlNodeImpl(mp.name(), mp.value()))
Expand All @@ -209,7 +249,7 @@ protected String[] mojoCoordinates(String goal) throws Exception {
return goal.split(":");
} else {
Path pluginPom = Paths.get(getBasedir(), "pom.xml");
Xpp3Dom pluginPomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(pluginPom.toFile()));
Xpp3Dom pluginPomDom = Xpp3DomBuilder.build(Files.newBufferedReader(pluginPom));
String artifactId = pluginPomDom.getChild("artifactId").getValue();
String groupId = resolveFromRootThenParent(pluginPomDom, "groupId");
String version = resolveFromRootThenParent(pluginPomDom, "version");
Expand Down Expand Up @@ -244,12 +284,16 @@ protected Mojo lookupMojo(String[] coord, XmlNode pluginConfiguration, PluginDes
try {
MojoExecution me = getContainer().lookup(MojoExecution.class);
mojoExecution = new org.apache.maven.plugin.MojoExecution(
new org.apache.maven.model.Plugin(me.getPlugin()), me.getGoal(), me.getExecutionId());
new org.apache.maven.model.Plugin(me.getPlugin().getModel()),
me.getGoal(),
me.getExecutionId());
} catch (ComponentLookupException e) {
mojoExecution = null;
}
ExpressionEvaluator evaluator = new WrapEvaluator(
getContainer(), new PluginParameterExpressionEvaluatorV4(session, project, mojoExecution));
getContainer(),
new PluginParameterExpressionEvaluatorV4(
session, project, new DefaultMojoExecution((InternalSession) session, mojoExecution)));
ComponentConfigurator configurator = new EnhancedComponentConfigurator();
configurator.configureComponent(
mojo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,47 @@
import java.util.Optional;

import org.apache.maven.api.MojoExecution;
import org.apache.maven.api.model.Plugin;
import org.apache.maven.api.Plugin;
import org.apache.maven.api.model.PluginExecution;
import org.apache.maven.api.plugin.descriptor.MojoDescriptor;
import org.apache.maven.api.xml.XmlNode;

/**
* Stub for {@link MojoExecution}.
*/
public class MojoExecutionStub implements MojoExecution {
private final String artifactId;
private final String executionId;
private final String goal;
private final XmlNode dom;

public MojoExecutionStub(String artifactId, String executionId, String goal) {
this(artifactId, executionId, goal, null);
public MojoExecutionStub(String executionId, String goal) {
this(executionId, goal, null);
}

public MojoExecutionStub(String artifactId, String executionId, String goal, XmlNode dom) {
this.artifactId = artifactId;
public MojoExecutionStub(String executionId, String goal, XmlNode dom) {
this.executionId = executionId;
this.goal = goal;
this.dom = dom;
}

@Override
public Plugin getPlugin() {
return Plugin.newBuilder().artifactId(artifactId).build();
return new PluginStub();
}

@Override
public PluginExecution getModel() {
return null;
}

@Override
public MojoDescriptor getDescriptor() {
return null;
}

@Override
public String getLifecyclePhase() {
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.api.plugin.testing.stubs;

import java.util.List;
import java.util.Map;

import org.apache.maven.api.Artifact;
import org.apache.maven.api.Dependency;
import org.apache.maven.api.Plugin;
import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
import org.apache.maven.api.plugin.descriptor.lifecycle.Lifecycle;

public class PluginStub implements Plugin {

@Override
public org.apache.maven.api.model.Plugin getModel() {
return null;
}

@Override
public PluginDescriptor getDescriptor() {
return null;
}

@Override
public List<Lifecycle> getLifecycles() {
return null;
}

@Override
public ClassLoader getClassLoader() {
return null;
}

@Override
public Artifact getArtifact() {
return null;
}

@Override
public List<Dependency> getDependencies() {
return null;
}

@Override
public Map<String, Dependency> getDependenciesMap() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,11 @@
import org.apache.maven.api.Session;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.Repository;
import org.apache.maven.api.services.ArtifactDeployer;
import org.apache.maven.api.services.ArtifactDeployerRequest;
import org.apache.maven.api.services.ArtifactFactory;
import org.apache.maven.api.services.ArtifactFactoryRequest;
import org.apache.maven.api.services.ArtifactInstaller;
import org.apache.maven.api.services.ArtifactInstallerRequest;
import org.apache.maven.api.services.ArtifactManager;
import org.apache.maven.api.services.LocalRepositoryManager;
import org.apache.maven.api.services.ProjectBuilder;
import org.apache.maven.api.services.ProjectBuilderRequest;
import org.apache.maven.api.services.ProjectBuilderResult;
import org.apache.maven.api.services.ProjectManager;
import org.apache.maven.api.services.RepositoryFactory;
import org.apache.maven.api.services.*;
import org.apache.maven.api.services.xml.ModelXmlFactory;
import org.apache.maven.internal.impl.DefaultModelXmlFactory;
import org.apache.maven.internal.impl.DefaultVersionParser;
import org.apache.maven.internal.impl.InternalSession;
import org.apache.maven.model.v4.MavenStaxReader;
import org.mockito.ArgumentMatchers;

Expand All @@ -67,15 +57,15 @@
*/
public class SessionStub {

public static Session getMockSession(String localRepo) {
public static InternalSession getMockSession(String localRepo) {
LocalRepository localRepository = mock(LocalRepository.class);
when(localRepository.getId()).thenReturn("local");
when(localRepository.getPath()).thenReturn(Paths.get(localRepo));
return getMockSession(localRepository);
}

public static Session getMockSession(LocalRepository localRepository) {
Session session = mock(Session.class);
public static InternalSession getMockSession(LocalRepository localRepository) {
InternalSession session = mock(InternalSession.class);

RepositoryFactory repositoryFactory = mock(RepositoryFactory.class);
when(repositoryFactory.createRemote(any(Repository.class))).thenAnswer(iom -> {
Expand All @@ -93,6 +83,10 @@ public static Session getMockSession(LocalRepository localRepository) {
return remoteRepository;
});

when(session.getService(VersionParser.class)).thenReturn(new DefaultVersionParser());
when(session.parseVersion(any())).thenAnswer(iom -> session.getService(VersionParser.class)
.parseVersion(iom.getArgument(0, String.class)));

LocalRepositoryManager localRepositoryManager = mock(LocalRepositoryManager.class);
when(localRepositoryManager.getPathForLocalArtifact(any(), any(), any()))
.thenAnswer(iom -> {
Expand Down

0 comments on commit 90134a1

Please sign in to comment.