Skip to content

Commit

Permalink
update-properties does not work across parent-child pom
Browse files Browse the repository at this point in the history
Fixes mojohaus#582
 o Updated PomHelper to validate version properties also across parents.
  • Loading branch information
bzamfir committed Jul 25, 2022
1 parent a5d2ca8 commit b18747d
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 50 deletions.
23 changes: 23 additions & 0 deletions src/it-repo/dummy-parent-issue-582-1.0.pom
@@ -0,0 +1,23 @@
<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>localhost</groupId>
<artifactId>dummy-parent-issue-582</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<properties>
<api>1.0</api>
</properties>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>${api}</version>
</dependency>
</dependencies>


</project>
1 change: 1 addition & 0 deletions src/it/it-update-properties-issue-582/invoker.properties
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:update-properties
20 changes: 20 additions & 0 deletions src/it/it-update-properties-issue-582/pom.xml
@@ -0,0 +1,20 @@
<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>
<parent>
<groupId>localhost</groupId>
<artifactId>dummy-parent-issue-582</artifactId>
<version>1.0</version>
</parent>

<groupId>localhost</groupId>
<artifactId>it-update-properties-issue-582</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>update-properties with one property, with dependency in parent</name>

<properties>
<api>1.0</api>
</properties>

</project>
3 changes: 3 additions & 0 deletions src/it/it-update-properties-issue-582/verify.groovy
@@ -0,0 +1,3 @@
pom = new File( basedir, "pom.xml" ).text;

assert pom =~ /<api>3.0<\/api>/
110 changes: 60 additions & 50 deletions src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
Expand Up @@ -290,7 +290,7 @@ public static boolean setProjectValue( final ModifiedPomXMLEventReader pom, Stri
*
* @param pom The pom.
* @return the project version or <code>null</code> if the project version is not defined (i.e. inherited from
* parent version).
* parent version).
* @throws XMLStreamException if something went wrong.
*/
public static String getProjectVersion( final ModifiedPomXMLEventReader pom )
Expand Down Expand Up @@ -442,7 +442,7 @@ else if ( "version".equals( elementName ) )
return null;
}
return helper.createDependencyArtifact( groupId, artifactId, VersionRange.createFromVersion( version ), "pom",
null, null, false );
null, null, false );
}

/**
Expand All @@ -467,8 +467,8 @@ public static boolean setDependencyVersion( final ModifiedPomXMLEventReader pom,

Set<String> implicitPaths =
new HashSet<>( Arrays.asList( "/project/parent/groupId", "/project/parent/artifactId",
"/project/parent/version", "/project/groupId",
"/project/artifactId", "/project/version" ) );
"/project/parent/version", "/project/groupId",
"/project/artifactId", "/project/version" ) );
Map<String, String> implicitProperties = new HashMap<>();

for ( Map.Entry<Object, Object> entry : model.getProperties().entrySet() )
Expand Down Expand Up @@ -842,7 +842,7 @@ else if ( "version".equals( elementName ) )
}
else if ( matchScopeRegex.matcher( path ).matches() )
{
if ( inMatchScope && pom.hasMark( 0 ) && pom.hasMark( 1 ) && ( haveGroupId || !needGroupId )
if ( inMatchScope && pom.hasMark( 0 ) && pom.hasMark( 1 ) && (haveGroupId || !needGroupId)
&& haveArtifactId && haveOldVersion )
{
pom.replaceBetween( 0, 1, newVersion );
Expand Down Expand Up @@ -895,15 +895,15 @@ public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHel
if ( profile.getDependencyManagement() != null )
{
addDependencyAssocations( helper, expressionEvaluator, result,
profile.getDependencyManagement().getDependencies(), false );
profile.getDependencyManagement().getDependencies(), false );
}
addDependencyAssocations( helper, expressionEvaluator, result, profile.getDependencies(), false );
if ( profile.getBuild() != null )
{
if ( profile.getBuild().getPluginManagement() != null )
{
addPluginAssociations( helper, expressionEvaluator, result,
profile.getBuild().getPluginManagement().getPlugins() );
profile.getBuild().getPluginManagement().getPlugins() );
}
addPluginAssociations( helper, expressionEvaluator, result, profile.getBuild().getPlugins() );
}
Expand All @@ -915,51 +915,61 @@ public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHel

// second, we add all the properties in the pom
addProperties( helper, result, null, model.getProperties() );
if ( model.getDependencyManagement() != null )
Model currentModel = model;
MavenProject currentPrj = project;
while ( currentPrj != null )
{
addDependencyAssocations( helper, expressionEvaluator, result,
model.getDependencyManagement().getDependencies(), false );
}
addDependencyAssocations( helper, expressionEvaluator, result, model.getDependencies(), false );
if ( model.getBuild() != null )
{
if ( model.getBuild().getPluginManagement() != null )
if ( currentModel.getDependencyManagement() != null )
{
addPluginAssociations( helper, expressionEvaluator, result,
model.getBuild().getPluginManagement().getPlugins() );
addDependencyAssocations( helper, expressionEvaluator, result,
currentModel.getDependencyManagement().getDependencies(), false );
}
addPluginAssociations( helper, expressionEvaluator, result, model.getBuild().getPlugins() );
}
if ( model.getReporting() != null )
{
addReportPluginAssociations( helper, expressionEvaluator, result, model.getReporting().getPlugins() );
}

// third, we add any associations from the active profiles
for ( Profile profile : model.getProfiles() )
{
if ( !activeProfiles.contains( profile.getId() ) )
addDependencyAssocations( helper, expressionEvaluator, result, currentModel.getDependencies(), false );
if ( currentModel.getBuild() != null )
{
continue;
if ( currentModel.getBuild().getPluginManagement() != null )
{
addPluginAssociations( helper, expressionEvaluator, result,
currentModel.getBuild().getPluginManagement().getPlugins() );
}
addPluginAssociations( helper, expressionEvaluator, result, currentModel.getBuild().getPlugins() );
}
if ( profile.getDependencyManagement() != null )
if ( currentModel.getReporting() != null )
{
addDependencyAssocations( helper, expressionEvaluator, result,
profile.getDependencyManagement().getDependencies(), false );
addReportPluginAssociations( helper, expressionEvaluator, result, currentModel.getReporting().getPlugins() );
}
addDependencyAssocations( helper, expressionEvaluator, result, profile.getDependencies(), false );
if ( profile.getBuild() != null )

// third, we add any associations from the active profiles
for ( Profile profile : currentModel.getProfiles() )
{
if ( profile.getBuild().getPluginManagement() != null )
if ( !activeProfiles.contains( profile.getId() ) )
{
addPluginAssociations( helper, expressionEvaluator, result,
profile.getBuild().getPluginManagement().getPlugins() );
continue;
}
if ( profile.getDependencyManagement() != null )
{
addDependencyAssocations( helper, expressionEvaluator, result,
profile.getDependencyManagement().getDependencies(), false );
}
addDependencyAssocations( helper, expressionEvaluator, result, profile.getDependencies(), false );
if ( profile.getBuild() != null )
{
if ( profile.getBuild().getPluginManagement() != null )
{
addPluginAssociations( helper, expressionEvaluator, result,
profile.getBuild().getPluginManagement().getPlugins() );
}
addPluginAssociations( helper, expressionEvaluator, result, profile.getBuild().getPlugins() );
}
if ( profile.getReporting() != null )
{
addReportPluginAssociations( helper, expressionEvaluator, result, profile.getReporting().getPlugins() );
}
addPluginAssociations( helper, expressionEvaluator, result, profile.getBuild().getPlugins() );
}
if ( profile.getReporting() != null )
currentPrj = currentPrj.getParent();
if ( currentPrj != null )
{
addReportPluginAssociations( helper, expressionEvaluator, result, profile.getReporting().getPlugins() );
currentModel = currentPrj.getOriginalModel();
}
}

Expand Down Expand Up @@ -1023,7 +1033,7 @@ private static void addPluginAssociations( VersionsHelper helper, ExpressionEval
VersionRange versionRange =
VersionRange.createFromVersion( (String) expressionEvaluator.evaluate( plugin.getVersion() ) );
property.addAssociation( helper.createPluginArtifact( groupId, artifactId, versionRange ),
true );
true );
if ( !propertyRef.equals( version ) )
{
addBounds( property, version, propertyRef, versionRange.toString() );
Expand Down Expand Up @@ -1080,7 +1090,7 @@ private static void addReportPluginAssociations( VersionsHelper helper, Expressi
VersionRange versionRange =
VersionRange.createFromVersion( (String) expressionEvaluator.evaluate( plugin.getVersion() ) );
property.addAssociation( helper.createPluginArtifact( groupId, artifactId, versionRange ),
true );
true );
if ( !propertyRef.equals( version ) )
{
addBounds( property, version, propertyRef, versionRange.toString() );
Expand Down Expand Up @@ -1136,11 +1146,11 @@ private static void addDependencyAssocations( VersionsHelper helper, ExpressionE
VersionRange versionRange =
VersionRange.createFromVersion( (String) expressionEvaluator.evaluate( dependency.getVersion() ) );
property.addAssociation( helper.createDependencyArtifact( groupId, artifactId, versionRange,
dependency.getType(),
dependency.getClassifier(),
dependency.getScope(),
dependency.isOptional() ),
usePluginRepositories );
dependency.getType(),
dependency.getClassifier(),
dependency.getScope(),
dependency.isOptional() ),
usePluginRepositories );
if ( !propertyRef.equals( version ) )
{
addBounds( property, version, propertyRef, versionRange.toString() );
Expand Down Expand Up @@ -1248,7 +1258,7 @@ public static void debugModules( Log logger, String message, Collection<String>
}
else
{
modules.forEach( module -> logger.debug( " " + module ));
modules.forEach( module -> logger.debug( " " + module ) );
}

}
Expand Down Expand Up @@ -1584,7 +1594,7 @@ public static int getReactorParentCount( Map<String, Model> reactor, Model model
public static StringBuilder readXmlFile( File outFile )
throws IOException
{
try( Reader reader = ReaderFactory.newXmlReader( outFile ) )
try (Reader reader = ReaderFactory.newXmlReader( outFile ))
{
return new StringBuilder( IOUtil.toString( reader ) );
}
Expand Down Expand Up @@ -1623,7 +1633,7 @@ public static List<Dependency> readImportedPOMsFromDependencyManagementSection(
String scopeElement = "scope";
Set<String> recognizedElements =
new HashSet<>( Arrays.asList( groupIdElement, artifactIdElement, versionElement, typeElement,
scopeElement ) );
scopeElement ) );
Map<String, String> depData = new HashMap<>();

pom.rewind();
Expand Down

0 comments on commit b18747d

Please sign in to comment.