Skip to content

Commit

Permalink
Run the v4 mojo (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 26, 2024
1 parent 41032b8 commit f054780
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

invoker.java.version = 17+
invoker.goals.1 = clean install
invoker.maven.version = 3
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Execute;
import org.apache.maven.api.plugin.annotations.Mojo;
import org.apache.maven.api.plugin.annotations.Parameter;
import org.apache.maven.api.services.ArtifactInstaller;
import org.apache.maven.api.settings.Settings;

/**
* Test mojo for the v4 api plugin descriptor generation.
Expand All @@ -42,7 +38,6 @@
* @since 1.2
*/
@Mojo(name = "first", defaultPhase = "integration-test")
@Execute(phase = "generate-sources", lifecycle = "cobertura")
public class FirstMojo implements org.apache.maven.api.plugin.Mojo {

/**
Expand Down Expand Up @@ -71,15 +66,10 @@ public class FirstMojo implements org.apache.maven.api.plugin.Mojo {
@Inject
private MojoExecution mojo;

@Inject
private Settings settings;

@Inject
private Log log;

@Inject
@Named("test")
private ArtifactInstaller custom;

public void execute() throws MojoException {}
public void execute() throws MojoException {
log.info("Executing first");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ assert mojo.projectRequired.text() == 'true'
assert mojo.onlineRequired.text() == 'false'
assert mojo.aggregator.text() == 'false'
assert mojo.phase.text() == 'integration-test'
assert mojo.executePhase.text() == 'generate-sources'
assert mojo.executeLifecycle.text() == 'cobertura'

assert mojo.parameters.parameter.size() == 3

Expand Down
21 changes: 21 additions & 0 deletions maven-plugin-plugin/src/it/v4api-4x/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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.

invoker.java.version = 17+
invoker.goals.1 = clean install
invoker.goals.2 = org.apache.maven.its:v4api:1.0-SNAPSHOT:first
invoker.maven.version = 4+
81 changes: 81 additions & 0 deletions maven-plugin-plugin/src/it/v4api-4x/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its</groupId>
<artifactId>v4api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Maven Integration Test :: v4api</name>
<description>
Test maven-plugin-plugin support for the Maven 4.x api.
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-core</artifactId>
<version>@maven4Version@</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@compilerPluginVersion@</version>
<configuration>
<source>1.8</source><!-- avoid ${maven.compiler.source} since value is not as expected -->
<target>1.8</target><!-- avoid ${maven.compiler.target} since value is not as expected -->
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<goalPrefix>prefix</goalPrefix>
</configuration>
<executions>
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.its.v4api;

import java.nio.file.Path;

import org.apache.maven.api.MojoExecution;
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Mojo;
import org.apache.maven.api.plugin.annotations.Parameter;

/**
* Test mojo for the v4 api plugin descriptor generation.
* This mojo is not actually runnable because:
* - it's using a custom lifecycle which is not defined
* - it has a @Inject dependency on ArtifactInstaller (hint=test) which does not exist
*
* @since 1.2
*/
@Mojo(name = "first", defaultPhase = "integration-test")
public class FirstMojo implements org.apache.maven.api.plugin.Mojo {

/**
* Project directory.
*/
@Parameter(defaultValue = "${basedir}", readonly = true)
private Path basedir;

@Parameter(property = "first.touchFile", defaultValue = "${project.build.directory}/touch.txt", required = true)
private Path touchFile;

/**
* @since 0.1
* @deprecated As of 0.2
*/
@Deprecated
@Parameter(name = "namedParam", alias = "alias")
private String aliasedParam;

@Inject
private Session session;

@Inject
private Project project;

@Inject
private MojoExecution mojo;

@Inject
private Log log;

public void execute() throws MojoException {
log.info("Executing first");
}
}
83 changes: 83 additions & 0 deletions maven-plugin-plugin/src/it/v4api-4x/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.
*/

import groovy.xml.XmlParser

File descriptorFile = new File( basedir, "target/classes/META-INF/maven/plugin.xml" );
assert descriptorFile.isFile()

def pluginDescriptor = new XmlParser().parse( descriptorFile );

assert pluginDescriptor.requiredJavaVersion.text() == '17'
assert pluginDescriptor.requiredMavenVersion.text() == '4.0.0-beta-3'

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

assert mojo.goal.text() == 'first'
assert mojo.implementation.text() == 'org.apache.maven.its.v4api.FirstMojo'
assert mojo.language.text() == 'java'
assert mojo.description.text().startsWith('Test mojo for the v4 api plugin descriptor generation.')
assert mojo.projectRequired.text() == 'true'
assert mojo.onlineRequired.text() == 'false'
assert mojo.aggregator.text() == 'false'
assert mojo.phase.text() == 'integration-test'

assert mojo.parameters.parameter.size() == 3

parameter = mojo.parameters.parameter.findAll{ it.name.text() == "basedir" }[0]
assert parameter.name.text() == 'basedir'
assert parameter.alias.isEmpty()
assert parameter.type.text() == 'java.nio.file.Path'
assert parameter.since.isEmpty()
assert parameter.deprecated.isEmpty()
assert parameter.required.text() == 'false'
assert parameter.editable.text() == 'false'
assert parameter.description.text() == 'Project directory.'
assert parameter.defaultValue.text() == '${basedir}'
assert parameter.expression.isEmpty()

parameter = mojo.parameters.parameter.findAll{ it.name.text() == "touchFile" }[0]
assert parameter.name.text() == 'touchFile'
assert parameter.alias.isEmpty()
assert parameter.type.text() == 'java.nio.file.Path'
assert parameter.since.isEmpty()
assert parameter.deprecated.isEmpty()
assert parameter.required.text() == 'true'
assert parameter.editable.text() == 'true'
assert parameter.description.text() == ''
assert parameter.defaultValue.text() == '${project.build.directory}/touch.txt'
assert parameter.expression.text() == '${first.touchFile}'

parameter = mojo.parameters.parameter.findAll{ it.name.text() == "namedParam" }[0]
assert parameter.name.text() == 'namedParam'
assert parameter.alias.text() == 'alias'
assert parameter.type.text() == 'java.lang.String'
assert parameter.since.text() == '0.1'
assert parameter.deprecated.text() == 'As of 0.2'
assert parameter.required.text() == 'false'
assert parameter.editable.text() == 'true'
assert parameter.description.text() == ''
assert parameter.defaultValue.isEmpty()
assert parameter.expression.isEmpty()

// check help mojo source and class
assert new File( basedir, "target/classes/org/apache/maven/its/v4api/HelpMojo.class" ).isFile()
assert new File( basedir, "target/generated-sources/plugin/org/apache/maven/its/v4api/HelpMojo.java" ).isFile()

return true;
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,14 @@ protected void analyzeVisitors(MojoClassVisitor mojoClassVisitor) throws Extract
}

// @Component annotations
List<MojoFieldVisitor> mojoFieldVisitors =
List<MojoFieldVisitor> mojoComponentVisitors =
mojoClassVisitor.findFieldWithAnnotation(new HashSet<>(Arrays.asList(COMPONENT_V3)));
for (MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors) {
for (MojoFieldVisitor mojoComponentVisitor : mojoComponentVisitors) {
ComponentAnnotationContent componentAnnotationContent =
new ComponentAnnotationContent(mojoFieldVisitor.getFieldName());
new ComponentAnnotationContent(mojoComponentVisitor.getFieldName());

Map<String, MojoAnnotationVisitor> annotationVisitorMap = mojoFieldVisitor.getAnnotationVisitorMap();
Map<String, MojoAnnotationVisitor> annotationVisitorMap =
mojoComponentVisitor.getAnnotationVisitorMap();
MojoAnnotationVisitor annotationVisitor = annotationVisitorMap.get(COMPONENT_V3);

if (annotationVisitor != null) {
Expand All @@ -362,7 +363,7 @@ protected void analyzeVisitors(MojoClassVisitor mojoClassVisitor) throws Extract
}

if (StringUtils.isEmpty(componentAnnotationContent.getRoleClassName())) {
componentAnnotationContent.setRoleClassName(mojoFieldVisitor.getClassName());
componentAnnotationContent.setRoleClassName(mojoComponentVisitor.getClassName());
}
}
mojoAnnotatedClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public interface MojoAnnotationsScanner {
Component.class.getName(),
Deprecated.class.getName(),
V4_API_ANNOTATIONS_PACKAGE + ".Parameter",
V4_API_ANNOTATIONS_PACKAGE + ".Component");
V4_API_ANNOTATIONS_PACKAGE + ".Resolution");

List<String> METHOD_LEVEL_ANNOTATIONS = Arrays.asList(
Parameter.class.getName(), Deprecated.class.getName(), V4_API_ANNOTATIONS_PACKAGE + ".Parameter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public void setV4Api(boolean v4Api) {

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(containsXhtmlTextValues);
return result;
return Objects.hash(containsXhtmlTextValues, v4Api);
}

@Override
Expand All @@ -83,6 +80,7 @@ public boolean equals(Object obj) {
return false;
}
ExtendedMojoDescriptor other = (ExtendedMojoDescriptor) obj;
return containsXhtmlTextValues == other.containsXhtmlTextValues;
return Objects.equals(containsXhtmlTextValues, other.containsXhtmlTextValues)
&& Objects.equals(v4Api, other.v4Api);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<reportingImplVersion>3.2.0</reportingImplVersion>
<compilerPluginVersion>3.11.0</compilerPluginVersion>
<javadocPluginVersion>3.5.0</javadocPluginVersion>
<version.maven-invoker-plugin>3.7.0</version.maven-invoker-plugin>
<project.build.outputTimestamp>2024-05-28T08:15:05Z</project.build.outputTimestamp>
</properties>

Expand Down

0 comments on commit f054780

Please sign in to comment.