diff --git a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java index 4d326b28..a8b63ce0 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java @@ -19,6 +19,7 @@ * under the License. */ +import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; @@ -29,8 +30,8 @@ import java.util.Properties; import org.apache.maven.model.InputLocation; -import org.apache.maven.model.Model; import org.apache.maven.model.InputSource; +import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.model.io.xpp3.MavenXpp3WriterExOldSupport; import org.apache.maven.plugin.MojoExecution; @@ -55,7 +56,7 @@ */ @Mojo( name = "effective-pom", aggregator = true ) public class EffectivePomMojo - extends AbstractEffectiveMojo + extends AbstractEffectiveMojo { // ---------------------------------------------------------------------- // Mojo parameters @@ -95,35 +96,55 @@ public class EffectivePomMojo /** * Output POM input location as comments. - * + * * @since 3.2.0 */ @Parameter( property = "verbose", defaultValue = "false" ) private boolean verbose = false; + /** + * Generate an effective pom for each module individually and + * save it in ${project.build.outputDirectory}/effective.pom.xml. + * + * @since 3.3.0 + */ + @Parameter( property = "individual", defaultValue = "false" ) + private boolean individual = false; + // ---------------------------------------------------------------------- // Public methods // ---------------------------------------------------------------------- - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ public void execute() - throws MojoExecutionException + throws MojoExecutionException { if ( StringUtils.isNotEmpty( artifact ) ) { project = getMavenProject( artifact ); projects = Collections.singletonList( project ); } + if ( individual ) + { + generateIndividualEffectivePom(); + } + else + { + generateSingleEffectivePom(); + } + } + private void generateSingleEffectivePom() throws MojoExecutionException + { StringWriter w = new StringWriter(); String encoding = output != null ? project.getModel().getModelEncoding() - : System.getProperty( "file.encoding" ); + : System.getProperty( "file.encoding" ); XMLWriter writer = - new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ), - encoding, null ); + getPrettyPrintXMLWriterForEffectivePom( w, encoding ); writeHeader( writer ); - if ( shouldWriteAllEffectivePOMsInReactor() ) { // outer root element @@ -140,24 +161,85 @@ public void execute() } String effectivePom = prettyFormat( w.toString(), encoding, false ); + effectivePom = prettyFormatVerbose( effectivePom ); + reportEffectivePom( effectivePom, output ); + } + + private void generateIndividualEffectivePom() throws MojoExecutionException + { + String encoding = project.getModel().getModelEncoding(); + if ( shouldWriteAllEffectivePOMsInReactor() ) + { + // outer root element + for ( MavenProject subProject : projects ) + { + StringWriter w = new StringWriter(); + XMLWriter writer = + getPrettyPrintXMLWriterForEffectivePom( w, encoding ); + writeHeader( writer ); + writeEffectivePom( subProject, writer ); + String effectivePom = prettyFormat( w.toString(), encoding, false ); + effectivePom = prettyFormatVerbose( effectivePom ); + File effectiveOutput = output == null ? null + : getRelativeOutput( subProject ); + reportEffectivePom( effectivePom, effectiveOutput ); + } + } + else + { + StringWriter w = new StringWriter(); + XMLWriter writer = + getPrettyPrintXMLWriterForEffectivePom( w, encoding ); + writeHeader( writer ); + writeEffectivePom( project, writer ); + String effectivePom = prettyFormat( w.toString(), encoding, false ); + effectivePom = prettyFormatVerbose( effectivePom ); + File effectiveOutput = output == null ? null + : getRelativeOutput( project ); + reportEffectivePom( effectivePom, effectiveOutput ); + } + } + + private File getRelativeOutput( MavenProject relativeProject ) + { + String rawOutputPath = output.getPath(); + String rawBasedirPath = project.getBasedir().getPath(); + String result = rawOutputPath.contains( rawBasedirPath ) + ? rawOutputPath.replace( rawBasedirPath, "" ) + : output.getName(); + return new File( relativeProject.getBuild().getDirectory() + "/" + result ); + } + + private String prettyFormatVerbose( String effectivePom ) + { if ( verbose ) { // tweak location tracking comment, that are put on a separate line by pretty print effectivePom = effectivePom.replaceAll( "(?m)>\\s+