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

Enhancement: add allowDowngrade capability to UseLatestReleasesMojo, UseNextReleasesMojo, UseNextVersionsMojo #883

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 @@ -96,6 +96,16 @@ public class UseLatestReleasesMojo extends UseLatestVersionsMojoBase {
@Parameter(property = "allowIncrementalUpdates", defaultValue = "true")
protected boolean allowIncrementalUpdates = true;

/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a non-snapshot version within the range fulfilling the criteria.</p>
* <p>Only valid if <code>allowSnapshots</code> is <code>false</code>.</p>
*
* @since 2.15.0
*/
@Parameter(property = "allowDowngrade", defaultValue = "false")
protected boolean allowDowngrade;

// ------------------------------ METHODS --------------------------

@Inject
Expand Down Expand Up @@ -148,7 +158,8 @@ private void useLatestReleases(
(dep, versions) -> {
try {
return getLastFiltered(
versions.getNewerVersions(dep.getVersion(), unchangedSegment, false, false), dep);
versions.getNewerVersions(dep.getVersion(), unchangedSegment, false, allowDowngrade),
dep);
} catch (InvalidSegmentException e) {
getLog().warn(String.format(
"Skipping the processing of %s:%s:%s due to: %s",
Expand All @@ -157,7 +168,8 @@ private void useLatestReleases(
return empty();
},
changeKind,
dep -> !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
dep -> allowDowngrade
|| !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
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.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
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.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
Expand All @@ -51,7 +54,15 @@
@Mojo(name = "use-next-releases", threadSafe = true)
public class UseNextReleasesMojo extends UseLatestVersionsMojoBase {

// ------------------------------ METHODS --------------------------
/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a non-snapshot version within the range fulfilling the criteria.</p>
* <p>Only valid if <code>allowSnapshots</code> is <code>false</code>.</p>
*
* @since 2.15.0
*/
@Parameter(property = "allowDowngrade", defaultValue = "false")
protected boolean allowDowngrade;

@Inject
public UseNextReleasesMojo(
Expand Down Expand Up @@ -99,9 +110,17 @@ private void useNextReleases(
useLatestVersions(
pom,
dependencies,
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), false))
.findFirst(),
(dep, versions) -> {
try {
return Arrays.stream(versions.getNewerVersions(
dep.getVersion(), Optional.empty(), false, allowDowngrade))
.findFirst();
} catch (InvalidSegmentException e) {
throw new RuntimeException(e);
}
},
changeKind,
dep -> !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
dep -> allowDowngrade
|| !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
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.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
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.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
Expand All @@ -51,6 +54,16 @@
@Mojo(name = "use-next-versions", threadSafe = true)
public class UseNextVersionsMojo extends UseLatestVersionsMojoBase {

/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a non-snapshot version within the range fulfilling the criteria.</p>
* <p>Only valid if <code>allowSnapshots</code> is <code>false</code>.</p>
*
* @since 2.15.0
*/
@Parameter(property = "allowDowngrade", defaultValue = "false")
protected boolean allowDowngrade;

// ------------------------------ METHODS --------------------------

@Inject
Expand Down Expand Up @@ -97,8 +110,15 @@ private void useNextVersions(
useLatestVersions(
pom,
dependencies,
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), allowSnapshots))
.findFirst(),
(dep, versions) -> {
try {
return Arrays.stream(versions.getNewerVersions(
dep.getVersion(), Optional.empty(), allowSnapshots, allowDowngrade))
.findFirst();
} catch (InvalidSegmentException e) {
throw new RuntimeException(e);
}
},
changeKind);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.change.DefaultVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.hamcrest.Matchers;
Expand All @@ -26,7 +27,10 @@
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;

public class UseLatestReleasesMojoTest {
Expand Down Expand Up @@ -98,4 +102,65 @@ public void testDontUpgradeToBeta()
}
assertThat(changeRecorder.getChanges(), Matchers.empty());
}

@Test
public void testAllowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));
mojo.allowDowngrade = true;

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange(
"default-group", "artifactA",
"1.0.1-SNAPSHOT", "1.0.0")));
}

@Test
public void testDisallowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(changeRecorder.getChanges(), empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
* under the License.
*/

import javax.xml.stream.XMLStreamException;

import java.util.Collections;
import java.util.HashMap;

import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.change.DefaultVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
Expand All @@ -34,14 +39,17 @@
import org.mockito.MockedStatic;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;
import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject;
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;

/**
Expand Down Expand Up @@ -123,4 +131,65 @@ public void testFindANewerRelease() throws IllegalAccessException {
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange("default-group", "dependency-artifact", "1.1.0", "1.1.1")));
}

@Test
public void testAllowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));
mojo.allowDowngrade = true;

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange(
"default-group", "artifactA",
"1.0.1-SNAPSHOT", "1.0.0")));
}

@Test
public void testDisallowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(changeRecorder.getChanges(), empty());
}
}
Loading