Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 92 additions & 22 deletions src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
Expand All @@ -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;
Expand All @@ -55,7 +56,7 @@
*/
@Mojo( name = "effective-pom", aggregator = true )
public class EffectivePomMojo
extends AbstractEffectiveMojo
extends AbstractEffectiveMojo
{
// ----------------------------------------------------------------------
// Mojo parameters
Expand Down Expand Up @@ -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
Expand All @@ -140,24 +161,73 @@ 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 = new File( subProject.getBuild().getDirectory() + "/effective.pom.xml" );
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 = new File( project.getBuild().getDirectory() + "/effective.pom.xml" );
reportEffectivePom( effectivePom, effectiveOutput );
}
}

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+<!--}", "> <!-- " );
}
return effectivePom;
}

private PrettyPrintXMLWriter getPrettyPrintXMLWriterForEffectivePom( StringWriter w, String encoding )
{
return new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
encoding, null );
}

if ( output != null )
private void reportEffectivePom( String effectivePom, File effectiveOutput ) throws MojoExecutionException
{
if ( effectiveOutput != null )
{
try
{
writeXmlFile( output, effectivePom );
writeXmlFile( effectiveOutput, effectivePom );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e );
throw new MojoExecutionException( "Cannot write effective-POM to output: " + effectiveOutput, e );
}

getLog().info( "Effective-POM written to: " + output );
getLog().info( "Effective-POM written to: " + effectiveOutput );
}
else
{
Expand Down Expand Up @@ -198,11 +268,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 );
Expand All @@ -213,7 +283,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 );
Expand Down Expand Up @@ -252,19 +322,19 @@ 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
{
Class<?> mavenXpp3WriterExClass = Class.forName( "org.apache.maven.model.io.xpp3.MavenXpp3WriterEx" );
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 );
Expand Down Expand Up @@ -312,7 +382,7 @@ private static String toString( InputLocation location )
}

private static class InputLocationStringFormatter
extends InputLocation.StringFormatter
extends InputLocation.StringFormatter
{

public String toString( InputLocation location )
Expand All @@ -326,7 +396,7 @@ public String toString( InputLocation location )
* Xpp3 extended writer extension to improve default InputSource display
*/
private static class EffectiveWriterExOldSupport
extends MavenXpp3WriterExOldSupport
extends MavenXpp3WriterExOldSupport
{

@Override
Expand All @@ -337,7 +407,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
Expand Down