Skip to content

Commit

Permalink
#321 fail the build when parsing XML of child modules fails due to in…
Browse files Browse the repository at this point in the history
…valid XML
  • Loading branch information
stefanseifert authored and khmarbaise committed May 20, 2020
1 parent 63b569e commit 9e1d45c
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/it/it-set-017-issue-321/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:set -DgroupId=* -DartifactId=* -DoldVersion=* -DnewVersion=1.3.0-SNAPSHOT
invoker.nonRecursive=true
invoker.buildResult=failure
8 changes: 8 additions & 0 deletions src/it/it-set-017-issue-321/module-a1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>localdomain.localhost</groupId>
<artifactId>module-a1</artifactId>
<packaging>pom</packaging>
<version>1.2.0-SNAPSHOT</version>
</project>
9 changes: 9 additions & 0 deletions src/it/it-set-017-issue-321/module-a2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- this file is invalid by intention - extra space before the XML prolog (accepted by Maven itself, but not by the XML parser used by the versions-maven-plugin) -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>localdomain.localhost</groupId>
<artifactId>module-a2</artifactId>
<packaging>pom</packaging>
<version>1.2.0-SNAPSHOT</version>
</project>
103 changes: 103 additions & 0 deletions src/it/it-set-017-issue-321/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>localdomain.localhost</groupId>
<artifactId>project-a</artifactId>
<packaging>pom</packaging>
<version>1.2.0-SNAPSHOT</version>
<name>mversions-321</name>

<description>
Ensure versions:set fails when one of the child modules has a POM with invalid XML.
</description>

<modules>
<module>module-a1</module>
<module>module-a2</module>
</modules>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.3.1</version>
</plugin>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>2.4.1</version>
</plugin>
<plugin>
<artifactId>maven-rar-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0-beta-7</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.0.4</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
22 changes: 22 additions & 0 deletions src/it/it-set-017-issue-321/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.io.*;
import org.codehaus.plexus.util.FileUtils;

try
{
File file = new File( basedir, "build.log" );
String buf = FileUtils.fileRead( file, "UTF-8" );
String expectedMessage = " Illegal processing instruction target (\"xml\"); xml (case insensitive) is reserved by the specs.";

if ( buf.indexOf( expectedMessage ) < 0 )
{
System.err.println( "Parsing XML of module-a2/pom.xml should fail the build" );
return false;
}
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ protected void process( File outFile )
try
{
StringBuilder input = PomHelper.readXmlFile( outFile );
ModifiedPomXMLEventReader newPom = newModifiedPomXER( input );
ModifiedPomXMLEventReader newPom = newModifiedPomXER( input, outFile.getAbsolutePath() );

update( newPom );

Expand Down Expand Up @@ -354,16 +354,17 @@ protected void process( File outFile )
* Creates a {@link org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader} from a StringBuilder.
*
* @param input The XML to read and modify.
* @param path Path pointing to the source of the XML
* @return The {@link org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader}.
*/
protected final ModifiedPomXMLEventReader newModifiedPomXER( StringBuilder input )
protected final ModifiedPomXMLEventReader newModifiedPomXER( StringBuilder input, String path )
{
ModifiedPomXMLEventReader newPom = null;
try
{
XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
newPom = new ModifiedPomXMLEventReader( input, inputFactory );
newPom = new ModifiedPomXMLEventReader( input, inputFactory, path );
}
catch ( XMLStreamException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private Map<String, String> getSuperPomPluginManagement()
try( Reader reader = ReaderFactory.newXmlReader( superPom ) )
{
StringBuilder buf = new StringBuilder( IOUtil.toString( reader ) );
ModifiedPomXMLEventReader pom = newModifiedPomXER( buf );
ModifiedPomXMLEventReader pom = newModifiedPomXER( buf, superPom.toString() );

Pattern pathRegex = Pattern.compile( "/project(/profiles/profile)?"
+ "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin" );
Expand Down Expand Up @@ -769,7 +769,7 @@ private Map<String, String> getParentsPlugins( List<MavenProject> parents )
try
{
Set<String> withVersionSpecified =
findPluginsWithVersionsSpecified( new StringBuilder( writer.toString() ) );
findPluginsWithVersionsSpecified( new StringBuilder( writer.toString() ), parentProject.getFile().getAbsolutePath() );

Map<String, String> map = getPluginManagement( interpolatedModel );
map.keySet().retainAll( withVersionSpecified );
Expand Down Expand Up @@ -850,20 +850,21 @@ public String toString()
private Set<String> findPluginsWithVersionsSpecified( MavenProject project )
throws IOException, XMLStreamException
{
return findPluginsWithVersionsSpecified( PomHelper.readXmlFile( project.getFile() ) );
return findPluginsWithVersionsSpecified( PomHelper.readXmlFile( project.getFile() ), project.getFile().getAbsolutePath() );
}

/**
* Returns a set of Strings which correspond to the plugin coordinates where there is a version specified.
*
* @param pomContents The project to get the plugins with versions specified.
* @param path Path that points to the source of the XML
* @return a set of Strings which correspond to the plugin coordinates where there is a version specified.
*/
private Set<String> findPluginsWithVersionsSpecified( StringBuilder pomContents )
private Set<String> findPluginsWithVersionsSpecified( StringBuilder pomContents, String path )
throws IOException, XMLStreamException
{
Set<String> result = new HashSet<>();
ModifiedPomXMLEventReader pom = newModifiedPomXER( pomContents );
ModifiedPomXMLEventReader pom = newModifiedPomXER( pomContents, path );

Pattern pathRegex = Pattern.compile( "/project(/profiles/profile)?"
+ "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,27 @@ public class ModifiedPomXMLEventReader
*/
private XMLEventReader backing;

/**
* Field path
*/
private final String path;

// --------------------------- CONSTRUCTORS ---------------------------

/**
* Constructor ModifiedPomXMLEventReader creates a new ModifiedPomXMLEventReader instance.
*
* @param pom of type StringBuilder
* @param factory of type XMLInputFactory
* @param path Path pointing to source of XML
* @throws XMLStreamException when
*/
public ModifiedPomXMLEventReader( StringBuilder pom, XMLInputFactory factory )
public ModifiedPomXMLEventReader( StringBuilder pom, XMLInputFactory factory, String path )
throws XMLStreamException
{
this.pom = pom;
this.factory = factory;
this.path = path;
rewind();
}

Expand Down Expand Up @@ -374,7 +381,7 @@ public boolean hasNext()
}
catch ( XMLStreamException e )
{
return false;
throw new RuntimeException("Error parsing " + path + ": " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testReplace()

XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
ModifiedPomXMLEventReader eventReader = new ModifiedPomXMLEventReader( output, inputFactory );
ModifiedPomXMLEventReader eventReader = new ModifiedPomXMLEventReader( output, inputFactory, null );
while ( eventReader.hasNext() )
{
XMLEvent event = eventReader.nextEvent();
Expand Down Expand Up @@ -208,7 +208,7 @@ public void testReplaceFancy()

XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
ModifiedPomXMLEventReader eventReader = new ModifiedPomXMLEventReader( output, inputFactory );
ModifiedPomXMLEventReader eventReader = new ModifiedPomXMLEventReader( output, inputFactory, null );

Stack<String> stack = new Stack<String>();
String path = "";
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/org/codehaus/mojo/versions/api/PomHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public void testImportedPOMsRetrievedFromDependencyManagement()
throws Exception
{
URL url = getClass().getResource( "PomHelperTest.dependencyManagementBOMs.pom.xml" );
StringBuilder input = PomHelper.readXmlFile( new File( url.getPath() ) );
File file = new File( url.getPath() );
StringBuilder input = PomHelper.readXmlFile( file );

XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );

ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory );
ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory, file.getAbsolutePath() );

List<Dependency> dependencies = PomHelper.readImportedPOMsFromDependencyManagementSection( pom );

Expand All @@ -65,12 +66,13 @@ public void testLongProperties()
throws Exception
{
URL url = getClass().getResource( "PomHelperTest.testLongProperties.pom.xml" );
StringBuilder input = PomHelper.readXmlFile( new File( url.getPath() ) );
File file = new File( url.getPath() );
StringBuilder input = PomHelper.readXmlFile( file );

XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );

ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory );
ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory, file.getAbsolutePath() );

String oldVersion = PomHelper.getProjectVersion( pom );

Expand Down

0 comments on commit 9e1d45c

Please sign in to comment.