Skip to content

Commit

Permalink
Resolves mojohaus#837: Add includeParent (default true) to UpdateProp…
Browse files Browse the repository at this point in the history
…ertiesMojo
  • Loading branch information
jarmoniuk committed Dec 2, 2022
1 parent 9576c2e commit 35372a8
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 5 deletions.
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:update-properties
invoker.mavenOpts = -DincludeParent=false
@@ -0,0 +1,14 @@
<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>default-group</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<properties>
<artifact-version>1.0</artifact-version>
</properties>

</project>
@@ -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>

<parent>
<groupId>default-group</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0</version>
<relativePath>parent-pom.xml</relativePath>
</parent>

<artifactId>child-artifact</artifactId>
<version>1.0.0</version>

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

</project>
@@ -0,0 +1 @@
assert ! ( new File( basedir, "build.log" ).text.contains( 'Property ${artifact-version}' ) )
Expand Up @@ -493,6 +493,20 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A
return true;
}

/**
* Attempts to update the property to a newer version, if that exists
* @param pom pom to update
* @param property property to update
* @param version {@link PropertyVersions} object
* @param currentVersion current version
* @param allowDowngrade if downgrades should be allowed if snapshots are not allowed
* @param unchangedSegment most major segment not to be changed
* @return new version of the artifact, if the property was updated; {@code null} if there was no update
* @throws XMLStreamException thrown from {@link ModifiedPomXMLEventReader} if the update doesn't succeed
* @throws InvalidVersionSpecificationException thrown if {@code unchangedSegment} doesn't match the version
* @throws InvalidSegmentException thrown if {@code unchangedSegment} is invalid
* @throws MojoExecutionException thrown if any other error occurs
*/
protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property,
PropertyVersions version, String currentVersion,
boolean allowDowngrade,
Expand All @@ -511,9 +525,10 @@ protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventRead
else if ( PomHelper.setPropertyVersion( pom, version.getProfileId(), property.getName(), winner.toString() ) )
{
getLog().info( "Updated ${" + property.getName() + "} from " + currentVersion + " to " + winner );
return winner;
}

return winner;
return null;
}

/**
Expand Down
Expand Up @@ -133,6 +133,16 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo
defaultValue = "true" )
protected boolean allowIncrementalUpdates = true;

/**
* <p>Whether to include parent POMs in the search. Default: {@code true}</p>
* <p>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;

// -------------------------- STATIC METHODS --------------------------

// -------------------------- OTHER METHODS --------------------------
Expand Down Expand Up @@ -164,6 +174,7 @@ protected void update( ModifiedPomXMLEventReader pom )
.withIncludeProperties( includeProperties )
.withExcludeProperties( excludeProperties )
.withAutoLinkItems( autoLinkItems )
.withIncludeParent( includeParent )
.build() );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Expand Down
Expand Up @@ -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}
Expand All @@ -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" ) ) );
}

Expand All @@ -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" ) ) );
}

Expand All @@ -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() ) );
}
}
@@ -0,0 +1,14 @@
<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>default-group</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<properties>
<artifact-version>1.0</artifact-version>
</properties>

</project>
@@ -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>

<parent>
<groupId>default-group</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0</version>
<relativePath>parent-pom.xml</relativePath>
</parent>

<artifactId>child-artifact</artifactId>
<version>1.0.0</version>

<dependencies>
<dependency>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>${artifact-version}</version>
</dependency>
</dependencies>

</project>

0 comments on commit 35372a8

Please sign in to comment.