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 30, 2023
1 parent 491dad4 commit 0bc1c26
Show file tree
Hide file tree
Showing 21 changed files with 430 additions and 183 deletions.
6 changes: 6 additions & 0 deletions maven-plugin-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
<version>0.0.7</version>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<!-- tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

import java.nio.file.Path;

import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.apache.maven.api.MojoExecution;
import org.apache.maven.api.Project;
import org.apache.maven.api.ResolutionScope;
import org.apache.maven.api.Session;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Component;
import org.apache.maven.api.plugin.annotations.Execute;
import org.apache.maven.api.plugin.annotations.LifecyclePhase;
import org.apache.maven.api.plugin.annotations.Mojo;
Expand All @@ -44,7 +45,7 @@
*/
@Mojo(
name = "first",
requiresDependencyResolution = ResolutionScope.TEST,
dependencyResolutionRequired = ResolutionScope.TEST,
defaultPhase = LifecyclePhase.INTEGRATION_TEST)
@Execute(phase = LifecyclePhase.GENERATE_SOURCES, lifecycle = "cobertura")
public class FirstMojo implements org.apache.maven.api.plugin.Mojo {
Expand All @@ -66,23 +67,24 @@ public class FirstMojo implements org.apache.maven.api.plugin.Mojo {
@Parameter(name = "namedParam", alias = "alias")
private String aliasedParam;

@Component
@Inject
private Session session;

@Component
@Inject
private Project project;

@Component
@Inject
private MojoExecution mojo;

@Component
@Inject
private Settings settings;

@Component
@Inject
private Log log;

@Component(role = ArtifactInstaller.class, hint = "test")
private Object custom;
@Inject
@Named("test")
private ArtifactInstaller custom;

public void execute() throws MojoException {}
}
2 changes: 1 addition & 1 deletion maven-plugin-plugin/src/it/v4api/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ assert descriptorFile.isFile()
def pluginDescriptor = new XmlParser().parse( descriptorFile );

assert pluginDescriptor.requiredJavaVersion.text() == '1.8'
assert pluginDescriptor.requiredMavenVersion.text() == '4.0.0-alpha-4'
assert pluginDescriptor.requiredMavenVersion.text() == '4.0.0-alpha-8'

def mojo = pluginDescriptor.mojos.mojo.findAll{ it.goal.text() == "first" }[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ public class DescriptorGeneratorMojo extends AbstractGeneratorMojo {
protected BuildContext buildContext;

public void generate() throws MojoExecutionException {

if (!"maven-plugin".equalsIgnoreCase(project.getArtifactId())
&& project.getArtifactId().toLowerCase().startsWith("maven-")
&& project.getArtifactId().toLowerCase().endsWith("-plugin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ public PlexusConfiguration buildConfiguration(Reader reader) throws PlexusConfig
@Override
public PluginDescriptor build(Reader reader, String source) throws PlexusConfigurationException {
PluginDescriptor pluginDescriptor = super.build(reader, source);
// elements added in plugin descriptor 1.1
ExtendedPluginDescriptor extendedPluginDescriptor = new ExtendedPluginDescriptor(pluginDescriptor);
extendedPluginDescriptor.setRequiredJavaVersion(
configuration.getChild("requiredJavaVersion").getValue());
extendedPluginDescriptor.setRequiredMavenVersion(
configuration.getChild("requiredMavenVersion").getValue());
return extendedPluginDescriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScanner;
import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScannerRequest;
import org.apache.maven.tools.plugin.javadoc.JavadocLinkGenerator;
import org.apache.maven.tools.plugin.util.PluginUtils;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
Expand All @@ -89,6 +88,8 @@
import org.eclipse.aether.resolution.ArtifactResult;
import org.objectweb.asm.Opcodes;

import static org.apache.maven.tools.plugin.util.PluginUtils.MAVEN_COMPONENTS;

/**
* JavaMojoDescriptorExtractor, a MojoDescriptor extractor to read descriptors from java classes with annotations.
* Notice that source files are also parsed to get description, since and deprecation information.
Expand Down Expand Up @@ -769,6 +770,14 @@ private List<MojoDescriptor> toMojoDescriptors(
parameter.setRequired(parameterAnnotationContent.required());

mojoDescriptor.addParameter(parameter);

if (MAVEN_COMPONENTS.values().contains(parameter.getExpression())) {
getLogger()
.warn("Deprecated @Parameter(expression = " + parameter.getExpression()
+ ") annotation for '" + parameter.getName() + "' field in "
+ mojoAnnotatedClass.getClassName()
+ ": replace with @Component or @Inject");
}
}

// Component annotations
Expand All @@ -781,18 +790,13 @@ private List<MojoDescriptor> toMojoDescriptors(
parameter.setName(componentAnnotationContent.getFieldName());

// recognize Maven-injected objects as components annotations instead of parameters
String expression = PluginUtils.MAVEN_COMPONENTS.get(componentAnnotationContent.getRoleClassName());
String expression = MAVEN_COMPONENTS.get(componentAnnotationContent.getRoleClassName());
if (expression == null) {
// normal component
parameter.setRequirement(new Requirement(
componentAnnotationContent.getRoleClassName(), componentAnnotationContent.hint()));
} else {
// not a component but a Maven object to be transformed into an expression/property: deprecated
getLogger()
.warn("Deprecated @Component annotation for '" + parameter.getName() + "' field in "
+ mojoAnnotatedClass.getClassName()
+ ": replace with @Parameter( defaultValue = \"" + expression
+ "\", readonly = true )");
parameter.setDefaultValue(expression);
parameter.setType(componentAnnotationContent.getRoleClassName());
parameter.setRequired(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public void requiresDependencyResolution(String requiresDependencyResolution) {
this.requiresDependencyResolution = ResolutionScope.valueOf(requiresDependencyResolution);
}

public void dependencyResolutionRequired(String dependencyResolutionRequired) {
this.requiresDependencyResolution = ResolutionScope.valueOf(dependencyResolutionRequired);
}

@Override
public ResolutionScope requiresDependencyCollection() {
return requiresDependencyCollection;
Expand Down Expand Up @@ -117,6 +121,10 @@ public void requiresProject(boolean requiresProject) {
this.requiresProject = requiresProject;
}

public void projectRequired(boolean requiresProject) {
this.requiresProject = requiresProject;
}

@Override
public boolean requiresReports() {
return requiresReports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -88,15 +89,23 @@ public Map<String, MojoAnnotatedClass> scan(MojoAnnotationsScannerRequest reques
Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = new HashMap<>();

try {
String mavenApiVersion = null;
for (Artifact dependency : request.getDependencies()) {
scan(mojoAnnotatedClasses, dependency.getFile(), request.getIncludePatterns(), dependency, true);
if (request.getMavenApiVersion() == null
&& dependency.getGroupId().equals("org.apache.maven")
&& (dependency.getArtifactId().equals("maven-plugin-api")
|| dependency.getArtifactId().equals("maven-api-core"))) {
request.setMavenApiVersion(dependency.getVersion());
String version = dependency.getVersion();
if (mavenApiVersion != null && !Objects.equals(version, mavenApiVersion)) {
throw new UnsupportedOperationException("Mixing Maven 3 and Maven 4 plugins is not supported."
+ " Fix your dependencies so that you depend either on maven-plugin-api for a Maven 3 plugin,"
+ " or maven-api-core for a Maven 4 plugin.");
}
mavenApiVersion = version;
}
}
request.setMavenApiVersion(mavenApiVersion);

for (File classDirectory : request.getClassesDirectories()) {
scan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class ParametersWithGenericsMojo extends AbstractMojo {
@Parameter
private Collection<Integer[]> integerArrayCollection;

@Parameter
private Map<String, List<String>> stringListStringMap;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ void scanParametersWithGenerics() throws ExtractionException, IOException {
assertNotNull(parameter);
assertEquals("java.util.List", parameter.getClassName());
assertThat(parameter.getTypeParameters()).containsExactly("java.lang.Number");

parameter = annotatedClass.getParameters().get("stringListStringMap");
assertNotNull(parameter);
assertEquals("java.util.Map", parameter.getClassName());
assertThat(parameter.getTypeParameters())
.containsExactly("java.lang.String", "java.util.List<java.lang.String>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
*/
package org.apache.maven.tools.plugin;

import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.apache.maven.api.plugin.descriptor.lifecycle.Lifecycle;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.descriptor.DuplicateMojoDescriptorException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.lifecycle.Lifecycle;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* Extensions to {@link PluginDescriptor} not supported by Maven 3.2.5.
Expand Down Expand Up @@ -223,7 +224,7 @@ public void setPluginArtifact(Artifact pluginArtifact) {
}

@Override
public Lifecycle getLifecycleMapping(String lifecycleId) throws IOException, XmlPullParserException {
public Lifecycle getLifecycleMapping(String lifecycleId) throws IOException, XMLStreamException {
return delegate.getLifecycleMapping(lifecycleId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,7 @@ public static String[] findSources(String basedir, String include, String exclud
*/
public static void sortMojos(List<MojoDescriptor> mojoDescriptors) {
if (mojoDescriptors != null) {
Collections.sort(mojoDescriptors, new Comparator<MojoDescriptor>() {
/** {@inheritDoc} */
@Override
public int compare(MojoDescriptor mojo0, MojoDescriptor mojo1) {
return mojo0.getGoal().compareToIgnoreCase(mojo1.getGoal());
}
});
mojoDescriptors.sort(Comparator.comparing(MojoDescriptor::getGoal));
}
}

Expand All @@ -136,13 +130,7 @@ public int compare(MojoDescriptor mojo0, MojoDescriptor mojo1) {
*/
public static void sortMojoParameters(List<Parameter> parameters) {
if (parameters != null) {
Collections.sort(parameters, new Comparator<Parameter>() {
/** {@inheritDoc} */
@Override
public int compare(Parameter parameter1, Parameter parameter2) {
return parameter1.getName().compareToIgnoreCase(parameter2.getName());
}
});
parameters.sort(Comparator.comparing(Parameter::getName));
}
}

Expand Down
9 changes: 9 additions & 0 deletions maven-plugin-tools-generators/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
<artifactId>velocity</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-xml-impl</artifactId>
</dependency>

<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
Expand Down
Loading

0 comments on commit 0bc1c26

Please sign in to comment.