From 8f804a54b0339c1b2f327f4a5a34a785036a35a3 Mon Sep 17 00:00:00 2001 From: Marco Carletti Date: Thu, 26 Jan 2023 22:02:44 +0100 Subject: [PATCH] Fixed #911- add profile parameter in set-property Add profileId property to manage the property value to set --- .../mojo/versions/SetPropertyMojo.java | 12 ++- .../mojo/versions/SetPropertyMojoTest.java | 79 +++++++++++++++++++ .../set-property/profiled-new-version/pom.xml | 40 ++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/set-property/profiled-new-version/pom.xml diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java index 2bb6d7fc9..4e3d1a75d 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java @@ -85,6 +85,14 @@ public class SetPropertyMojo extends AbstractVersionsUpdaterMojo { @Parameter(property = "propertiesVersionsFile") private String propertiesVersionsFile; + /** + * The Maven profile to apply the changes. If the provided profile is not found, no changes will be applied + * + * @since 2.15 + */ + @Parameter(property = "profileId") + private String profileId = null; + @Inject public SetPropertyMojo( RepositorySystem repositorySystem, @@ -148,13 +156,13 @@ private void update(ModifiedPomXMLEventReader pom, Property[] propertiesConfig, Property currentProperty = entry.getKey(); PropertyVersions version = entry.getValue(); String newVersionGiven = currentProperty.getVersion(); - + final String profileToApply = isEmpty(profileId) ? version.getProfileId() : profileId; final String currentVersion = getProject().getProperties().getProperty(currentProperty.getName()); if (currentVersion == null) { continue; } PomHelper.setPropertyVersion( - pom, version.getProfileId(), currentProperty.getName(), defaultString(newVersionGiven)); + pom, profileToApply, currentProperty.getName(), defaultString(newVersionGiven)); } } diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java index c2890ebcb..19537af9c 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java @@ -22,10 +22,14 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.NoSuchElementException; +import java.util.UUID; +import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.MojoRule; +import org.codehaus.mojo.versions.api.PomHelper; import org.eclipse.aether.resolution.VersionRangeRequest; import org.eclipse.aether.resolution.VersionRangeResult; import org.junit.After; @@ -38,6 +42,7 @@ import static org.codehaus.mojo.versions.utils.TestUtils.tearDownTempDir; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.matchesPattern; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -121,4 +126,78 @@ public void testNullProperty() throws Exception { containsString("Please provide either 'property' or 'propertiesVersionsFile' parameter.")); } } + + @Test + public void testChangeOnlyPropertiesInTheProfile() throws Exception { + final String newVersion = UUID.randomUUID().toString(); + final Model model = getModelForProfile("test-profile", true, newVersion); + + assertThat(model.getProperties().getProperty("dummy-api-version"), is("1.0.0")); + assertThat( + model.getProfiles().stream() + .filter(profile -> "test-profile".equals(profile.getId())) + .findFirst() + .orElseThrow(() -> new NoSuchElementException("profile test-profile not found")) + .getProperties() + .getProperty("dummy-api-version"), + is(newVersion)); + } + + @Test + public void testKeepPropertiesInTheProfile() throws Exception { + final String newVersion = UUID.randomUUID().toString(); + final Model model = getModelForProfile("test-profile", false, newVersion); + + assertThat(model.getProperties().getProperty("dummy-api-version"), is(newVersion)); + assertThat( + model.getProfiles().stream() + .filter(profile -> "test-profile".equals(profile.getId())) + .findFirst() + .orElseThrow(() -> new NoSuchElementException("profile test-profile not found")) + .getProperties() + .getProperty("dummy-api-version"), + is("test-value")); + } + + @Test + public void testDoNotChangePropertyIfTheProfileNotfound() throws Exception { + final Model model = + getModelForProfile("new-profile", true, UUID.randomUUID().toString()); + + assertThat(model.getProperties().getProperty("dummy-api-version"), is("1.0.0")); + assertThat( + model.getProfiles().stream() + .filter(profile -> "new-profile".equals(profile.getId())) + .count(), + is(0L)); + assertThat( + model.getProfiles().stream() + .filter(profile -> "test-profile".equals(profile.getId())) + .findFirst() + .orElseThrow(() -> new NoSuchElementException("profile test-profile not found")) + .getProperties() + .getProperty("dummy-api-version"), + is("test-value")); + } + + private Model getModelForProfile(String profileName, Boolean setProfile, String newVersion) throws Exception { + copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set-property/profiled-new-version"), pomDir); + SetPropertyMojo mojo = (SetPropertyMojo) mojoRule.lookupConfiguredMojo(pomDir.toFile(), "set-property"); + + mojo.aetherRepositorySystem = mock(org.eclipse.aether.RepositorySystem.class); + when(mojo.aetherRepositorySystem.resolveVersionRange(any(), any(VersionRangeRequest.class))) + .then(i -> new VersionRangeResult(i.getArgument(1))); + + setVariableValueToObject(mojo, "newVersion", newVersion); + + if (setProfile) { + setVariableValueToObject(mojo, "profileId", profileName); + } + + mojo.execute(); + + final Model model = PomHelper.getRawModel( + Paths.get(pomDir.toAbsolutePath().toString(), "pom.xml").toFile()); + return model; + } } diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set-property/profiled-new-version/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set-property/profiled-new-version/pom.xml new file mode 100644 index 000000000..abf093634 --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set-property/profiled-new-version/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + default-group + default-artifact + 1.0 + pom + + + 1.0.0 + + + + + localhost + dummy-api + ${dummy-api-version} + + + + + + + org.codehaus.mojo + versions-maven-plugin + + dummy-api-version + + + + + + + test-profile + + test-value + + + +