This is an automated email from the ASF dual-hosted git repository. hboutemy pushed a commit to branch MPH-160 in repository https://gitbox.apache.org/repos/asf/maven-help-plugin.git
commit 67b04657b0ef0f9a4b121f92417ce8e63a6a02f4 Author: Hervé Boutemy <[email protected]> AuthorDate: Fri Feb 15 12:33:01 2019 +0100 [MPH-160] add "verbose" to effective-pom to display POM input location --- pom.xml | 45 ++++++++++++++++++ src/it/effective-pom-verbose/invoker.properties | 18 ++++++++ src/it/effective-pom-verbose/pom.xml | 37 +++++++++++++++ .../maven/plugins/help/EffectivePomMojo.java | 53 ++++++++++++++++++++-- 4 files changed, 150 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 3316d04..c8bead1 100644 --- a/pom.xml +++ b/pom.xml @@ -277,6 +277,51 @@ </execution> </executions> </plugin> + <!-- unpack old Maven core sources for maven.mdo model to be able to generate xpp3-extended-writer when not yet provided by Maven core --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <phase>generate-sources</phase> + <goals> + <goal>unpack</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>org.apache.maven</groupId> + <artifactId>apache-maven</artifactId> + <version>${mavenVersion}</version> + <type>tar.gz</type> + <classifier>src</classifier> + <outputDirectory>${project.build.directory}</outputDirectory> + </artifactItem> + </artifactItems> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.codehaus.modello</groupId> + <artifactId>modello-maven-plugin</artifactId> + <version>1.9.2-SNAPSHOT</version> + <configuration> + <version>4.0.0</version> + <models> + <model>target/apache-maven-${mavenVersion}/maven-model/src/main/mdo/maven.mdo</model> + </models> + <extendedClassnameSuffix>ExOldSupport</extendedClassnameSuffix><!-- to avoid conflict with class provided by Maven core --> + </configuration> + <executions> + <execution> + <id>modello</id> + <goals> + <goal>xpp3-extended-writer</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> </project> diff --git a/src/it/effective-pom-verbose/invoker.properties b/src/it/effective-pom-verbose/invoker.properties new file mode 100644 index 0000000..fd3f2fe --- /dev/null +++ b/src/it/effective-pom-verbose/invoker.properties @@ -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 -Dverbose diff --git a/src/it/effective-pom-verbose/pom.xml b/src/it/effective-pom-verbose/pom.xml new file mode 100644 index 0000000..2138bfe --- /dev/null +++ b/src/it/effective-pom-verbose/pom.xml @@ -0,0 +1,37 @@ +<?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> + <parent> + <artifactId>maven-plugins</artifactId> + <groupId>org.apache.maven.plugins</groupId> + <version>33</version> + </parent> + + <groupId>org.apache.maven.its.help</groupId> + <artifactId>test</artifactId> + <version>1.0-SNAPSHOT</version> + + <description> + </description> + +</project> 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 63c9d27..8e2c4b2 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java @@ -24,8 +24,11 @@ import java.io.StringWriter; import java.util.List; 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.io.xpp3.MavenXpp3Writer; +import org.apache.maven.model.io.xpp3.MavenXpp3WriterExOldSupport; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecution.Source; import org.apache.maven.plugin.MojoExecutionException; @@ -82,6 +85,14 @@ public class EffectivePomMojo @Parameter( property = "artifact" ) private String artifact; + /** + * Output POM input location as comments. + * + * @since 3.2.0 + */ + @Parameter( property = "verbose", defaultValue = "false" ) + private boolean verbose = false; + // ---------------------------------------------------------------------- // Public methods // ---------------------------------------------------------------------- @@ -120,6 +131,11 @@ public class EffectivePomMojo } String effectivePom = prettyFormat( w.toString(), encoding, false ); + if ( verbose ) + { + // tweak location tracking comment, that are put on a separate line by pretty print + effectivePom = effectivePom.replaceAll( "(?m)>\\s+<!--}", "> <!-- " ); + } if ( output != null ) { @@ -174,17 +190,25 @@ public class EffectivePomMojo * @param writer the XML writer , not null, not null. * @throws MojoExecutionException if any */ - private static void writeEffectivePom( MavenProject project, XMLWriter writer ) + private void writeEffectivePom( MavenProject project, XMLWriter writer ) throws MojoExecutionException { Model pom = project.getModel(); cleanModel( pom ); StringWriter sWriter = new StringWriter(); - MavenXpp3Writer pomWriter = new MavenXpp3Writer(); try { - pomWriter.write( sWriter, pom ); + if ( verbose ) + { + // use local xpp3 extended writer when not provided by Maven core + new EffectiveWriterExOldSupport().write( sWriter, pom ); + // TODO detect Maven core-provided xpp3 extended writer and use it instead + } + else + { + new MavenXpp3Writer().write( sWriter, pom ); + } } catch ( IOException e ) { @@ -210,4 +234,27 @@ public class EffectivePomMojo properties.putAll( pom.getProperties() ); pom.setProperties( properties ); } + + /** + * Xpp3 extended writer extension to improve default InputSource display + */ + private static class EffectiveWriterExOldSupport + extends MavenXpp3WriterExOldSupport + { + @Override + protected String toString( InputLocation location ) + { + InputSource source = location.getSource(); + + String s = source.getModelId(); // by default, display modelId + + if ( StringUtils.isBlank( s ) || s.contains( "[unknown-version]" ) ) + { + // unless it is blank or does not provide version information + s = source.toString(); + } + + return '}' + s + ", line " + location.getLineNumber() + ' '; + } + } }
