Skip to content

Commit

Permalink
Fixed mojohaus#911- add profile parameter in set-property
Browse files Browse the repository at this point in the history
Add profileId property to manage the property value to set
  • Loading branch information
mcarlett authored and andrzejj0 committed Feb 13, 2023
1 parent 8c40177 commit c88c7b2
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<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>default-artifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<properties>
<dummy-api-version>1.0.0</dummy-api-version>
</properties>

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

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<property>dummy-api-version</property>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test-profile</id>
<properties>
<dummy-api-version>test-value</dummy-api-version>
</properties>
</profile>
</profiles>
</project>

0 comments on commit c88c7b2

Please sign in to comment.