From f5af2de22d4c0c6cee2962e9522a4d9f28a541a8 Mon Sep 17 00:00:00 2001 From: Tom Scholten Date: Fri, 28 Oct 2022 16:22:15 +0200 Subject: [PATCH 1/5] [MPH-168] - Add parameter to generate effective-pom per module --- .../org/apache/maven/plugins/help/EffectivePomMojo.java | 9 +++++++++ 1 file changed, 9 insertions(+) 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..3e284e6f 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java @@ -101,6 +101,15 @@ public class EffectivePomMojo @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 // ---------------------------------------------------------------------- From 0cb37010373832c1f5bc7ee25129756a9b545a9d Mon Sep 17 00:00:00 2001 From: Tom Scholten Date: Fri, 28 Oct 2022 16:23:02 +0200 Subject: [PATCH 2/5] [MPH-168] - Reformat code based on maven-idea-codestyle.xml --- .../maven/plugins/help/EffectivePomMojo.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) 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 3e284e6f..14a7740f 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java @@ -29,8 +29,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 +55,7 @@ */ @Mojo( name = "effective-pom", aggregator = true ) public class EffectivePomMojo - extends AbstractEffectiveMojo + extends AbstractEffectiveMojo { // ---------------------------------------------------------------------- // Mojo parameters @@ -95,7 +95,7 @@ public class EffectivePomMojo /** * Output POM input location as comments. - * + * * @since 3.2.0 */ @Parameter( property = "verbose", defaultValue = "false" ) @@ -114,9 +114,11 @@ public class EffectivePomMojo // Public methods // ---------------------------------------------------------------------- - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ public void execute() - throws MojoExecutionException + throws MojoExecutionException { if ( StringUtils.isNotEmpty( artifact ) ) { @@ -126,10 +128,10 @@ public void execute() 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 ); + new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ), + encoding, null ); writeHeader( writer ); @@ -207,11 +209,11 @@ private boolean shouldWriteAllEffectivePOMsInReactor() * Method for writing the effective pom informations of the current build. * * @param project the project of the current build, not null. - * @param writer the XML writer , not null, not null. + * @param writer the XML writer , not null, not null. * @throws MojoExecutionException if any */ private void writeEffectivePom( MavenProject project, XMLWriter writer ) - throws MojoExecutionException + throws MojoExecutionException { Model pom = project.getModel(); cleanModel( pom ); @@ -222,7 +224,7 @@ private void writeEffectivePom( MavenProject project, XMLWriter writer ) if ( verbose ) { // try to use Maven core-provided xpp3 extended writer (available since Maven 3.6.1) - if ( ! writeMavenXpp3WriterEx( sWriter, pom ) ) + if ( !writeMavenXpp3WriterEx( sWriter, pom ) ) { // xpp3 extended writer not provided by Maven core, use local code new EffectiveWriterExOldSupport().write( sWriter, pom ); @@ -261,11 +263,11 @@ private static void cleanModel( Model pom ) private void warnWriteMavenXpp3WriterEx( Throwable t ) { getLog().warn( "Unexpected exception while running Maven Model Extended Writer, " - + "falling back to old internal implementation.", t ); + + "falling back to old internal implementation.", t ); } private boolean writeMavenXpp3WriterEx( Writer writer, Model model ) - throws IOException + throws IOException { try { @@ -273,7 +275,7 @@ private boolean writeMavenXpp3WriterEx( Writer writer, Model model ) Object mavenXpp3WriterEx = mavenXpp3WriterExClass.getDeclaredConstructor().newInstance(); Method setStringFormatter = - mavenXpp3WriterExClass.getMethod( "setStringFormatter", InputLocation.StringFormatter.class ); + mavenXpp3WriterExClass.getMethod( "setStringFormatter", InputLocation.StringFormatter.class ); setStringFormatter.invoke( mavenXpp3WriterEx, new InputLocationStringFormatter() ); Method write = mavenXpp3WriterExClass.getMethod( "write", Writer.class, Model.class ); @@ -321,7 +323,7 @@ private static String toString( InputLocation location ) } private static class InputLocationStringFormatter - extends InputLocation.StringFormatter + extends InputLocation.StringFormatter { public String toString( InputLocation location ) @@ -335,7 +337,7 @@ public String toString( InputLocation location ) * Xpp3 extended writer extension to improve default InputSource display */ private static class EffectiveWriterExOldSupport - extends MavenXpp3WriterExOldSupport + extends MavenXpp3WriterExOldSupport { @Override @@ -346,7 +348,7 @@ public String toString( InputLocation location ) @Override protected void writeXpp3DomToSerializer( Xpp3Dom dom, XmlSerializer serializer ) - throws IOException + throws IOException { // default method uses Xpp3Dom input location tracking, not available in older Maven versions // use old Xpp3Dom serialization, without input location tracking From a07c1cfc3b15fac725d8fc396bc123a4126719f5 Mon Sep 17 00:00:00 2001 From: Tom Scholten Date: Fri, 28 Oct 2022 17:15:46 +0200 Subject: [PATCH 3/5] [MPH-168] - Enable generation of effective POM for each module individually --- .../maven/plugins/help/EffectivePomMojo.java | 93 ++++++++++++++----- 1 file changed, 71 insertions(+), 22 deletions(-) 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 14a7740f..e9602c96 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; @@ -125,50 +126,98 @@ public void execute() project = getMavenProject( artifact ); projects = Collections.singletonList( project ); } - - StringWriter w = new StringWriter(); - String encoding = output != null ? project.getModel().getModelEncoding() - : System.getProperty( "file.encoding" ); - XMLWriter writer = - new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ), - encoding, null ); - - writeHeader( writer ); - - if ( shouldWriteAllEffectivePOMsInReactor() ) + if ( individual ) { - // outer root element - writer.startElement( "projects" ); - for ( MavenProject subProject : projects ) + 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 = new File( subProject.getBuild().getDirectory() + "/effective.pom.xml" ); + reportEffectivePom( effectivePom, effectiveOutput ); + } + } + else { - writeEffectivePom( subProject, writer ); + 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 = new File( project.getBuild().getDirectory() + "/effective.pom.xml" ); + reportEffectivePom( effectivePom, effectiveOutput ); } - writer.endElement(); } else { - writeEffectivePom( project, writer ); + StringWriter w = new StringWriter(); + String encoding = output != null ? project.getModel().getModelEncoding() + : System.getProperty( "file.encoding" ); + XMLWriter writer = + getPrettyPrintXMLWriterForEffectivePom( w, encoding ); + + writeHeader( writer ); + if ( shouldWriteAllEffectivePOMsInReactor() ) + { + // outer root element + writer.startElement( "projects" ); + for ( MavenProject subProject : projects ) + { + writeEffectivePom( subProject, writer ); + } + writer.endElement(); + } + else + { + writeEffectivePom( project, writer ); + } + + String effectivePom = prettyFormat( w.toString(), encoding, false ); + effectivePom = prettyFormatVerbose( effectivePom ); + reportEffectivePom( effectivePom, output ); } + } - String effectivePom = prettyFormat( w.toString(), encoding, false ); + 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+