unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
- allowIncrementalUpdates, getLog() );
- try
- {
- ArtifactVersion targetVersion =
- updatePropertyToNewestVersion( pom, property, version, currentVersion,
- allowDowngrade, unchangedSegment );
-
- if ( targetVersion != null )
- {
- for ( final ArtifactAssociation association : version.getAssociations() )
- {
- if ( ( isIncluded( association.getArtifact() ) ) )
- {
- getChangeRecorder().recordChange( DefaultChangeRecord.builder()
- .withKind(
- ChangeRecord.ChangeKind.PROPERTY )
- .withArtifact( association.getArtifact() )
- .withOldVersion( currentVersion )
- .withNewVersion( targetVersion.toString() )
- .build() );
- }
- }
- }
- }
- catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
- {
- getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
- property.getVersion(), e.getMessage() ) );
- }
- }
-
- }
+ .withIncludeParent( includeParent )
+ .build() ) );
}
}
diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojoBase.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojoBase.java
new file mode 100644
index 0000000000..4e06e726e5
--- /dev/null
+++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojoBase.java
@@ -0,0 +1,180 @@
+package org.codehaus.mojo.versions;
+/*
+ * 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.
+ */
+
+import javax.xml.stream.XMLStreamException;
+
+import java.util.Map;
+import java.util.Optional;
+
+import org.apache.maven.artifact.manager.WagonManager;
+import org.apache.maven.artifact.versioning.ArtifactVersion;
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.repository.RepositorySystem;
+import org.codehaus.mojo.versions.api.ArtifactAssociation;
+import org.codehaus.mojo.versions.api.Property;
+import org.codehaus.mojo.versions.api.PropertyVersions;
+import org.codehaus.mojo.versions.api.Segment;
+import org.codehaus.mojo.versions.api.recording.ChangeRecord;
+import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
+import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
+import org.codehaus.mojo.versions.recording.DefaultChangeRecord;
+import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
+
+import static org.codehaus.mojo.versions.utils.SegmentUtils.determineUnchangedSegment;
+
+/**
+ * Common base class for {@link UpdatePropertiesMojo}
+ * and {@link UpdatePropertyMojo}
+ */
+public abstract class UpdatePropertiesMojoBase extends AbstractVersionsDependencyUpdaterMojo
+{
+ /**
+ * Whether properties linking versions should be auto-detected or not.
+ *
+ * @since 1.0-alpha-2
+ */
+ @Parameter( property = "autoLinkItems", defaultValue = "true" )
+ protected boolean autoLinkItems;
+
+ /**
+ * If a property points to a version like 1.2.3
and your repository contains versions like
+ * 1.2.3
and 1.1.0
without settings this to true
the property will never
+ * being changed back to 1.1.0
by using -DnewVersion=[1.1.0]
.
+ *
+ * @since 3.0.0
+ */
+ @Parameter( property = "allowDowngrade", defaultValue = "false" )
+ protected boolean allowDowngrade;
+
+ /**
+ * Whether to allow the major version number to be changed.
+ *
+ * @since 2.4
+ */
+ @Parameter( property = "allowMajorUpdates", defaultValue = "true" )
+ protected boolean allowMajorUpdates;
+
+ /**
+ * Whether to allow the minor version number to be changed.
+ *
+ * Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}
+ *
+ * @since 2.4
+ */
+ @Parameter( property = "allowMinorUpdates", defaultValue = "true" )
+ protected boolean allowMinorUpdates;
+
+ /**
+ * Whether to allow the incremental version number to be changed.
+ *
+ * Note: {@code false} also implies {@linkplain #allowMajorUpdates}
+ * and {@linkplain #allowMinorUpdates} {@code false}
+ *
+ * @since 2.4
+ */
+ @Parameter( property = "allowIncrementalUpdates", defaultValue = "true" )
+ protected boolean allowIncrementalUpdates;
+
+ /**
+ * Whether to include parent POMs in the search. Default: {@code true}
+ * Setting this to {@code false} can speed up execution, but will not resolve
+ * property-bound dependencies, defined in parent POMs.
+ *
+ * @since 2.14.0
+ */
+
+ @Parameter( property = "includeParent", defaultValue = "true" )
+ protected boolean includeParent = true;
+
+ public UpdatePropertiesMojoBase( RepositorySystem repositorySystem,
+ org.eclipse.aether.RepositorySystem aetherRepositorySystem,
+ WagonManager wagonManager,
+ Map changeRecorders )
+ {
+ super( repositorySystem, aetherRepositorySystem, wagonManager, changeRecorders );
+ }
+
+ protected void update( ModifiedPomXMLEventReader pom, Map propertyVersions )
+ throws XMLStreamException
+ {
+ for ( Map.Entry entry : propertyVersions.entrySet() )
+ {
+ Property property = entry.getKey();
+ PropertyVersions version = entry.getValue();
+
+ final String currentVersion = getProject().getProperties().getProperty( property.getName() );
+ if ( currentVersion == null )
+ {
+ continue;
+ }
+ boolean canUpdateProperty = true;
+ for ( ArtifactAssociation association : version.getAssociations() )
+ {
+ if ( !( isIncluded( association.getArtifact() ) ) )
+ {
+ getLog().info(
+ "Not updating the property ${" + property.getName() + "} because it is used by artifact "
+ + association.getArtifact().toString()
+ + " and that artifact is not included in the list of "
+ + " allowed artifacts to be updated." );
+ canUpdateProperty = false;
+ break;
+ }
+ }
+
+ if ( canUpdateProperty )
+ {
+ Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
+ allowIncrementalUpdates, getLog() );
+ try
+ {
+ ArtifactVersion targetVersion =
+ updatePropertyToNewestVersion( pom, property, version, currentVersion,
+ allowDowngrade, unchangedSegment );
+
+ if ( targetVersion != null )
+ {
+ for ( final ArtifactAssociation association : version.getAssociations() )
+ {
+ if ( ( isIncluded( association.getArtifact() ) ) )
+ {
+ getChangeRecorder().recordChange( DefaultChangeRecord.builder()
+ .withKind(
+ ChangeRecord.ChangeKind.PROPERTY )
+ .withArtifact( association.getArtifact() )
+ .withOldVersion( currentVersion )
+ .withNewVersion( targetVersion.toString() )
+ .build() );
+ }
+ }
+ }
+ }
+ catch ( InvalidSegmentException | InvalidVersionSpecificationException | MojoExecutionException e )
+ {
+ getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
+ property.getVersion(), e.getMessage() ) );
+ }
+ }
+
+ }
+ }
+}
diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java
index e8371fd369..ffba2c3894 100644
--- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java
+++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java
@@ -23,27 +23,17 @@
import javax.xml.stream.XMLStreamException;
import java.util.Map;
-import java.util.Optional;
import org.apache.maven.artifact.manager.WagonManager;
-import org.apache.maven.artifact.versioning.ArtifactVersion;
-import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.repository.RepositorySystem;
-import org.codehaus.mojo.versions.api.ArtifactAssociation;
import org.codehaus.mojo.versions.api.Property;
-import org.codehaus.mojo.versions.api.PropertyVersions;
-import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.api.VersionsHelper;
-import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
-import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
-import org.codehaus.mojo.versions.recording.DefaultChangeRecord;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
-import org.codehaus.mojo.versions.utils.SegmentUtils;
/**
* Sets a property to the latest version in a given range of associated artifacts.
@@ -52,12 +42,8 @@
* @since 1.3
*/
@Mojo( name = "update-property", threadSafe = true )
-public class UpdatePropertyMojo
- extends AbstractVersionsUpdaterMojo
+public class UpdatePropertyMojo extends UpdatePropertiesMojoBase
{
-
- // ------------------------------ FIELDS ------------------------------
-
/**
* A property to update.
*
@@ -85,57 +71,6 @@ public class UpdatePropertyMojo
@Parameter( property = "newVersion" )
private String newVersion = null;
- /**
- * Whether properties linking versions should be auto-detected or not.
- *
- * @since 1.0-alpha-2
- */
- @Parameter( property = "autoLinkItems", defaultValue = "true" )
- private boolean autoLinkItems;
-
- /**
- * If a property points to a version like 1.2.3
and your repository contains versions like
- * 1.2.3
and 1.1.0
without settings this to true
the property will never
- * being changed back to 1.1.0
by using -DnewVersion=[1.1.0]
.
- *
- * @since 3.0.0
- */
- @Parameter( property = "allowDowngrade", defaultValue = "false" )
- private boolean allowDowngrade;
-
- /**
- * Whether to allow the major version number to be changed.
- *
- * @since 2.4
- */
- @Parameter( property = "allowMajorUpdates", defaultValue = "true" )
- protected boolean allowMajorUpdates;
-
- /**
- * Whether to allow the minor version number to be changed.
- *
- * Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}
- *
- * @since 2.4
- */
- @Parameter( property = "allowMinorUpdates", defaultValue = "true" )
- protected boolean allowMinorUpdates;
-
- /**
- * Whether to allow the incremental version number to be changed.
- *
- * Note: {@code false} also implies {@linkplain #allowMajorUpdates}
- * and {@linkplain #allowMinorUpdates} {@code false}
- *
- * @since 2.4
- */
- @Parameter( property = "allowIncrementalUpdates", defaultValue = "true" )
- protected boolean allowIncrementalUpdates;
-
- // -------------------------- STATIC METHODS --------------------------
-
- // -------------------------- OTHER METHODS --------------------------
-
@Inject
public UpdatePropertyMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
@@ -156,54 +91,17 @@ public UpdatePropertyMojo( RepositorySystem repositorySystem,
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException
{
- Property propertyConfig = new Property( property );
- propertyConfig.setVersion( newVersion );
- Map propertyVersions =
- this.getHelper().getVersionPropertiesMap(
- VersionsHelper.VersionPropertiesMapRequest.builder()
- .withMavenProject( getProject() )
- .withPropertyDefinitions( new Property[] {propertyConfig} )
- .withIncludeProperties( property )
- .withAutoLinkItems( autoLinkItems )
- .build() );
- for ( Map.Entry entry : propertyVersions.entrySet() )
- {
- Property property = entry.getKey();
- PropertyVersions version = entry.getValue();
-
- final String currentVersion = getProject().getProperties().getProperty( property.getName() );
- if ( currentVersion == null )
- {
- continue;
- }
-
- Optional unchangedSegment =
- SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
- allowIncrementalUpdates, getLog() );
- try
- {
- ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
- allowDowngrade, unchangedSegment );
-
- if ( targetVersion != null )
- {
- for ( final ArtifactAssociation association : version.getAssociations() )
- {
- this.getChangeRecorder().recordChange( DefaultChangeRecord.builder()
- .withKind( ChangeRecord.ChangeKind.PROPERTY )
- .withArtifact( association.getArtifact() )
- .withOldVersion( currentVersion )
- .withNewVersion( targetVersion.toString() )
- .build() );
- }
- }
- }
- catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
- {
- getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
- property.getVersion(), e.getMessage() ) );
- }
- }
+ update( pom, getHelper().getVersionPropertiesMap(
+ VersionsHelper.VersionPropertiesMapRequest.builder()
+ .withMavenProject( getProject() )
+ .withPropertyDefinitions( new Property[] { new Property( property )
+ {{
+ setVersion( newVersion );
+ }} } )
+ .withIncludeProperties( property )
+ .withAutoLinkItems( autoLinkItems )
+ .withIncludeParent( includeParent )
+ .build() ) );
}
}
diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java
index 62c034b105..2da6749d03 100644
--- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java
+++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java
@@ -22,11 +22,15 @@
import java.nio.file.Paths;
import org.codehaus.mojo.versions.change.DefaultVersionChange;
-import org.hamcrest.Matchers;
+import org.codehaus.mojo.versions.utils.TestChangeRecorder;
+import org.codehaus.mojo.versions.utils.TestUtils;
import org.junit.Test;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.is;
/**
* Unit tests for {@link UpdatePropertiesMojo}
@@ -39,7 +43,7 @@ public void testAllowMajorUpdates() throws Exception
Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-454-pom.xml" ),
Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING );
setUpMojo( "update-properties" ).execute();
- assertThat( changeRecorder.getChanges(), Matchers.hasItem(
+ assertThat( changeRecorder.getChanges(), hasItem(
new DefaultVersionChange( "default-group", "default-artifact", "1.0.0", "2.0.0-M1" ) ) );
}
@@ -51,7 +55,7 @@ public void testAllowMinorUpdates() throws Exception
UpdatePropertiesMojo mojo = setUpMojo( "update-properties" );
mojo.allowMajorUpdates = false;
mojo.execute();
- assertThat( changeRecorder.getChanges(), Matchers.hasItem(
+ assertThat( changeRecorder.getChanges(), hasItem(
new DefaultVersionChange( "default-group", "default-artifact", "1.0.0", "1.1.0-alpha" ) ) );
}
@@ -64,7 +68,23 @@ public void testAllowIncrementalUpdates() throws Exception
mojo.allowMajorUpdates = false;
mojo.allowMinorUpdates = false;
mojo.execute();
- assertThat( changeRecorder.getChanges(), Matchers.hasItem(
+ assertThat( changeRecorder.getChanges(), hasItem(
new DefaultVersionChange( "default-group", "default-artifact", "1.0.0", "1.0.1-rc1" ) ) );
}
+
+ @Test
+ public void testChangesNotRegisteredIfNoUpdatesInPom()
+ throws Exception
+ {
+ TestUtils.copyDir( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-837" ),
+ pomDir );
+ UpdatePropertiesMojo mojo = setUpMojo( "update-properties" );
+ TestChangeRecorder changeRecorder = new TestChangeRecorder();
+ setVariableValueToObject( mojo, "changeRecorders", changeRecorder.asTestMap() );
+ setVariableValueToObject( mojo, "changeRecorderFormat", "none" );
+// pomHelperClass.when( () -> PomHelper.setPropertyVersion( any(), anyString(), anyString(), anyString() ) )
+// .thenReturn( false );
+ mojo.execute( );
+ assertThat( changeRecorder.getChanges(), is( empty() ) );
+ }
}
diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/update-properties/issue-837/parent-pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/update-properties/issue-837/parent-pom.xml
new file mode 100644
index 0000000000..d01abb3eec
--- /dev/null
+++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/update-properties/issue-837/parent-pom.xml
@@ -0,0 +1,14 @@
+
+ 4.0.0
+
+ default-group
+ parent-artifact
+ 1.0.0
+ pom
+
+
+ 1.0
+
+
+
diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/update-properties/issue-837/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/update-properties/issue-837/pom.xml
new file mode 100644
index 0000000000..31c6622ece
--- /dev/null
+++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/update-properties/issue-837/pom.xml
@@ -0,0 +1,23 @@
+
+ 4.0.0
+
+
+ default-group
+ parent-artifact
+ 1.0.0
+ parent-pom.xml
+
+
+ child-artifact
+ 1.0.0
+
+
+
+ default-group
+ default-artifact
+ ${artifact-version}
+
+
+
+