diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index 3ba86c0a7d..e2f01e25f8 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -64,13 +64,6 @@ public abstract class AbstractVersionDetails implements VersionDetails { */ private ArtifactVersion currentVersion = null; - /** - * Do we want to include snapshots when snapshot inclusion is not specified. Guarded by {@link #currentVersionLock}. - * - * @since 1.0-beta-1 - */ - private boolean includeSnapshots = false; - protected boolean verboseDetail = true; /** @@ -145,38 +138,11 @@ public final void setCurrentVersion(String currentVersion) { setCurrentVersion(currentVersion == null ? null : new DefaultArtifactVersion(currentVersion)); } - @Override - public final boolean isIncludeSnapshots() { - synchronized (currentVersionLock) { - return includeSnapshots; - } - } - - @Override - public final void setIncludeSnapshots(boolean includeSnapshots) { - synchronized (currentVersionLock) { - this.includeSnapshots = includeSnapshots; - } - } - - @Override - public final ArtifactVersion[] getVersions() { - return getVersions(isIncludeSnapshots()); - } - - @Override - public abstract ArtifactVersion[] getVersions(boolean includeSnapshots); - @Override public final ArtifactVersion[] getVersions(VersionRange versionRange, boolean includeSnapshots) { return getVersions(versionRange, null, includeSnapshots); } - @Override - public final ArtifactVersion[] getVersions(ArtifactVersion lowerBound, ArtifactVersion upperBound) { - return getVersions(lowerBound, upperBound, isIncludeSnapshots()); - } - @Override public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots) { @@ -184,11 +150,6 @@ public final ArtifactVersion[] getVersions( return getVersions(restriction, includeSnapshots); } - @Override - public final ArtifactVersion getNewestVersion(ArtifactVersion lowerBound, ArtifactVersion upperBound) { - return getNewestVersion(lowerBound, upperBound, isIncludeSnapshots()); - } - @Override public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots) { @@ -334,16 +295,6 @@ public final ArtifactVersion[] getAllUpdates( } } - @Override - public final ArtifactVersion getNewestUpdate(Optional updateScope) { - return getNewestUpdate(updateScope, isIncludeSnapshots()); - } - - @Override - public final ArtifactVersion[] getAllUpdates(Optional updateScope) { - return getAllUpdates(updateScope, isIncludeSnapshots()); - } - @Override public final ArtifactVersion getNewestUpdate(Optional updateScope, boolean includeSnapshots) { if (isCurrentVersionDefined()) { @@ -361,13 +312,8 @@ public final ArtifactVersion[] getAllUpdates(Optional updateScope, bool } @Override - public final ArtifactVersion[] getAllUpdates() { - return getAllUpdates((VersionRange) null, isIncludeSnapshots()); - } - - @Override - public final ArtifactVersion[] getAllUpdates(VersionRange versionRange) { - return getAllUpdates(versionRange, isIncludeSnapshots()); + public final ArtifactVersion[] getAllUpdates(boolean includeSnapshots) { + return getAllUpdates((VersionRange) null, includeSnapshots); } @Override @@ -441,13 +387,14 @@ public boolean isVersionInRestriction(Restriction restriction, ArtifactVersion c /** * Returns the latest version newer than the specified current version, and within the specified update scope, - * or null if no such version exists. + * or {@code null} if no such version exists. * @param updateScope the scope of updates to include. + * @param includeSnapshots whether snapshots should be included * @return the newest version after currentVersion within the specified update scope, * or null if no version is available. */ - public final ArtifactVersion getReportNewestUpdate(Optional updateScope) { - return getArtifactVersionStream(updateScope) + public final ArtifactVersion getReportNewestUpdate(Optional updateScope, boolean includeSnapshots) { + return getArtifactVersionStream(updateScope, includeSnapshots) .min(Collections.reverseOrder(getVersionComparator())) .orElse(null); } @@ -455,10 +402,11 @@ public final ArtifactVersion getReportNewestUpdate(Optional updateScope /** * Returns all versions newer than the specified current version, and within the specified update scope. * @param updateScope the scope of updates to include. + * @param includeSnapshots whether snapshots should be included * @return all versions after currentVersion within the specified update scope. */ - public final ArtifactVersion[] getReportUpdates(Optional updateScope) { - TreeSet versions = getArtifactVersionStream(updateScope) + public final ArtifactVersion[] getReportUpdates(Optional updateScope, boolean includeSnapshots) { + TreeSet versions = getArtifactVersionStream(updateScope, includeSnapshots) .collect(Collectors.toCollection(() -> new TreeSet<>(getVersionComparator()))); // filter out intermediate minor versions. if (!verboseDetail) { @@ -493,16 +441,16 @@ public final ArtifactVersion[] getReportUpdates(Optional updateScope) { /** * Returns all versions newer than the specified current version, and within the specified update scope. * @param updateScope the scope of updates to include. + * @param includeSnapshots whether snapshots should be included * @return all versions after currentVersion within the specified update scope. */ - private Stream getArtifactVersionStream(Optional updateScope) { + private Stream getArtifactVersionStream(Optional updateScope, boolean includeSnapshots) { if (isCurrentVersionDefined()) { try { Restriction restriction = restrictionFor(updateScope); - return Arrays.stream(getVersions()) - .filter(candidate -> (isIncludeSnapshots() || !ArtifactUtils.isSnapshot(candidate.toString())) - && isVersionInRestriction(restriction, candidate)); + return Arrays.stream(getVersions(includeSnapshots)) + .filter(candidate -> isVersionInRestriction(restriction, candidate)); } catch (InvalidSegmentException ignored) { ignored.printStackTrace(System.err); } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java index a31ce9171e..586baf6d0a 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java @@ -19,10 +19,12 @@ * under the License. */ +import static org.apache.commons.lang3.StringUtils.compare; + import java.util.List; import java.util.SortedSet; import java.util.TreeSet; - +import java.util.stream.Collectors; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.maven.artifact.Artifact; @@ -32,8 +34,6 @@ import org.apache.maven.artifact.versioning.VersionRange; import org.codehaus.mojo.versions.ordering.VersionComparator; -import static org.apache.commons.lang3.StringUtils.compare; - /** * Holds the results of a search for versions of an artifact. * @@ -91,7 +91,6 @@ public ArtifactVersions(ArtifactVersions other) { versionComparator = other.versionComparator; versions = other.versions; setCurrentVersion(other.getCurrentVersion()); - setIncludeSnapshots(other.isIncludeSnapshots()); } @SuppressWarnings("checkstyle:InnerAssignment") @@ -122,7 +121,7 @@ public boolean equals(Object o) { return new EqualsBuilder() .append(getArtifact(), that.getArtifact()) - .append(getVersions(), that.getVersions()) + .append(getVersions(true), that.getVersions(true)) .append(getVersionComparator(), that.getVersionComparator()) .isEquals(); } @@ -131,7 +130,7 @@ public boolean equals(Object o) { public int hashCode() { return new HashCodeBuilder(17, 37) .append(getArtifact()) - .append(getVersions()) + .append(getVersions(true)) .append(getVersionComparator()) .toHashCode(); } @@ -218,6 +217,24 @@ public ArtifactVersion[] getVersions(boolean includeSnapshots) { .toArray(ArtifactVersion[]::new); } + /** + * Says whether the versions present in the {@link ArtifactVersions} collection are empty. + * If {@code includeSnapshots} is {@code true}, snapshots will not counted, which means that + * the method will only count release versions. + * + * @param includeSnapshots {@code includeSnapshots} is {@code true}, snapshots will not counted, which means that + * * the method will only count release versions. + * @return if {@code includeSnapshots} is {@code true}, returns {@code true} if there are no versions. + * if {@code includeSnapshots} is {@code false}, returns {@code true} if there are no releases. + */ + public boolean isEmpty(boolean includeSnapshots) { + return includeSnapshots + ? versions.isEmpty() + // the below means: the only versions that could be present in the collection + // are snapshots + : versions.stream().map(Object::toString).allMatch(ArtifactUtils::isSnapshot); + } + public VersionComparator getVersionComparator() { return versionComparator; } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersionsCache.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersionsCache.java index e51e91938b..8df888dfa7 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersionsCache.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersionsCache.java @@ -22,24 +22,24 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; -import java.util.function.BiFunction; -import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.lang3.function.TriFunction; +import org.apache.commons.lang3.tuple.Triple; /** - * Utility providing a cached {@link ArtifactVersions#getNewestUpdate(Optional)} API + * Utility providing a cached {@link ArtifactVersions#getNewestUpdate(Optional, boolean)} API */ public class ArtifactVersionsCache { - private BiFunction, ?> cachedFunction; - - private Map>, Object> updateCache = new HashMap<>(); + private TriFunction, Boolean, ?> cachedFunction; + private Map, Boolean>, Object> updateCache = + new HashMap<>(); /** * Constructs a new instance given the concrete function for obtaining the details * * @param cachedFunction reference to the function computing the required information */ - public ArtifactVersionsCache(BiFunction, ?> cachedFunction) { + public ArtifactVersionsCache(TriFunction, Boolean, ?> cachedFunction) { this.cachedFunction = cachedFunction; } @@ -52,11 +52,14 @@ public ArtifactVersionsCache(BiFunction return type of the cached function * @param artifactVersions {@linkplain ArtifactVersions} object referring to the given dependency * @param updateScope update scope + * @param allowSnapshots whether snapshots should be included * @return last retrieved update information */ @SuppressWarnings("unchecked") - public R get(V artifactVersions, Optional updateScope) { + public R get( + V artifactVersions, Optional updateScope, boolean allowSnapshots) { return (R) updateCache.computeIfAbsent( - Pair.of(artifactVersions, updateScope), pair -> cachedFunction.apply(pair.getLeft(), pair.getRight())); + Triple.of(artifactVersions, updateScope, allowSnapshots), + triple -> cachedFunction.apply(triple.getLeft(), triple.getMiddle(), triple.getRight())); } } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java index 0c8fe8d146..edee268bde 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java @@ -422,23 +422,16 @@ public ArtifactVersion createArtifactVersion(String version) { return new DefaultArtifactVersion(version); } - /** - * Returns a map of all possible updates per dependency. The lookup is done in parallel using - * {@code LOOKUP_PARALLEL_THREADS} threads. - * - * @param dependencies The set of {@link Dependency} instances to look up. - * @param usePluginRepositories Search the plugin repositories. - * @return map containing the ArtifactVersions object per dependency - */ @Override public Map lookupDependenciesUpdates( - Set dependencies, boolean usePluginRepositories) throws VersionRetrievalException { + Set dependencies, boolean usePluginRepositories, boolean allowSnapshots) + throws VersionRetrievalException { ExecutorService executor = Executors.newFixedThreadPool(LOOKUP_PARALLEL_THREADS); try { Map dependencyUpdates = new TreeMap<>(DependencyComparator.INSTANCE); List>> futures = dependencies.stream() .map(dependency -> executor.submit(() -> new ImmutablePair<>( - dependency, lookupDependencyUpdates(dependency, usePluginRepositories)))) + dependency, lookupDependencyUpdates(dependency, usePluginRepositories, allowSnapshots)))) .collect(Collectors.toList()); for (Future> details : futures) { Pair pair = details.get(); @@ -455,13 +448,14 @@ dependency, lookupDependencyUpdates(dependency, usePluginRepositories)))) } @Override - public ArtifactVersions lookupDependencyUpdates(Dependency dependency, boolean usePluginRepositories) + public ArtifactVersions lookupDependencyUpdates( + Dependency dependency, boolean usePluginRepositories, boolean allowSnapshots) throws VersionRetrievalException { ArtifactVersions allVersions = lookupArtifactVersions(createDependencyArtifact(dependency), usePluginRepositories); return new ArtifactVersions( allVersions.getArtifact(), - Arrays.stream(allVersions.getAllUpdates()).collect(Collectors.toList()), + Arrays.stream(allVersions.getAllUpdates(allowSnapshots)).collect(Collectors.toList()), allVersions.getVersionComparator()); } @@ -498,13 +492,13 @@ public PluginUpdatesDetails lookupPluginUpdates(Plugin plugin, boolean allowSnap pluginDependencies.addAll(plugin.getDependencies()); } Map pluginDependencyDetails = - lookupDependenciesUpdates(pluginDependencies, false); + lookupDependenciesUpdates(pluginDependencies, false, allowSnapshots); ArtifactVersions allVersions = lookupArtifactVersions( createPluginArtifact(plugin.getGroupId(), plugin.getArtifactId(), version), true); ArtifactVersions updatedVersions = new ArtifactVersions( allVersions.getArtifact(), - Arrays.stream(allVersions.getAllUpdates()).collect(Collectors.toList()), + Arrays.stream(allVersions.getAllUpdates(allowSnapshots)).collect(Collectors.toList()), allVersions.getVersionComparator()); return new PluginUpdatesDetails(updatedVersions, pluginDependencyDetails, allowSnapshots); } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java index 245ebdf582..79b369bf68 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java @@ -67,10 +67,6 @@ public interface VersionDetails { */ void setCurrentVersion(String currentVersion); - boolean isIncludeSnapshots(); - - void setIncludeSnapshots(boolean includeSnapshots); - /** * Retrieves the current version. * @@ -87,14 +83,6 @@ public interface VersionDetails { */ VersionComparator getVersionComparator(); - /** - * Returns all the available versions in increasing order. - * - * @return all the available versions in increasing order. - * @since 1.0-alpha-3 - */ - ArtifactVersion[] getVersions(); - /** * Returns all available versions in increasing order. * @@ -114,16 +102,6 @@ public interface VersionDetails { */ ArtifactVersion[] getVersions(VersionRange versionRange, boolean includeSnapshots); - /** - * Returns all available versions within the specified bounds. - * - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. - * @return all available versions within the specified version range. - * @since 1.0-beta-1 - */ - ArtifactVersion[] getVersions(ArtifactVersion lowerBound, ArtifactVersion upperBound); - /** * Returns all available versions within the specified bounds. * @@ -170,17 +148,6 @@ public interface VersionDetails { ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction, boolean includeSnapshots, boolean allowDowngrade); - /** - * Returns the latest version newer than the specified lowerBound, but less than the specified upper bound or - * null if no such version exists. - * - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. - * @return the latest version between lowerBound and upperBound or null if no version is available. - * @since 1.0-alpha-3 - */ - ArtifactVersion getNewestVersion(ArtifactVersion lowerBound, ArtifactVersion upperBound); - /** * Returns the latest version newer than the specified lowerBound, but less than the specified upper bound or * null if no such version exists. @@ -317,28 +284,6 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Optional updateScope, boolean includeSnapshots) throws InvalidSegmentException; - /** - * Returns the newest version newer than the specified current version, but within the specified update scope or - * null if no such version exists. - * - * @param updateScope the update scope to include. - * @return the newest version after currentVersion within the specified update scope or null if no - * version is available. - * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments - * @since 1.0-beta-1 - */ - ArtifactVersion getNewestUpdate(Optional updateScope) throws InvalidSegmentException; - - /** - * Returns the all versions newer than the specified current version, but within the specified update scope. - * - * @param updateScope the update scope to include. - * @return the all versions after currentVersion within the specified update scope. - * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments - * @since 1.0-beta-1 - */ - ArtifactVersion[] getAllUpdates(Optional updateScope) throws InvalidSegmentException; - /** * Returns the newest version newer than the specified current version, but within the specified update scope or * null if no such version exists. @@ -357,7 +302,7 @@ ArtifactVersion getNewestUpdate(Optional updateScope, boolean includeSn * Returns the all versions newer than the specified current version, but within the specified update scope. * * @param updateScope the update scope to include. - * @param includeSnapshots true if snapshots are to be included. + * @param includeSnapshots {@code true} if snapshots are to be included. * @return the all versions after currentVersion within the specified update scope. * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 @@ -368,19 +313,11 @@ ArtifactVersion[] getAllUpdates(Optional updateScope, boolean includeSn /** * Returns the all versions newer than the specified current version * + * @param includeSnapshots {@code true} if snapshots are to be included. * @return the all versions after currentVersion * @since 2.13.0 */ - ArtifactVersion[] getAllUpdates(); - - /** - * Returns the all versions newer than the specified current version, but within the specified update scope. - * - * @param versionRange the version range to include. - * @return the all versions after currentVersion within the specified update scope. - * @since 1.0-beta-1 - */ - ArtifactVersion[] getAllUpdates(VersionRange versionRange); + ArtifactVersion[] getAllUpdates(boolean includeSnapshots); /** * Returns the all versions newer than the specified current version, but within the specified update scope. diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java index d76fe9d7b7..d10b4e0d92 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java @@ -149,6 +149,7 @@ Artifact createDependencyArtifact( /** * Looks up the versions of the specified artifact that are available in either the local repository, or the * appropriate remote repositories. + * The resulting {@link ArtifactVersions} instance will contain all versions, including snapshots. * * @param artifact The artifact to look for versions of. * @param usePluginRepositories true will consult the pluginRepositories, while false will @@ -163,6 +164,7 @@ ArtifactVersions lookupArtifactVersions(Artifact artifact, boolean usePluginRepo /** * Looks up the versions of the specified artifact that are available in either the local repository, or the * appropriate remote repositories. + * The resulting {@link ArtifactVersions} instance will contain all versions, including snapshots. * * @param artifact The artifact to look for versions of. * @param versionRange versionRange to restrict the search @@ -176,27 +178,30 @@ ArtifactVersions lookupArtifactVersions(Artifact artifact, VersionRange versionR throws VersionRetrievalException; /** - * Looks up the updates for a set of dependencies. + * Returns a map of all possible updates per dependency. The lookup is done in parallel using + * {@code LOOKUP_PARALLEL_THREADS} threads. * * @param dependencies The set of {@link Dependency} instances to look up. * @param usePluginRepositories Search the plugin repositories. - * @return A map, keyed by dependency, with values of type {@link org.codehaus.mojo.versions.api.ArtifactVersions}. - * @throws VersionRetrievalException thrown if version resolution fails - * @since 1.0-beta-1 + * @param allowSnapshots whether snapshots should be included + * @return map containing the ArtifactVersions object per dependency */ Map lookupDependenciesUpdates( - Set dependencies, boolean usePluginRepositories) throws VersionRetrievalException; + Set dependencies, boolean usePluginRepositories, boolean allowSnapshots) + throws VersionRetrievalException; /** * Creates an {@link org.codehaus.mojo.versions.api.ArtifactVersions} instance from a dependency. * * @param dependency The dependency. * @param usePluginRepositories Search the plugin repositories. + * @param allowSnapshots whether snapshots should be included * @return The details of updates to the dependency. * @throws VersionRetrievalException thrown if version resolution fails * @since 1.0-beta-1 */ - ArtifactVersions lookupDependencyUpdates(Dependency dependency, boolean usePluginRepositories) + ArtifactVersions lookupDependencyUpdates( + Dependency dependency, boolean usePluginRepositories, boolean allowSnapshots) throws VersionRetrievalException; /** diff --git a/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java b/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java index 9809bfb7bd..0bb5c29446 100644 --- a/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java +++ b/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java @@ -64,21 +64,22 @@ public void test4DigitVersions() throws Exception { assertEquals("artifact", instance.getArtifactId()); assertEquals("group", instance.getGroupId()); assertThat( - instance.getVersions(), + instance.getVersions(true), arrayContaining(versions("1.0.0.1", "1.0.0.2", "1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertThat( - instance.getVersions(new DefaultArtifactVersion("1.1"), null), + instance.getVersions(new DefaultArtifactVersion("1.1"), null, true), arrayContaining(versions("1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertThat( - instance.getVersions(new DefaultArtifactVersion("1.0.0.2"), null), + instance.getVersions(new DefaultArtifactVersion("1.0.0.2"), null, true), // Matchers.arrayContaining(versions("1.1.1", "2.121.2.1", "2.100.0.1", "3.1.0.1"))); arrayContaining(versions("1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertEquals( new DefaultArtifactVersion("2.121.2.1"), - instance.getNewestVersion(new DefaultArtifactVersion("1.0"), new DefaultArtifactVersion("3.0"))); - assertNull(instance.getNewestVersion(new DefaultArtifactVersion("1.1.1"), new DefaultArtifactVersion("2.0"))); + instance.getNewestVersion(new DefaultArtifactVersion("1.0"), new DefaultArtifactVersion("3.0"), true)); + assertNull(instance.getNewestVersion( + new DefaultArtifactVersion("1.1.1"), new DefaultArtifactVersion("2.0"), true)); } @Test @@ -96,13 +97,15 @@ public void testSmokes() throws Exception { new ArtifactVersions(artifact, Arrays.asList(versions), new MavenVersionComparator()); assertEquals("artifact", instance.getArtifactId()); assertEquals("group", instance.getGroupId()); - assertArrayEquals(versions("1.0", "1.0.1", "1.1", "3.0"), instance.getVersions()); - assertArrayEquals(versions("3.0"), instance.getVersions(new DefaultArtifactVersion("1.1"), null)); - assertArrayEquals(versions("1.1", "3.0"), instance.getVersions(new DefaultArtifactVersion("1.0.1"), null)); + assertArrayEquals(versions("1.0", "1.0.1", "1.1", "3.0"), instance.getVersions(true)); + assertArrayEquals(versions("3.0"), instance.getVersions(new DefaultArtifactVersion("1.1"), null, true)); + assertArrayEquals( + versions("1.1", "3.0"), instance.getVersions(new DefaultArtifactVersion("1.0.1"), null, true)); assertEquals( new DefaultArtifactVersion("1.1"), - instance.getNewestVersion(new DefaultArtifactVersion("1.0"), new DefaultArtifactVersion("3.0"))); - assertNull(instance.getNewestVersion(new DefaultArtifactVersion("1.1"), new DefaultArtifactVersion("3.0"))); + instance.getNewestVersion(new DefaultArtifactVersion("1.0"), new DefaultArtifactVersion("3.0"), true)); + assertNull( + instance.getNewestVersion(new DefaultArtifactVersion("1.1"), new DefaultArtifactVersion("3.0"), true)); } private ArtifactVersion[] versions(String... versions) { @@ -143,8 +146,8 @@ public void testReportLabels() { Arrays.asList(versions), new MavenVersionComparator()); - assertThat(instance.getNewestUpdate(of(SUBINCREMENTAL)).toString(), is("1.1.0-2")); - assertThat(instance.getNewestUpdate(of(INCREMENTAL)).toString(), is("1.1.3")); + assertThat(instance.getNewestUpdate(of(SUBINCREMENTAL), false).toString(), is("1.1.0-2")); + assertThat(instance.getNewestUpdate(of(INCREMENTAL), false).toString(), is("1.1.3")); } @Test diff --git a/versions-enforcer/src/it-repo/dummy-api-2.2-SNAPSHOT.pom b/versions-enforcer/src/it-repo/dummy-api-2.2-SNAPSHOT.pom new file mode 100644 index 0000000000..ca49213420 --- /dev/null +++ b/versions-enforcer/src/it-repo/dummy-api-2.2-SNAPSHOT.pom @@ -0,0 +1,10 @@ + + 4.0.0 + + localhost + dummy-api + 2.2-SNAPSHOT + jar + + diff --git a/versions-enforcer/src/it/it-max-dependency-updates-005/invoker.properties b/versions-enforcer/src/it/it-max-dependency-updates-005/invoker.properties new file mode 100644 index 0000000000..cea2f9d436 --- /dev/null +++ b/versions-enforcer/src/it/it-max-dependency-updates-005/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = enforcer:enforce +invoker.buildResult = succes diff --git a/versions-enforcer/src/it/it-max-dependency-updates-005/pom.xml b/versions-enforcer/src/it/it-max-dependency-updates-005/pom.xml new file mode 100644 index 0000000000..63033a1c4a --- /dev/null +++ b/versions-enforcer/src/it/it-max-dependency-updates-005/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + localhost + it-max-dependency-upgrades + 1.0 + pom + + + + localhost + dummy-api + 2.1 + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + @maven-enforcer-plugin.version@ + + enforce + + + + + 0 + + + + + + org.codehaus.mojo.versions + versions-enforcer + @project.version@ + + + + + + + diff --git a/versions-enforcer/src/it/it-max-dependency-updates-006/invoker.properties b/versions-enforcer/src/it/it-max-dependency-updates-006/invoker.properties new file mode 100644 index 0000000000..38b185e579 --- /dev/null +++ b/versions-enforcer/src/it/it-max-dependency-updates-006/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = enforcer:enforce +invoker.buildResult = failure diff --git a/versions-enforcer/src/it/it-max-dependency-updates-006/pom.xml b/versions-enforcer/src/it/it-max-dependency-updates-006/pom.xml new file mode 100644 index 0000000000..25752d8e4c --- /dev/null +++ b/versions-enforcer/src/it/it-max-dependency-updates-006/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + localhost + it-max-dependency-upgrades + 1.0 + pom + + + + localhost + dummy-api + 2.1 + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + @maven-enforcer-plugin.version@ + + enforce + + + + + 0 + true + + + + + + org.codehaus.mojo.versions + versions-enforcer + @project.version@ + + + + + + + diff --git a/versions-enforcer/src/main/java/org/apache/maven/plugins/enforcer/MaxDependencyUpdates.java b/versions-enforcer/src/main/java/org/apache/maven/plugins/enforcer/MaxDependencyUpdates.java index 3316460cb3..0bc4f3c88a 100644 --- a/versions-enforcer/src/main/java/org/apache/maven/plugins/enforcer/MaxDependencyUpdates.java +++ b/versions-enforcer/src/main/java/org/apache/maven/plugins/enforcer/MaxDependencyUpdates.java @@ -243,6 +243,13 @@ public class MaxDependencyUpdates implements EnforcerRule2 { */ protected RuleSet ruleSet; + /** + * Whether snapshots should be counted as updates. Default is {@code false}. + * + * @since 2.15.0 + */ + protected boolean allowSnapshots; + /** * Retrieves the maven project from metadata * @param ruleHelper EnforcerRuleHelper object @@ -349,14 +356,14 @@ public void execute(EnforcerRuleHelper ruleHelper) throws EnforcerRuleException ? of(SUBINCREMENTAL) : ignoreIncrementalUpdates ? of(INCREMENTAL) : ignoreMinorUpdates ? of(MINOR) : empty(); List upgradable = - versionsHelper.lookupDependenciesUpdates(dependencies, false).values().stream() + versionsHelper.lookupDependenciesUpdates(dependencies, false, allowSnapshots).values().stream() .filter(v -> v.getVersions(v.restrictionForIgnoreScope(ignoredSegment), true).length > 0) .collect(Collectors.toList()); if (upgradable.size() > maxUpdates) { throw new EnforcerRuleException("More than " + maxUpdates + " upgradable artifacts detected: " + upgradable.stream() .map(av -> av.getArtifact() + " -> [" - + Arrays.stream(av.getVersions()) + + Arrays.stream(av.getVersions(allowSnapshots)) .map(ArtifactVersion::toString) .collect(Collectors.joining(", ")) + "]") diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractDependencyUpdatesReportMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractDependencyUpdatesReportMojo.java index ac9b15b083..b7adcb82ec 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractDependencyUpdatesReportMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractDependencyUpdatesReportMojo.java @@ -146,27 +146,27 @@ protected void doGenerateReport(Locale locale, Sink sink) throws MavenReportExce try { Map dependencyUpdates = - getHelper().lookupDependenciesUpdates(dependencies, false); + getHelper().lookupDependenciesUpdates(dependencies, false, allowSnapshots); Map dependencyManagementUpdates = processDependencyManagement - ? getHelper().lookupDependenciesUpdates(dependencyManagement, false) + ? getHelper().lookupDependenciesUpdates(dependencyManagement, false, allowSnapshots) : emptyMap(); if (onlyUpgradable) { - dependencyUpdates = filter(dependencyUpdates, e -> e.getVersions().length > 0); - dependencyManagementUpdates = filter(dependencyManagementUpdates, e -> e.getVersions().length > 0); + dependencyUpdates = filter(dependencyUpdates, e -> !e.isEmpty(allowSnapshots)); + dependencyManagementUpdates = filter(dependencyManagementUpdates, e -> !e.isEmpty(allowSnapshots)); } if (getLog().isDebugEnabled()) { getLog().debug("Dependency versions:"); dependencyUpdates.forEach((key, value) -> getLog().debug(key.toString() + ": " - + Arrays.stream(value.getVersions()) + + Arrays.stream(value.getVersions(true /* already filtered */)) .map(ArtifactVersion::toString) .collect(Collectors.joining(", ")))); getLog().debug("Dependency management versions:"); dependencyManagementUpdates.forEach((key, value) -> getLog().debug(key.toString() + ": " - + Arrays.stream(value.getVersions()) + + Arrays.stream(value.getVersions(true /* already filtered */)) .map(ArtifactVersion::toString) .collect(Collectors.joining(", ")))); } @@ -275,7 +275,7 @@ private void renderReport(Locale locale, Sink sink, DependencyUpdatesModel model for (String format : formats) { if ("html".equals(format)) { rendererFactory - .createReportRenderer(getOutputName(), sink, locale, model) + .createReportRenderer(getOutputName(), sink, locale, model, allowSnapshots) .render(); } else if ("xml".equals(format)) { Path outputDir = Paths.get(getProject().getBuild().getDirectory()); @@ -287,7 +287,7 @@ private void renderReport(Locale locale, Sink sink, DependencyUpdatesModel model } } Path outputFile = outputDir.resolve(getOutputName() + ".xml"); - new DependencyUpdatesXmlReportRenderer(model, outputFile).render(); + new DependencyUpdatesXmlReportRenderer(model, outputFile, allowSnapshots).render(); } } } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPluginUpdatesReportMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPluginUpdatesReportMojo.java index f96ec322cb..2682fc7efd 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPluginUpdatesReportMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPluginUpdatesReportMojo.java @@ -131,8 +131,8 @@ protected void doGenerateReport(Locale locale, Sink sink) throws MavenReportExce getHelper().lookupPluginsUpdates(pluginManagement, getAllowSnapshots()); if (onlyUpgradable) { - pluginUpdates = filter(pluginUpdates, p -> p.getVersions().length > 0); - pluginManagementUpdates = filter(pluginManagementUpdates, p -> p.getVersions().length > 0); + pluginUpdates = filter(pluginUpdates, p -> !p.isEmpty(allowSnapshots)); + pluginManagementUpdates = filter(pluginManagementUpdates, p -> !p.isEmpty(allowSnapshots)); } renderReport(locale, sink, new PluginUpdatesModel(pluginUpdates, pluginManagementUpdates)); @@ -188,7 +188,7 @@ private void renderReport(Locale locale, Sink sink, PluginUpdatesModel model) th for (String format : formats) { if ("html".equals(format)) { rendererFactory - .createReportRenderer(getOutputName(), sink, locale, model) + .createReportRenderer(getOutputName(), sink, locale, model, allowSnapshots) .render(); } else if ("xml".equals(format)) { Path outputDir = Paths.get(getProject().getBuild().getDirectory()); @@ -200,7 +200,7 @@ private void renderReport(Locale locale, Sink sink, PluginUpdatesModel model) th } } Path outputFile = outputDir.resolve(getOutputName() + ".xml"); - new PluginUpdatesXmlReportRenderer(model, outputFile).render(); + new PluginUpdatesXmlReportRenderer(model, outputFile, allowSnapshots).render(); } } } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPropertyUpdatesReportMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPropertyUpdatesReportMojo.java index e2ab84276a..38985fb98e 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPropertyUpdatesReportMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractPropertyUpdatesReportMojo.java @@ -153,7 +153,7 @@ private void renderReport(Locale locale, Sink sink, PropertyUpdatesModel propert for (String format : this.formats) { if ("html".equals(format)) { this.rendererFactory - .createReportRenderer(getOutputName(), sink, locale, propertyUpdatesModel) + .createReportRenderer(getOutputName(), sink, locale, propertyUpdatesModel, allowSnapshots) .render(); } else if ("xml".equals(format)) { Path outputDir = Paths.get(getProject().getBuild().getDirectory()); @@ -165,7 +165,7 @@ private void renderReport(Locale locale, Sink sink, PropertyUpdatesModel propert } } Path outputFile = outputDir.resolve(getOutputName() + ".xml"); - new PropertyUpdatesXmlReportRenderer(propertyUpdatesModel, outputFile).render(); + new PropertyUpdatesXmlReportRenderer(propertyUpdatesModel, outputFile, allowSnapshots).render(); } } } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java index 5154d547af..6a2a93b78c 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java @@ -421,7 +421,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { "Dependecy Management", getLog()); - logUpdates(getHelper().lookupDependenciesUpdates(dependencyManagement, false), "Dependency Management"); + logUpdates( + getHelper().lookupDependenciesUpdates(dependencyManagement, false, allowSnapshots), + "Dependency Management"); } if (isProcessingDependencies()) { Set finalDependencyManagement = dependencyManagement; @@ -440,7 +442,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { dependencyExcludes, "Dependencies", getLog()), - false), + false, + allowSnapshots), "Dependencies"); } if (isProcessPluginDependenciesInDependencyManagement()) { @@ -453,7 +456,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { pluginManagementDependencyExcludes, "Plugin Management Dependencies", getLog()), - false), + false, + allowSnapshots), "pluginManagement of plugins"); } if (isProcessingPluginDependencies()) { @@ -466,7 +470,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { pluginDependencyExcludes, "Plugin Dependencies", getLog()), - false), + false, + allowSnapshots), "Plugin Dependencies"); } } catch (VersionRetrievalException e) { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java index fcef804c21..6e46c6f2b7 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java @@ -95,7 +95,6 @@ public boolean canGenerateReport() { protected void doGenerateReport(Locale locale, Sink sink) throws MavenReportException { try { ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions(project.getParentArtifact(), false); - artifactVersions.setIncludeSnapshots(allowSnapshots); rendererFactory .createReportRenderer( getOutputName(), @@ -118,7 +117,8 @@ protected void doGenerateReport(Locale locale, Sink sink) throws MavenReportExce .getArtifact() .getClassifier()) .build(), - artifactVersions)) + artifactVersions), + allowSnapshots) .render(); } catch (VersionRetrievalException e) { throw new MavenReportException(e.getMessage(), e); diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java index 632fec930a..6d849e7c28 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java @@ -35,7 +35,7 @@ import org.codehaus.mojo.versions.api.AbstractVersionDetails; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.ArtifactVersionsCache; -import org.codehaus.mojo.versions.api.ReportRenderer; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; import org.codehaus.plexus.i18n.I18N; import static java.util.Optional.empty; @@ -69,8 +69,9 @@ public abstract class AbstractVersionsReportRenderer extends VersionsReportRe protected final SinkEventAttributes headerAttributes = new SinkEventAttributeSet(SinkEventAttributes.WIDTH, "30%"); - protected AbstractVersionsReportRenderer(I18N i18n, Sink sink, Locale locale, String bundleName, T model) { - super(sink, i18n, locale, bundleName); + protected AbstractVersionsReportRenderer( + I18N i18n, Sink sink, Locale locale, String bundleName, T model, boolean allowSnapshots) { + super(sink, i18n, locale, bundleName, allowSnapshots); this.model = model; } @@ -214,7 +215,7 @@ protected void renderSummaryTableHeader(boolean hasScope, boolean hasType) { protected void renderSummaryTableRow(Dependency artifact, ArtifactVersions details, boolean includeScope) { details.setCurrentVersion(artifact.getVersion()); - ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty()); + ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty(), isAllowSnapshots()); boolean upToDate = allUpdates == null || allUpdates.length == 0; sink.tableRow(); @@ -238,14 +239,14 @@ protected void renderSummaryTableRow(Dependency artifact, ArtifactVersions detai * @param details the artifact for which to render the newest versions. */ protected void renderNewestVersions(AbstractVersionDetails details) { - renderBoldCell(newestUpdateCache.get(details, of(SUBINCREMENTAL))); - renderBoldCell(newestUpdateCache.get(details, of(INCREMENTAL))); - renderBoldCell(newestUpdateCache.get(details, of(MINOR))); - renderBoldCell(newestUpdateCache.get(details, of(MAJOR))); + renderBoldCell(newestUpdateCache.get(details, of(SUBINCREMENTAL), isAllowSnapshots())); + renderBoldCell(newestUpdateCache.get(details, of(INCREMENTAL), isAllowSnapshots())); + renderBoldCell(newestUpdateCache.get(details, of(MINOR), isAllowSnapshots())); + renderBoldCell(newestUpdateCache.get(details, of(MAJOR), isAllowSnapshots())); } protected void renderDependencyDetailTable(Dependency artifact, ArtifactVersions details, boolean includeScope) { - ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty()); + ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty(), isAllowSnapshots()); boolean upToDate = allUpdates == null || allUpdates.length == 0; sink.table(); @@ -305,19 +306,19 @@ protected void renderTwoCellsRow(String textKey, Runnable runnable) { * @param details the artifact for which to render the status. */ protected void renderStatus(AbstractVersionDetails details) { - if (newestUpdateCache.get(details, of(SUBINCREMENTAL)) != null) { + if (newestUpdateCache.get(details, of(SUBINCREMENTAL), isAllowSnapshots()) != null) { renderWarningIcon(); sink.nonBreakingSpace(); sink.text(getText("report.otherUpdatesAvailable")); - } else if (newestUpdateCache.get(details, of(INCREMENTAL)) != null) { + } else if (newestUpdateCache.get(details, of(INCREMENTAL), isAllowSnapshots()) != null) { renderWarningIcon(); sink.nonBreakingSpace(); sink.text(getText("report.incrementalUpdatesAvailable")); - } else if (newestUpdateCache.get(details, of(MINOR)) != null) { + } else if (newestUpdateCache.get(details, of(MINOR), isAllowSnapshots()) != null) { renderWarningIcon(); sink.nonBreakingSpace(); sink.text(getText("report.minorUpdatesAvailable")); - } else if (newestUpdateCache.get(details, of(MAJOR)) != null) { + } else if (newestUpdateCache.get(details, of(MAJOR), isAllowSnapshots()) != null) { renderWarningIcon(); sink.nonBreakingSpace(); sink.text(getText("report.majorUpdatesAvailable")); @@ -398,19 +399,19 @@ protected void renderVersions(ArtifactVersion[] allUpdates, AbstractVersionDetai */ protected String getLabel(ArtifactVersion version, AbstractVersionDetails details) { - if (equals(version, newestUpdateCache.get(details, of(SUBINCREMENTAL)))) { + if (equals(version, newestUpdateCache.get(details, of(SUBINCREMENTAL), isAllowSnapshots()))) { return getText("report.latestSubIncremental"); } - if (equals(version, newestUpdateCache.get(details, of(INCREMENTAL)))) { + if (equals(version, newestUpdateCache.get(details, of(INCREMENTAL), isAllowSnapshots()))) { return getText("report.latestIncremental"); } - if (equals(version, newestUpdateCache.get(details, of(MINOR)))) { + if (equals(version, newestUpdateCache.get(details, of(MINOR), isAllowSnapshots()))) { return getText("report.latestMinor"); } - if (equals(version, newestUpdateCache.get(details, of(MAJOR)))) { + if (equals(version, newestUpdateCache.get(details, of(MAJOR), isAllowSnapshots()))) { return getText("report.latestMajor"); } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java index 4e81d32161..7233e3371c 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java @@ -35,8 +35,9 @@ */ public class DependencyUpdatesReportRenderer extends AbstractVersionsReportRenderer { - public DependencyUpdatesReportRenderer(I18N i18n, Sink sink, Locale locale, String bundleName, K model) { - super(i18n, sink, locale, bundleName, model); + public DependencyUpdatesReportRenderer( + I18N i18n, Sink sink, Locale locale, String bundleName, K model, boolean allowSnapshots) { + super(i18n, sink, locale, bundleName, model, allowSnapshots); } /** @@ -78,7 +79,7 @@ protected void renderTable(String titleKey, Map co @Override protected OverviewStats computeOverviewStats() { - return OverviewStats.fromUpdates(model.getAllUpdates().values(), newestUpdateCache); + return OverviewStats.fromUpdates(model.getAllUpdates().values(), newestUpdateCache, isAllowSnapshots()); } protected void renderDependencyDetail(Dependency artifact, ArtifactVersions details) { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java index cb30cc74a8..1553ccdfaa 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java @@ -57,22 +57,23 @@ public class OverviewStats { * @param updates collection of all version updates, typically from * {@linkplain org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel#getAllUpdates()} * @param cache if not null, cache to retrieve the version information, initialised with - * the {@link ArtifactVersions#getNewestUpdate(Optional)} update information + * the {@link ArtifactVersions#getNewestUpdate(Optional, boolean)} update information * @param subclass of {@linkplain OverviewStats} * @param subclass of {@linkplain ArtifactVersions} + * @param allowSnapshots whether snapshots should be included * @return instance of the {@linkplain OverviewStats} */ public static T fromUpdates( - Collection updates, ArtifactVersionsCache cache) { + Collection updates, ArtifactVersionsCache cache, boolean allowSnapshots) { OverviewStats stats = new OverviewStats(); updates.forEach(details -> { - if (getNewestUpdate(cache, details, of(SUBINCREMENTAL)) != null) { + if (getNewestUpdate(cache, details, of(SUBINCREMENTAL), allowSnapshots) != null) { stats.incrementAny(); - } else if (getNewestUpdate(cache, details, of(INCREMENTAL)) != null) { + } else if (getNewestUpdate(cache, details, of(INCREMENTAL), allowSnapshots) != null) { stats.incrementIncremental(); - } else if (getNewestUpdate(cache, details, of(MINOR)) != null) { + } else if (getNewestUpdate(cache, details, of(MINOR), allowSnapshots) != null) { stats.incrementMinor(); - } else if (getNewestUpdate(cache, details, of(MAJOR)) != null) { + } else if (getNewestUpdate(cache, details, of(MAJOR), allowSnapshots) != null) { stats.incrementMajor(); } else { stats.incrementUpToDate(); @@ -82,8 +83,10 @@ public static T from } protected static ArtifactVersion getNewestUpdate( - ArtifactVersionsCache cache, V details, Optional segment) { - return cache != null ? cache.get(details, segment) : details.getNewestUpdate(segment); + ArtifactVersionsCache cache, V details, Optional segment, boolean allowSnapshots) { + return cache != null + ? cache.get(details, segment, allowSnapshots) + : details.getNewestUpdate(segment, allowSnapshots); } public int getMajor() { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ParentUpdatesReportRenderer.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ParentUpdatesReportRenderer.java index 003c23e468..2fd21c554f 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ParentUpdatesReportRenderer.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ParentUpdatesReportRenderer.java @@ -30,8 +30,8 @@ */ public class ParentUpdatesReportRenderer extends DependencyUpdatesReportRenderer { public ParentUpdatesReportRenderer( - I18N i18n, Sink sink, Locale locale, String bundleName, ParentUpdatesModel model) { - super(i18n, sink, locale, bundleName, model); + I18N i18n, Sink sink, Locale locale, String bundleName, ParentUpdatesModel model, boolean allowSnapshots) { + super(i18n, sink, locale, bundleName, model, allowSnapshots); } @Override diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java index 57186cb688..5fa0e56326 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java @@ -53,22 +53,23 @@ public void incrementDependencies() { * * @param updates collection of all version updates, typically from {@linkplain PluginUpdatesModel#getAllUpdates()} * @param cache if not null, cache to retrieve the version information, initialised with - * the {@link ArtifactVersions#getNewestUpdate(Optional)} update information + * the {@link ArtifactVersions#getNewestUpdate(Optional, boolean)} update information * @param always equal to {@linkplain PluginOverviewStats} * @param always equal to {@linkplain PluginUpdatesDetails} + * @param allowSnapshots whether snapshots should be included * @return instance of the {@linkplain PluginOverviewStats}, initialised with the update information */ public static T fromUpdates( - Collection updates, ArtifactVersionsCache cache) { + Collection updates, ArtifactVersionsCache cache, boolean allowSnapshots) { PluginOverviewStats stats = new PluginOverviewStats(); updates.forEach(details -> { - if (getNewestUpdate(cache, details, of(SUBINCREMENTAL)) != null) { + if (getNewestUpdate(cache, details, of(SUBINCREMENTAL), allowSnapshots) != null) { stats.incrementAny(); - } else if (getNewestUpdate(cache, details, of(INCREMENTAL)) != null) { + } else if (getNewestUpdate(cache, details, of(INCREMENTAL), allowSnapshots) != null) { stats.incrementIncremental(); - } else if (getNewestUpdate(cache, details, of(MINOR)) != null) { + } else if (getNewestUpdate(cache, details, of(MINOR), allowSnapshots) != null) { stats.incrementMinor(); - } else if (getNewestUpdate(cache, details, of(MAJOR)) != null) { + } else if (getNewestUpdate(cache, details, of(MAJOR), allowSnapshots) != null) { stats.incrementMajor(); } else { stats.incrementUpToDate(); diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java index bf5efe6164..1e1f9dbed2 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java @@ -40,8 +40,8 @@ public class PluginUpdatesReportRenderer extends AbstractVersionsReportRenderer { public PluginUpdatesReportRenderer( - I18N i18n, Sink sink, Locale locale, String bundleName, PluginUpdatesModel model) { - super(i18n, sink, locale, bundleName, model); + I18N i18n, Sink sink, Locale locale, String bundleName, PluginUpdatesModel model, boolean allowSnapshots) { + super(i18n, sink, locale, bundleName, model, allowSnapshots); } @Override @@ -112,7 +112,7 @@ protected void renderSummaryTable(Map contents */ @Override protected PluginOverviewStats computeOverviewStats() { - return PluginOverviewStats.fromUpdates(model.getAllUpdates().values(), newestUpdateCache); + return PluginOverviewStats.fromUpdates(model.getAllUpdates().values(), newestUpdateCache, isAllowSnapshots()); } @Override @@ -178,7 +178,7 @@ private void renderPluginDetail(Dependency artifact, PluginUpdatesDetails detail private void renderPluginDetailTable(PluginUpdatesDetails details) { // warning: using caches here might break plugin report - ArtifactVersion[] allUpdates = details.getAllUpdates(empty()); + ArtifactVersion[] allUpdates = details.getAllUpdates(empty(), isAllowSnapshots()); boolean upToDate = allUpdates == null || allUpdates.length == 0; sink.table(); diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java index 88f5ad05c4..2913fcfb93 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java @@ -38,8 +38,13 @@ */ public class PropertyUpdatesReportRenderer extends AbstractVersionsReportRenderer { public PropertyUpdatesReportRenderer( - I18N i18n, Sink sink, Locale locale, String bundleName, PropertyUpdatesModel model) { - super(i18n, sink, locale, bundleName, model); + I18N i18n, + Sink sink, + Locale locale, + String bundleName, + PropertyUpdatesModel model, + boolean allowSnapshots) { + super(i18n, sink, locale, bundleName, model, allowSnapshots); } @Override @@ -91,7 +96,7 @@ protected void renderSummaryTable(Map contents) { } private void renderPropertySummaryTableRow(Property property, PropertyVersions details) { - ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty()); + ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty(), isAllowSnapshots()); boolean upToDate = allUpdates == null || allUpdates.length == 0; sink.tableRow(); @@ -107,7 +112,7 @@ private void renderPropertySummaryTableRow(Property property, PropertyVersions d } protected void renderPropertyDetailTable(Property property, PropertyVersions details) { - ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty()); + ArtifactVersion[] allUpdates = allUpdatesCache.get(details, empty(), isAllowSnapshots()); boolean upToDate = allUpdates == null || allUpdates.length == 0; sink.table(); @@ -163,7 +168,7 @@ protected void renderSummaryTableHeader(boolean hasScope, boolean hasType) { @Override protected OverviewStats computeOverviewStats() { - return OverviewStats.fromUpdates(model.getAllUpdates().values(), newestUpdateCache); + return OverviewStats.fromUpdates(model.getAllUpdates().values(), newestUpdateCache, isAllowSnapshots()); } private void renderPropertyDetail(Property property, PropertyVersions details) { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java index 56dd9d33cc..52224080e6 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java @@ -22,7 +22,7 @@ import java.util.Locale; import org.apache.maven.doxia.sink.Sink; -import org.codehaus.mojo.versions.api.ReportRenderer; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; /** * Factory for report renderers @@ -37,9 +37,11 @@ public interface ReportRendererFactory { * @param sink sink to use for rendering * @param locale locale to use for rendering * @param model data to render + * @param allowSnapshots whether snapshots should be included * @return new report renderer * @throws IllegalArgumentException thrown if the report with the given name could not be found */ - T createReportRenderer(String reportName, Sink sink, Locale locale, U model) + T createReportRenderer( + String reportName, Sink sink, Locale locale, U model, boolean allowSnapshots) throws IllegalArgumentException; } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java index 81af9c857d..2e6c0c211f 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java @@ -26,11 +26,11 @@ import java.util.Locale; import org.apache.maven.doxia.sink.Sink; -import org.codehaus.mojo.versions.api.ReportRenderer; import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; import org.codehaus.mojo.versions.reporting.model.ParentUpdatesModel; import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; import org.codehaus.mojo.versions.reporting.model.PropertyUpdatesModel; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; import org.codehaus.plexus.i18n.I18N; /** @@ -58,20 +58,24 @@ public ReportRendererFactoryImpl(I18N i18N) { */ @Override @SuppressWarnings("unchecked") - public T createReportRenderer(String reportName, Sink sink, Locale locale, U model) + public T createReportRenderer( + String reportName, Sink sink, Locale locale, U model, boolean allowSnapshots) throws IllegalArgumentException { if (DEPENDENCY_UPDATES_REPORT.equals(reportName) || DEPENDENCY_UPDATES_AGGREGATE_REPORT.equals(reportName)) { return (T) new DependencyUpdatesReportRenderer<>( - i18N, sink, locale, reportName, (DependencyUpdatesModel) model); + i18N, sink, locale, reportName, (DependencyUpdatesModel) model, allowSnapshots); } if (PLUGIN_UPDATES_REPORT.equals(reportName) || PLUGIN_UPDATES_AGGREGATE_REPORT.equals(reportName)) { - return (T) new PluginUpdatesReportRenderer(i18N, sink, locale, reportName, (PluginUpdatesModel) model); + return (T) new PluginUpdatesReportRenderer( + i18N, sink, locale, reportName, (PluginUpdatesModel) model, allowSnapshots); } if (PROPERTY_UPDATES_REPORT.equals(reportName) || PROPERTY_UPDATES_AGGREGATE_REPORT.equals(reportName)) { - return (T) new PropertyUpdatesReportRenderer(i18N, sink, locale, reportName, (PropertyUpdatesModel) model); + return (T) new PropertyUpdatesReportRenderer( + i18N, sink, locale, reportName, (PropertyUpdatesModel) model, allowSnapshots); } if (PARENT_UPDATES_REPORT.equals(reportName)) { - return (T) new ParentUpdatesReportRenderer(i18N, sink, locale, reportName, (ParentUpdatesModel) model); + return (T) new ParentUpdatesReportRenderer( + i18N, sink, locale, reportName, (ParentUpdatesModel) model, allowSnapshots); } throw new IllegalArgumentException("Invalid report name: " + reportName); } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java index 9719639752..0f21dbc1d3 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java @@ -25,19 +25,23 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.doxia.sink.Sink; import org.apache.maven.reporting.AbstractMavenReportRenderer; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; import org.codehaus.plexus.i18n.I18N; /** * Base class over AbstractVersionsReportRenderer providing base * utility methods */ -public abstract class VersionsReportRendererBase extends AbstractMavenReportRenderer { +public abstract class VersionsReportRendererBase extends AbstractMavenReportRenderer implements ReportRenderer { /** * Internationalization component. * * @since 1.0-beta-1 */ protected final I18N i18n; + + private final boolean allowSnapshots; + /** * The locale we are rendering for. * @@ -51,17 +55,23 @@ public abstract class VersionsReportRendererBase extends AbstractMavenReportRend */ protected String bundleName; - public VersionsReportRendererBase(Sink sink, I18N i18n, Locale locale, String bundleName) { + public VersionsReportRendererBase(Sink sink, I18N i18n, Locale locale, String bundleName, boolean allowSnapshots) { super(sink); this.i18n = i18n; this.locale = locale; this.bundleName = bundleName; + this.allowSnapshots = allowSnapshots; } public String getTitle() { return getText("report.title"); } + @Override + public boolean isAllowSnapshots() { + return allowSnapshots; + } + /** * Gets the localized message for this report. * diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/AbstractUpdatesModel.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/AbstractUpdatesModel.java index d6855b9a31..9aa4380535 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/AbstractUpdatesModel.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/AbstractUpdatesModel.java @@ -28,7 +28,7 @@ import org.codehaus.mojo.versions.utils.DependencyComparator; /** - * Base class for using with the {@linkplain org.codehaus.mojo.versions.api.ReportRenderer} API + * Base class for using with the {@linkplain org.codehaus.mojo.versions.reporting.util.ReportRenderer} API * @param class extending ArtifactVersion in the constructor */ public abstract class AbstractUpdatesModel { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/DependencyUpdatesModel.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/DependencyUpdatesModel.java index c80c4ba6d7..5a264cc3c7 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/DependencyUpdatesModel.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/DependencyUpdatesModel.java @@ -23,9 +23,10 @@ import org.apache.maven.model.Dependency; import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; /** - * Model class for using with the {@linkplain org.codehaus.mojo.versions.api.ReportRenderer} API + * Model class for using with the {@linkplain ReportRenderer} API */ public class DependencyUpdatesModel extends AbstractUpdatesModel { public DependencyUpdatesModel( diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/ParentUpdatesModel.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/ParentUpdatesModel.java index a4bd329975..fe2a1ade99 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/ParentUpdatesModel.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/ParentUpdatesModel.java @@ -21,12 +21,13 @@ import org.apache.maven.model.Dependency; import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; import static java.util.Collections.emptyMap; import static java.util.Collections.singletonMap; /** - * Model class for using with the {@linkplain org.codehaus.mojo.versions.api.ReportRenderer} API + * Model class for using with the {@linkplain ReportRenderer} API */ public class ParentUpdatesModel extends DependencyUpdatesModel { public ParentUpdatesModel(Dependency parent, ArtifactVersions dependencyUpdates) { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PluginUpdatesModel.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PluginUpdatesModel.java index 2d67f31a20..1c210624f8 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PluginUpdatesModel.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PluginUpdatesModel.java @@ -23,10 +23,11 @@ import org.apache.maven.model.Plugin; import org.codehaus.mojo.versions.api.PluginUpdatesDetails; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; import org.codehaus.mojo.versions.utils.DependencyBuilder; /** - * Model class for using with the {@linkplain org.codehaus.mojo.versions.api.ReportRenderer} API + * Model class for using with the {@linkplain ReportRenderer} API */ public class PluginUpdatesModel extends AbstractUpdatesModel { /** diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PropertyUpdatesModel.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PropertyUpdatesModel.java index 53cba990d6..1997d22259 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PropertyUpdatesModel.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/model/PropertyUpdatesModel.java @@ -27,7 +27,7 @@ import org.codehaus.mojo.versions.utils.PropertyComparator; /** - * Model class for using with the {@linkplain org.codehaus.mojo.versions.api.ReportRenderer} API + * Model class for using with the {@linkplain org.apache.maven.reporting.MavenReportRenderer} API */ public class PropertyUpdatesModel { private final Map allUpdates; diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/ReportRenderer.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/util/ReportRenderer.java similarity index 70% rename from versions-common/src/main/java/org/codehaus/mojo/versions/api/ReportRenderer.java rename to versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/util/ReportRenderer.java index 37d8639af6..239072a8c4 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/ReportRenderer.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/util/ReportRenderer.java @@ -1,4 +1,4 @@ -package org.codehaus.mojo.versions.api; +package org.codehaus.mojo.versions.reporting.util; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -19,12 +19,16 @@ * under the License. */ +import org.apache.maven.reporting.MavenReportRenderer; + /** * Common interface for reporting components */ -public interface ReportRenderer { +public interface ReportRenderer extends MavenReportRenderer { + /** - * Renders the report + * If {@code true}, the report renderer will include snapshots in the report + * @return if {@code true}, the report renderer will include snapshots in the report */ - void render(); + boolean isAllowSnapshots(); } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java index e282381011..5f9c15d86d 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java @@ -36,8 +36,12 @@ * Common utils for Xml report renderers */ class CommonXmlReportRendererUtils { - static void setSection(AbstractVersionDetails versions, Segment segment, Consumer> setterFunction) { - ofNullable(versions.getAllUpdates(of(segment))) + static void setSection( + AbstractVersionDetails versions, + Segment segment, + Consumer> setterFunction, + boolean allowSnapshots) { + ofNullable(versions.getAllUpdates(of(segment), allowSnapshots)) .map(v -> Arrays.stream(v).map(ArtifactVersion::toString).collect(Collectors.toList())) .ifPresent(setterFunction); } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java index 863329ed21..d009a3853a 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java @@ -33,13 +33,13 @@ import org.codehaus.mojo.versions.api.AbstractVersionDetails; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.ArtifactVersionsCache; -import org.codehaus.mojo.versions.api.ReportRenderer; import org.codehaus.mojo.versions.reporting.OverviewStats; import org.codehaus.mojo.versions.reporting.model.DependencyInfo; import org.codehaus.mojo.versions.reporting.model.DependencyReportSummary; import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesReport; import org.codehaus.mojo.versions.reporting.model.io.xpp3.DependencyUpdatesReportXpp3Writer; +import org.codehaus.mojo.versions.reporting.util.ReportRenderer; import static java.util.Optional.empty; import static java.util.Optional.ofNullable; @@ -61,14 +61,28 @@ public class DependencyUpdatesXmlReportRenderer implements ReportRenderer { private final Path outputFile; private final ArtifactVersionsCache newestUpdateCache = new ArtifactVersionsCache(AbstractVersionDetails::getNewestUpdate); + + private final boolean allowSnapshots; + /** * Creates a new instance * @param model object containing the updates model * @param outputFile output file for the report */ - public DependencyUpdatesXmlReportRenderer(DependencyUpdatesModel model, Path outputFile) { + public DependencyUpdatesXmlReportRenderer(DependencyUpdatesModel model, Path outputFile, boolean allowSnapshots) { this.model = model; this.outputFile = outputFile; + this.allowSnapshots = allowSnapshots; + } + + @Override + public boolean isAllowSnapshots() { + return allowSnapshots; + } + + @Override + public String getTitle() { + return "Dependency updates"; } /** @@ -82,7 +96,7 @@ public void render() { setSummary(new DependencyReportSummary() { { OverviewStats overviewStats = OverviewStats.fromUpdates( - model.getAllUpdates().values(), newestUpdateCache); + model.getAllUpdates().values(), newestUpdateCache, isAllowSnapshots()); setUsingLastVersion(String.valueOf(overviewStats.getUpToDate())); setNextVersionAvailable(String.valueOf(overviewStats.getAny())); setNextIncrementalAvailable(String.valueOf(overviewStats.getIncremental())); @@ -90,8 +104,9 @@ public void render() { setNextMajorAvailable(String.valueOf(overviewStats.getMajor())); } }); - setDependencyManagements(createDependencyInfo(model.getArtifactManagementUpdates())); - setDependencies(createDependencyInfo(model.getArtifactUpdates())); + setDependencyManagements( + createDependencyInfo(model.getArtifactManagementUpdates(), isAllowSnapshots())); + setDependencies(createDependencyInfo(model.getArtifactUpdates(), isAllowSnapshots())); } }); } catch (IOException e) { @@ -99,7 +114,8 @@ public void render() { } } - private static List createDependencyInfo(Map versions) { + private static List createDependencyInfo( + Map versions, boolean allowSnapshots) { return versions.entrySet().stream() .map(e -> new DependencyInfo() { { @@ -110,13 +126,13 @@ private static List createDependencyInfo(Map createPluginInfo(Map versions) { + private static List createPluginInfo( + Map versions, boolean allowSnapshots) { return versions.entrySet().stream() .map(e -> new PluginInfo() { { @@ -111,13 +126,13 @@ private static List createPluginInfo(Map createPropertyInfo(Map versions) { + private static List createPropertyInfo( + Map versions, boolean allowSnapshots) { return versions.entrySet().stream() .map(e -> new PropertyInfo() { { @@ -114,13 +128,13 @@ private static List createPropertyInfo(Map\\s*<", "><"); diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java index 035268371c..bb039104d3 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java @@ -432,4 +432,43 @@ public void testVersionInterpolation() throws Exception { assert outputFile == null || !outputFile.exists() || outputFile.delete(); } } + + @Test + public void testAllowSnapshots() + throws MojoExecutionException, MojoFailureException, IllegalAccessException, IOException { + Path tempPath = null; + try { + tempPath = Files.createTempFile("display-dependency-updates", ""); + File tempFile = tempPath.toFile(); + new DisplayDependencyUpdatesMojo( + mockRepositorySystem(), + mockAetherRepositorySystem(new HashMap() { + { + put("default-dependency", new String[] {"1.0.0", "1.0.1-SNAPSHOT"}); + } + }), + null, + null) { + { + setProject(createProject()); + setVariableValueToObject(this, "processDependencies", true); + setVariableValueToObject(this, "dependencyIncludes", singletonList(WildcardMatcher.WILDCARD)); + setVariableValueToObject(this, "dependencyExcludes", emptyList()); + this.allowSnapshots = true; + this.outputFile = tempFile; + setPluginContext(new HashMap<>()); + + session = mockMavenSession(); + } + }.execute(); + + String output = String.join("", Files.readAllLines(tempPath)); + + assertThat(output, containsString("1.0.1-SNAPSHOT")); + } finally { + if (tempPath != null && Files.exists(tempPath)) { + Files.delete(tempPath); + } + } + } } diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java index a25b092bc0..a763c80985 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java @@ -87,7 +87,7 @@ public void testReportGeneration() throws IOException { new MavenVersionComparator())), false)), emptyMap()); - new PluginUpdatesXmlReportRenderer(pluginUpdates, tempFile).render(); + new PluginUpdatesXmlReportRenderer(pluginUpdates, tempFile, false).render(); String output = String.join("", Files.readAllLines(tempFile)).replaceAll(">\\s*<", "><");