Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #911- add profile parameter in set-property #912

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>