Skip to content

Commit

Permalink
Merge pull request #135 from akacme/master
Browse files Browse the repository at this point in the history
Fix for #134: Goal: use-releases doesn't processes imported POMs in dependencyManagement
  • Loading branch information
khmarbaise authored Mar 18, 2017
2 parents 4f3f5c3 + 6c9a422 commit 045e52e
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected void update( ModifiedPomXMLEventReader pom )
{
if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
{
useReleases( pom, PomHelper.readImportedPOMsFromDependencyManagementSection(pom) );
useReleases( pom, getProject().getDependencyManagement().getDependencies() );
}
if ( isProcessingDependencies() )
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
Expand Down Expand Up @@ -1625,4 +1626,77 @@ public static String getGAV( Model model )
{
return getGroupId( model ) + ":" + getArtifactId( model ) + ":" + getVersion( model );
}

/**
* Reads imported POMs from the dependency management section.
*
* @param pom
* @return a non-null list of {@link Dependency} for each imported POM
* @throws XMLStreamException
* @see <a href="https://github.com/mojohaus/versions-maven-plugin/issues/134">bug #134</a>
* @since 2.4
*/
public static List<Dependency> readImportedPOMsFromDependencyManagementSection( ModifiedPomXMLEventReader pom )
throws XMLStreamException
{
List<Dependency> importedPOMs = new ArrayList<Dependency>();
Stack<String> stack = new Stack<String>();

String groupIdElement = "groupId";
String artifactIdElement = "artifactId";
String versionElement = "version";
String typeElement = "type";
String scopeElement = "scope";
Set<String> recognizedElements =
new HashSet<String>( Arrays.asList( groupIdElement, artifactIdElement, versionElement, typeElement,
scopeElement ) );
Map<String, String> depData = new HashMap<String, String>();

pom.rewind();

String depMgmtDependencyPath = "/project/dependencyManagement/dependencies/dependency";

while ( pom.hasNext() )
{
XMLEvent event = pom.nextEvent();

if ( event.isStartElement() )
{
final String elementName = event.asStartElement().getName().getLocalPart();
String parent = "";
if ( !stack.isEmpty() )
{
parent = stack.peek();
}
String currentPath = parent + "/" + elementName;
stack.push( currentPath );

if ( currentPath.startsWith( depMgmtDependencyPath ) && recognizedElements.contains( elementName ) )
{
final String elementText = pom.getElementText().trim();
depData.put( elementName, elementText );
stack.pop();
}
}
if ( event.isEndElement() )
{
String path = stack.pop();
if ( depMgmtDependencyPath.equals( path ) )
{
if ( "pom".equals( depData.get( typeElement ) ) && "import".equals( depData.get( scopeElement ) ) )
{
Dependency dependency = new Dependency();
dependency.setGroupId( depData.get( groupIdElement ) );
dependency.setArtifactId( depData.get( artifactIdElement ) );
dependency.setVersion( depData.get( versionElement ) );
dependency.setType( depData.get( typeElement ) );
dependency.setScope( depData.get( scopeElement ) );
importedPOMs.add( dependency );
}
depData.clear();
}
}
}
return importedPOMs;
}
}
33 changes: 32 additions & 1 deletion src/test/java/org/codehaus/mojo/versions/api/PomHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.codehaus.mojo.versions.api;

import junit.framework.TestCase;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
Expand All @@ -10,13 +11,43 @@
import java.io.File;
import java.io.StringReader;
import java.net.URL;
import java.util.List;

/**
* Tets the methods of {@link PomHelper}.
* Tests the methods of {@link PomHelper}.
*/
public class PomHelperTest
extends TestCase
{
/**
* Tests if imported POMs are properly read from dependency management section. Such logic is required to resolve
* <a href="https://github.com/mojohaus/versions-maven-plugin/issues/134">bug #134</a>
*
* @throws Exception if the test fails.
*/
public void testImportedPOMsRetrievedFromDependencyManagement()
throws Exception
{
URL url = getClass().getResource( "PomHelperTest.dependencyManagementBOMs.pom.xml" );
StringBuilder input = PomHelper.readXmlFile( new File( url.getPath() ) );

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

ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory );

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

assertNotNull( dependencies );
assertEquals( 1, dependencies.size() );

Dependency dependency = dependencies.get( 0 );
assertEquals( "org.group1", dependency.getGroupId() );
assertEquals( "artifact-pom", dependency.getArtifactId() );
assertEquals( "1.0-SNAPSHOT", dependency.getVersion() );
assertEquals( "import", dependency.getScope() );
assertEquals( "pom", dependency.getType() );
}
/**
* Tests what happens when changing a long property substitution pattern, e.g.
* <a href="http://jira.codehaus.org/browse/MVERSIONS-44">MVERSIONS-44</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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>
<parent>
<groupId>org.myorg</groupId>
<artifactId>myorg-parent</artifactId>
<version>3.0-SNAPSHOT</version>
</parent>
<artifactId>bug-79</artifactId>
<packaging>jar</packaging>
<name>extracts BOMs from dependency management</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.group1</groupId>
<artifactId>artifact-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.group1</groupId>
<artifactId>artifact-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.group1</groupId>
<artifactId>artifact-pom-standard</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>

0 comments on commit 045e52e

Please sign in to comment.