From 5e01f6a46783cdc81a799ce484f22b4cd7846402 Mon Sep 17 00:00:00 2001 From: bzamfir Date: Mon, 25 Jul 2022 23:06:22 +0300 Subject: [PATCH] update-properties does not work across parent-child pom Fixes #582 o Updated PomHelper to validate version properties also across parents. --- src/it-repo/dummy-parent-issue-582-1.0.pom | 23 ++++++ .../invoker.properties | 1 + src/it/it-update-properties-issue-582/pom.xml | 20 +++++ .../verify.groovy | 3 + .../codehaus/mojo/versions/api/PomHelper.java | 82 +++++++++++-------- 5 files changed, 93 insertions(+), 36 deletions(-) create mode 100644 src/it-repo/dummy-parent-issue-582-1.0.pom create mode 100644 src/it/it-update-properties-issue-582/invoker.properties create mode 100644 src/it/it-update-properties-issue-582/pom.xml create mode 100644 src/it/it-update-properties-issue-582/verify.groovy diff --git a/src/it-repo/dummy-parent-issue-582-1.0.pom b/src/it-repo/dummy-parent-issue-582-1.0.pom new file mode 100644 index 000000000..65fc001b6 --- /dev/null +++ b/src/it-repo/dummy-parent-issue-582-1.0.pom @@ -0,0 +1,23 @@ + + 4.0.0 + + localhost + dummy-parent-issue-582 + 1.0 + pom + + + 1.0 + + + + + localhost + dummy-api + ${api} + + + + + diff --git a/src/it/it-update-properties-issue-582/invoker.properties b/src/it/it-update-properties-issue-582/invoker.properties new file mode 100644 index 000000000..9b9e55e13 --- /dev/null +++ b/src/it/it-update-properties-issue-582/invoker.properties @@ -0,0 +1 @@ +invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:update-properties \ No newline at end of file diff --git a/src/it/it-update-properties-issue-582/pom.xml b/src/it/it-update-properties-issue-582/pom.xml new file mode 100644 index 000000000..68203983f --- /dev/null +++ b/src/it/it-update-properties-issue-582/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + + localhost + dummy-parent-issue-582 + 1.0 + + + localhost + it-update-properties-issue-582 + 1.0 + pom + update-properties with one property, with dependency in parent + + + 1.0 + + + diff --git a/src/it/it-update-properties-issue-582/verify.groovy b/src/it/it-update-properties-issue-582/verify.groovy new file mode 100644 index 000000000..ab001bf4c --- /dev/null +++ b/src/it/it-update-properties-issue-582/verify.groovy @@ -0,0 +1,3 @@ +pom = new File( basedir, "pom.xml" ).text; + +assert pom =~ /3.0<\/api>/ diff --git a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index ab727488a..2f9ea4e08 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -875,7 +875,7 @@ public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHel throws ExpressionEvaluationException, IOException { ExpressionEvaluator expressionEvaluator = helper.getExpressionEvaluator( project ); - Model model = getRawModel( project ); + Model projectModel = getRawModel( project ); Map result = new TreeMap<>(); Set activeProfiles = new TreeSet<>(); @@ -885,7 +885,7 @@ public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHel } // add any properties from profiles first (as they override properties from the project - for ( Profile profile : model.getProfiles() ) + for ( Profile profile : projectModel.getProfiles() ) { if ( !activeProfiles.contains( profile.getId() ) ) { @@ -914,52 +914,62 @@ public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHel } // second, we add all the properties in the pom - addProperties( helper, result, null, model.getProperties() ); - if ( model.getDependencyManagement() != null ) + addProperties( helper, result, null, projectModel.getProperties() ); + Model model = projectModel; + 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 ( model.getDependencyManagement() != null ) { - addPluginAssociations( helper, expressionEvaluator, result, - model.getBuild().getPluginManagement().getPlugins() ); + addDependencyAssocations( helper, expressionEvaluator, result, + model.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, model.getDependencies(), false ); + if ( model.getBuild() != null ) { - continue; + if ( model.getBuild().getPluginManagement() != null ) + { + addPluginAssociations( helper, expressionEvaluator, result, + model.getBuild().getPluginManagement().getPlugins() ); + } + addPluginAssociations( helper, expressionEvaluator, result, model.getBuild().getPlugins() ); } - if ( profile.getDependencyManagement() != null ) + if ( model.getReporting() != null ) { - addDependencyAssocations( helper, expressionEvaluator, result, - profile.getDependencyManagement().getDependencies(), false ); + addReportPluginAssociations( helper, expressionEvaluator, result, model.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 : model.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() ); + model = currentPrj.getOriginalModel(); } }