Author: gboue Date: Sat Nov 5 17:09:51 2016 New Revision: 1768250 URL: http://svn.apache.org/viewvc?rev=1768250&view=rev Log: [MPH-106] add gav parameter to calculate effective pom for any gav, not only reactor
Adding an "artifact" parameter to the effective-pom goal. The name is to be consistent with the evaluate goal. -> Refactoring the common code into AbstractHelpMojo Added: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/ maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/invoker.properties (with props) maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/pom.xml (with props) maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/test.properties (with props) maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/verify.groovy (with props) Modified: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java Added: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/invoker.properties URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/invoker.properties?rev=1768250&view=auto ============================================================================== --- maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/invoker.properties (added) +++ maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/invoker.properties Sat Nov 5 17:09:51 2016 @@ -0,0 +1,18 @@ +# 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.goals = ${project.groupId}:${project.artifactId}:${project.version}:effective-pom Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/invoker.properties ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/invoker.properties ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/pom.xml?rev=1768250&view=auto ============================================================================== --- maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/pom.xml (added) +++ maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/pom.xml Sat Nov 5 17:09:51 2016 @@ -0,0 +1,30 @@ +<?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> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.its.help</groupId> + <artifactId>mph-106</artifactId> + <version>1.0-SNAPSHOT</version> + <description>https://issues.apache.org/jira/browse/MPH-106</description> + +</project> Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/test.properties URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/test.properties?rev=1768250&view=auto ============================================================================== --- maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/test.properties (added) +++ maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/test.properties Sat Nov 5 17:09:51 2016 @@ -0,0 +1,19 @@ +# 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. + +output = result.txt +artifact = org.apache.maven.plugins:maven-help-plugin Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/test.properties ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/test.properties ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/verify.groovy URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/verify.groovy?rev=1768250&view=auto ============================================================================== --- maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/verify.groovy (added) +++ maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/verify.groovy Sat Nov 5 17:09:51 2016 @@ -0,0 +1,26 @@ +/* + * 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. + */ +def result = new File(basedir, 'result.txt'); +assert result.exists() + +def LS = System.getProperty("line.separator") +assert result.text.find('(?s)' + +' <groupId>org.apache.maven.plugins</groupId>' + LS + +' <artifactId>maven-help-plugin</artifactId>' +) != null Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/verify.groovy ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-help-plugin/src/it/effective-pom-artifact/verify.groovy ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java?rev=1768250&r1=1768249&r2=1768250&view=diff ============================================================================== --- maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java (original) +++ maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java Sat Nov 5 17:09:51 2016 @@ -19,15 +19,26 @@ package org.apache.maven.plugins.help; * under the License. */ +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.ArtifactUtils; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.WriterFactory; -import java.io.File; -import java.io.IOException; -import java.io.Writer; - /** * Base class with some Help Mojo functionalities. * @@ -40,6 +51,30 @@ public abstract class AbstractHelpMojo { /** The maximum length of a display line. */ protected static final int LINE_LENGTH = 79; + + /** + * Maven Artifact Factory component. + */ + @Component + private ArtifactFactory artifactFactory; + + /** + * Maven Project Builder component. + */ + @Component + private MavenProjectBuilder mavenProjectBuilder; + + /** + * Remote repositories used for the project. + */ + @Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true ) + protected List<ArtifactRepository> remoteRepositories; + + /** + * Local Repository. + */ + @Parameter( defaultValue = "${localRepository}", required = true, readonly = true ) + protected ArtifactRepository localRepository; /** * Optional parameter to write the output of this help in a given file, instead of writing to the console. @@ -95,4 +130,85 @@ public abstract class AbstractHelpMojo IOUtil.close( out ); } } + + /** + * @param artifactString should respect the format <code>groupId:artifactId[:version][:classifier]</code> + * @return the <code>Artifact</code> object for the <code>artifactString</code> parameter. + * @throws MojoExecutionException if the <code>artifactString</code> doesn't respect the format. + */ + protected Artifact getArtifact( String artifactString ) + throws MojoExecutionException + { + if ( StringUtils.isEmpty( artifactString ) ) + { + throw new IllegalArgumentException( "artifact parameter could not be empty" ); + } + + String groupId; // required + String artifactId; // required + String version; // optional + String classifier = null; // optional + + String[] artifactParts = artifactString.split( ":" ); + + switch ( artifactParts.length ) + { + case 2: + groupId = artifactParts[0]; + artifactId = artifactParts[1]; + version = Artifact.LATEST_VERSION; + break; + case 3: + groupId = artifactParts[0]; + artifactId = artifactParts[1]; + version = artifactParts[2]; + break; + case 4: + groupId = artifactParts[0]; + artifactId = artifactParts[1]; + version = artifactParts[2]; + classifier = artifactParts[3]; + break; + default: + throw new MojoExecutionException( "The artifact parameter '" + artifactString + + "' should be conform to: " + "'groupId:artifactId[:version][:classifier]'." ); + } + + if ( StringUtils.isNotEmpty( classifier ) ) + { + return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", classifier ); + } + + return artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar" ); + } + + protected MavenProject getMavenProject( String artifactString ) + throws MojoExecutionException + { + Artifact artifactObj = getArtifact( artifactString ); + + if ( Artifact.SCOPE_SYSTEM.equals( artifactObj.getScope() ) ) + { + throw new MojoExecutionException( "System artifact is not be handled." ); + } + + Artifact copyArtifact = ArtifactUtils.copyArtifact( artifactObj ); + if ( !"pom".equals( copyArtifact.getType() ) ) + { + copyArtifact = + artifactFactory.createProjectArtifact( copyArtifact.getGroupId(), copyArtifact.getArtifactId(), + copyArtifact.getVersion(), copyArtifact.getScope() ); + } + + try + { + return mavenProjectBuilder.buildFromRepository( copyArtifact, remoteRepositories, localRepository ); + } + catch ( ProjectBuildingException e ) + { + throw new MojoExecutionException( "Unable to get the POM for the artifact '" + artifactString + + "'. Verify the artifact parameter." ); + } + } + } Modified: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java?rev=1768250&r1=1768249&r2=1768250&view=diff ============================================================================== --- maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java (original) +++ maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java Sat Nov 5 17:09:51 2016 @@ -42,7 +42,7 @@ import java.util.List; import java.util.Properties; /** - * Displays the effective POM as an XML for this build, with the active profiles factored in. + * Displays the effective POM as an XML for this build, with the active profiles factored in, or a specified artifact. * * @version $Id$ * @since 2.0 @@ -69,6 +69,17 @@ public class EffectivePomMojo */ @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true ) private List<MavenProject> projects; + + /** + * The artifact for which to display the effective POM. + * <br/> + * <b>Note</b>: Should respect the Maven format, i.e. <code>groupId:artifactId[:version]</code>. The + * latest version of the artifact will be used when no version is specified. + * + * @since 2.2.1 + */ + @Parameter( property = "artifact" ) + private String artifact; // ---------------------------------------------------------------------- // Public methods @@ -78,6 +89,11 @@ public class EffectivePomMojo public void execute() throws MojoExecutionException { + if ( StringUtils.isNotEmpty( artifact ) ) + { + project = getMavenProject( artifact ); + } + StringWriter w = new StringWriter(); XMLWriter writer = new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ), Modified: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java?rev=1768250&r1=1768249&r2=1768250&view=diff ============================================================================== --- maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java (original) +++ maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java Sat Nov 5 17:09:51 2016 @@ -34,9 +34,6 @@ import java.util.jar.JarInputStream; import org.apache.commons.lang3.ClassUtils; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolver; @@ -53,7 +50,6 @@ import org.apache.maven.plugins.annotati import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.path.PathTranslator; import org.apache.maven.settings.Settings; @@ -84,24 +80,12 @@ public class EvaluateMojo // ---------------------------------------------------------------------- /** - * Maven Artifact Factory component. - */ - @Component - private ArtifactFactory artifactFactory; - - /** * Input handler, needed for command line handling. */ @Component private InputHandler inputHandler; /** - * Maven Project Builder component. - */ - @Component - private MavenProjectBuilder mavenProjectBuilder; - - /** */ @Component private PathTranslator pathTranslator; @@ -136,7 +120,7 @@ public class EvaluateMojo /** * An artifact for evaluating Maven expressions. * <br/> - * <b>Note</b>: Should respect the Maven format, i.e. <code>groupId:artifactId[:version][:classifier]</code>. The + * <b>Note</b>: Should respect the Maven format, i.e. <code>groupId:artifactId[:version]</code>. The * latest version of the artifact will be used when no version is specified. */ @Parameter( property = "artifact" ) @@ -149,24 +133,12 @@ public class EvaluateMojo private String expression; /** - * Local Repository. - */ - @Parameter( defaultValue = "${localRepository}", required = true, readonly = true ) - protected ArtifactRepository localRepository; - - /** * Maven project built from the given {@link #artifact}. Otherwise, the current Maven project or the super pom. */ @Parameter( defaultValue = "${project}", readonly = true, required = true ) protected MavenProject project; /** - * Remote repositories used for the project. - */ - @Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true ) - private List<ArtifactRepository> remoteRepositories; - - /** * The system settings for Maven. */ @Parameter( defaultValue = "${settings}", readonly = true, required = true ) @@ -208,17 +180,7 @@ public class EvaluateMojo if ( StringUtils.isNotEmpty( artifact ) ) { - Artifact artifactObj = getArtifact( artifact ); - - try - { - project = getMavenProject( artifactObj ); - } - catch ( ProjectBuildingException e ) - { - throw new MojoExecutionException( "Unable to get the POM for the artifact '" + artifact - + "'. Verify the artifact parameter." ); - } + project = getMavenProject( artifact ); } if ( expression == null ) @@ -271,82 +233,6 @@ public class EvaluateMojo } /** - * @param artifactString should respect the format <code>groupId:artifactId[:version][:classifier]</code> - * @return the <code>Artifact</code> object for the <code>artifactString</code> parameter. - * @throws MojoExecutionException if the <code>artifactString</code> doesn't respect the format. - */ - private Artifact getArtifact( String artifactString ) - throws MojoExecutionException - { - if ( StringUtils.isEmpty( artifactString ) ) - { - throw new IllegalArgumentException( "artifact parameter could not be empty" ); - } - - String groupId; // required - String artifactId; // required - String version; // optional - String classifier = null; // optional - - String[] artifactParts = artifactString.split( ":" ); - - switch ( artifactParts.length ) - { - case 2: - groupId = artifactParts[0]; - artifactId = artifactParts[1]; - version = Artifact.LATEST_VERSION; - break; - case 3: - groupId = artifactParts[0]; - artifactId = artifactParts[1]; - version = artifactParts[2]; - break; - case 4: - groupId = artifactParts[0]; - artifactId = artifactParts[1]; - version = artifactParts[2]; - classifier = artifactParts[3]; - break; - default: - throw new MojoExecutionException( "The artifact parameter '" + artifactString - + "' should be conform to: " + "'groupId:artifactId[:version][:classifier]'." ); - } - - if ( StringUtils.isNotEmpty( classifier ) ) - { - return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", classifier ); - } - - return artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar" ); - } - - /** - * @param artifactObj not null - * @return the POM for the given artifact. - * @throws MojoExecutionException if the artifact has a system scope. - * @throws ProjectBuildingException when building pom. - */ - private MavenProject getMavenProject( Artifact artifactObj ) - throws MojoExecutionException, ProjectBuildingException - { - if ( Artifact.SCOPE_SYSTEM.equals( artifactObj.getScope() ) ) - { - throw new MojoExecutionException( "System artifact is not be handled." ); - } - - Artifact copyArtifact = ArtifactUtils.copyArtifact( artifactObj ); - if ( !"pom".equals( copyArtifact.getType() ) ) - { - copyArtifact = - artifactFactory.createProjectArtifact( copyArtifact.getGroupId(), copyArtifact.getArtifactId(), - copyArtifact.getVersion(), copyArtifact.getScope() ); - } - - return mavenProjectBuilder.buildFromRepository( copyArtifact, remoteRepositories, localRepository ); - } - - /** * @return a lazy loading evaluator object. * @throws MojoExecutionException if any * @throws MojoFailureException if any reflection exceptions occur or missing components. @@ -770,36 +656,33 @@ public class EvaluateMojo String resource = "META-INF/maven/org.apache.maven.plugins/maven-help-plugin/pom.properties"; InputStream resourceAsStream = EvaluateMojo.class.getClassLoader().getResourceAsStream( resource ); - Artifact helpPluginArtifact = null; - if ( resourceAsStream != null ) + if ( resourceAsStream == null ) { - Properties properties = new Properties(); - try - { - properties.load( resourceAsStream ); - } - catch ( IOException e ) + throw new MojoExecutionException( "The help plugin artifact was not found." ); + } + Properties properties = new Properties(); + try + { + properties.load( resourceAsStream ); + } + catch ( IOException e ) + { + if ( getLog().isDebugEnabled() ) { - if ( getLog().isDebugEnabled() ) - { - getLog().debug( "IOException: " + e.getMessage(), e ); - } + getLog().debug( "IOException: " + e.getMessage(), e ); } - - String artifactString = - properties.getProperty( "groupId", "unknown" ) + ":" - + properties.getProperty( "artifactId", "unknown" ) + ":" - + properties.getProperty( "version", "unknown" ); - - helpPluginArtifact = getArtifact( artifactString ); } - - if ( helpPluginArtifact == null ) + finally { - throw new MojoExecutionException( "The help plugin artifact was not found." ); + IOUtil.close( resourceAsStream ); } - return getMavenProject( helpPluginArtifact ); + String artifactString = + properties.getProperty( "groupId", "unknown" ) + ":" + + properties.getProperty( "artifactId", "unknown" ) + ":" + + properties.getProperty( "version", "unknown" ); + + return getMavenProject( artifactString ); } /**