Skip to content

Commit

Permalink
Issue #74: Add display-extension-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk committed Jan 25, 2023
1 parent 293206e commit 852bdda
Show file tree
Hide file tree
Showing 11 changed files with 681 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -89,6 +90,7 @@
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;
import static java.util.Optional.of;
Expand Down Expand Up @@ -172,12 +174,35 @@ public Log getLog() {
public ArtifactVersions lookupArtifactVersions(
Artifact artifact, VersionRange versionRange, boolean usePluginRepositories)
throws VersionRetrievalException {
return lookupArtifactVersions(artifact, versionRange, usePluginRepositories, !usePluginRepositories);
}

@Override
public ArtifactVersions lookupArtifactVersions(
Artifact artifact, VersionRange versionRange, boolean usePluginRepositories, boolean useProjectRepositories)
throws VersionRetrievalException {
try {
Collection<IgnoreVersion> ignoredVersions = getIgnoredVersions(artifact);
if (!ignoredVersions.isEmpty() && getLog().isDebugEnabled()) {
getLog().debug("Found ignored versions: "
+ ignoredVersions.stream().map(IgnoreVersion::toString).collect(Collectors.joining(", ")));
}

final List<RemoteRepository> repositories;
if (usePluginRepositories && !useProjectRepositories) {
repositories = mavenSession.getCurrentProject().getRemotePluginRepositories();
} else if (!usePluginRepositories && useProjectRepositories) {
repositories = mavenSession.getCurrentProject().getRemoteProjectRepositories();
} else if (usePluginRepositories) {
repositories = Stream.concat(
mavenSession.getCurrentProject().getRemoteProjectRepositories().stream(),
mavenSession.getCurrentProject().getRemotePluginRepositories().stream())
.distinct()
.collect(Collectors.toList());
} else {
// testing?
repositories = emptyList();
}
return new ArtifactVersions(
artifact,
aetherRepositorySystem
Expand All @@ -191,13 +216,7 @@ public ArtifactVersions lookupArtifactVersions(
.findFirst()
.map(Restriction::toString))
.orElse("(,)")),
usePluginRepositories
? mavenSession
.getCurrentProject()
.getRemotePluginRepositories()
: mavenSession
.getCurrentProject()
.getRemoteProjectRepositories(),
repositories,
"lookupArtifactVersions"))
.getVersions()
.stream()
Expand Down Expand Up @@ -428,16 +447,20 @@ public ArtifactVersion createArtifactVersion(String version) {
return DefaultArtifactVersionCache.of(version);
}

@Override
public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates(
Set<Dependency> dependencies, boolean usePluginRepositories, boolean allowSnapshots)
Set<Dependency> dependencies,
boolean usePluginRepositories,
boolean useProjectRepositories,
boolean allowSnapshots)
throws VersionRetrievalException {
ExecutorService executor = Executors.newFixedThreadPool(LOOKUP_PARALLEL_THREADS);
try {
Map<Dependency, ArtifactVersions> dependencyUpdates = new TreeMap<>(DependencyComparator.INSTANCE);
List<Future<? extends Pair<Dependency, ArtifactVersions>>> futures = dependencies.stream()
.map(dependency -> executor.submit(() -> new ImmutablePair<>(
dependency, lookupDependencyUpdates(dependency, usePluginRepositories, allowSnapshots))))
dependency,
lookupDependencyUpdates(
dependency, usePluginRepositories, useProjectRepositories, allowSnapshots))))
.collect(Collectors.toList());
for (Future<? extends Pair<Dependency, ArtifactVersions>> details : futures) {
Pair<Dependency, ArtifactVersions> pair = details.get();
Expand All @@ -453,12 +476,22 @@ dependency, lookupDependencyUpdates(dependency, usePluginRepositories, allowSnap
}
}

@Override
public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates(
Set<Dependency> dependencies, boolean usePluginRepositories, boolean allowSnapshots)
throws VersionRetrievalException {
return lookupDependenciesUpdates(dependencies, usePluginRepositories, !usePluginRepositories, allowSnapshots);
}

@Override
public ArtifactVersions lookupDependencyUpdates(
Dependency dependency, boolean usePluginRepositories, boolean allowSnapshots)
Dependency dependency,
boolean usePluginRepositories,
boolean useProjectRepositories,
boolean allowSnapshots)
throws VersionRetrievalException {
ArtifactVersions allVersions =
lookupArtifactVersions(createDependencyArtifact(dependency), usePluginRepositories);
ArtifactVersions allVersions = lookupArtifactVersions(
createDependencyArtifact(dependency), null, usePluginRepositories, useProjectRepositories);
return new ArtifactVersions(
allVersions.getArtifact(),
Arrays.stream(allVersions.getAllUpdates(allowSnapshots)).collect(Collectors.toList()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Artifact createDependencyArtifact(
* <b>The resulting {@link ArtifactVersions} instance will contain all versions, including snapshots.</b>
*
* @param artifact The artifact to look for versions of.
* @param usePluginRepositories <code>true</code> will consult the pluginRepositories, while <code>false</code> will
* @param usePluginRepositories {@code true} will consult the pluginRepositories, while {@code false} will
* consult the repositories for normal dependencies.
* @return The details of the available artifact versions.
* @throws VersionRetrievalException thrown if version resolution fails
Expand All @@ -167,7 +167,24 @@ ArtifactVersions lookupArtifactVersions(Artifact artifact, boolean usePluginRepo
* <b>The resulting {@link ArtifactVersions} instance will contain all versions, including snapshots.</b>
*
* @param artifact The artifact to look for versions of.
* @param versionRange versionRange to restrict the search
* @param versionRange versionRange to restrict the search, may be {@code null}
* @param usePluginRepositories {@code true} will consult the pluginRepositories
* @param useProjectRepositories {@code true} will consult regular project repositories
* @return The details of the available artifact versions.
* @throws VersionRetrievalException thrown if version resolution fails
* @since 2.15.0
*/
ArtifactVersions lookupArtifactVersions(
Artifact artifact, VersionRange versionRange, boolean usePluginRepositories, boolean useProjectRepositories)
throws VersionRetrievalException;

/**
* Looks up the versions of the specified artifact that are available in either the local repository, or the
* appropriate remote repositories.
* <b>The resulting {@link ArtifactVersions} instance will contain all versions, including snapshots.</b>
*
* @param artifact The artifact to look for versions of.
* @param versionRange versionRange to restrict the search, may be {@code null}
* @param usePluginRepositories <code>true</code> will consult the pluginRepositories, while <code>false</code> will
* consult the repositories for normal dependencies.
* @return The details of the available artifact versions.
Expand All @@ -190,18 +207,39 @@ Map<Dependency, ArtifactVersions> lookupDependenciesUpdates(
Set<Dependency> dependencies, boolean usePluginRepositories, boolean allowSnapshots)
throws VersionRetrievalException;

/**
* 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.
* @param useProjectRepositories whether to use regular project repositories
* @param allowSnapshots whether snapshots should be included
* @return map containing the ArtifactVersions object per dependency
*/
Map<Dependency, ArtifactVersions> lookupDependenciesUpdates(
Set<Dependency> dependencies,
boolean usePluginRepositories,
boolean useProjectRepositories,
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 useProjectRepositories whether to use regular project 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, boolean allowSnapshots)
Dependency dependency,
boolean usePluginRepositories,
boolean useProjectRepositories,
boolean allowSnapshots)
throws VersionRetrievalException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ public Set<Dependency> removingFrom(Collection<Dependency> dependencies) {
return filterBy(dependencies, not(this::matchersMatch));
}

private boolean matchersMatch(Dependency dependency) {
public boolean matchersMatch(Dependency dependency) {
return matchers.stream().anyMatch(m -> m.test(dependency));
}

public boolean matchersDontMatch(Dependency dependency) {
return !matchersMatch(dependency);
}

private TreeSet<Dependency> filterBy(Collection<Dependency> dependencies, Predicate<Dependency> predicate) {
return dependencies.stream()
.filter(predicate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public static DependencyBuilder newBuilder() {
* @param artifactId artifactId of the dependency
* @param version version of the dependency
* @return new instance of {@linkplain Dependency}
* @deprecated please use the {@link #newBuilder()} method instead
*/
@Deprecated
public static Dependency dependencyWith(String groupId, String artifactId, String version) {
return newBuilder()
.withGroupId(groupId)
Expand All @@ -154,7 +156,9 @@ public static Dependency dependencyWith(String groupId, String artifactId, Strin
* @param classifier classifier of the dependency
* @param scope scope of the dependency
* @return new instance of {@linkplain Dependency}
* @deprecated please use the {@link #newBuilder()} method instead
*/
@Deprecated
public static Dependency dependencyWith(
String groupId, String artifactId, String version, String type, String classifier, String scope) {
return newBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.codehaus.mojo.versions.utils;

/*
* Copyright MojoHaus and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Extension;
import org.apache.maven.model.InputLocation;

import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;

/**
* Builder class for {@linkplain Extension}
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public class ExtensionBuilder {
private Optional<String> groupId = empty();
private Optional<String> artifactId = empty();
private Optional<String> version = empty();
private Map<Object, InputLocation> locations = new HashMap<>();

private ExtensionBuilder() {}

/**
* Passes groupId to the builder
* @param groupId given groupId
* @return builder instance
*/
public ExtensionBuilder withGroupId(String groupId) {
this.groupId = ofNullable(groupId);
return this;
}

/**
* Passes artifactId to the builder
* @param artifactId given artifactId
* @return builder instance
*/
public ExtensionBuilder withArtifactId(String artifactId) {
this.artifactId = ofNullable(artifactId);
return this;
}

/**
* Passes version to the builder
* @param version given version
* @return builder instance
*/
public ExtensionBuilder withVersion(String version) {
this.version = ofNullable(version);
return this;
}

/**
* Passes type to the builder
* @param key location key
* @param location input location
* @return builder instance
*/
public ExtensionBuilder withLocation(Object key, InputLocation location) {
this.locations.put(key, location);
return this;
}

/**
* Creates a new instance of the builder
* @return new instance of the builder
*/
public static ExtensionBuilder newBuilder() {
return new ExtensionBuilder();
}

/**
* Builds the {@linkplain Dependency} instance
* @return {@linkplain Dependency} instance
*/
public Extension build() {
Extension inst = new Extension();
groupId.ifPresent(inst::setGroupId);
artifactId.ifPresent(inst::setArtifactId);
version.ifPresent(inst::setVersion);
locations.forEach(inst::setLocation);
return inst;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:display-extension-updates
invoker.mavenOpts.1 = -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8

invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:display-extension-updates
invoker.mavenOpts.2 = -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -DextensionExcludes=localhost

invoker.goals.3 = ${project.groupId}:${project.artifactId}:${project.version}:display-extension-updates
invoker.mavenOpts.3 = -Dversions.outputFile=./output3.txt -DoutputEncoding=UTF-8 -DextensionIncludes=localhost -DextensionExcludes=localhost:dummy-api
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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>localhost</groupId>
<artifactId>it-display-extension-updates-001</artifactId>

<version>1.0</version>
<packaging>pom</packaging>

<build>
<extensions>
<extension>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
</extension>
<extension>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</extension>
</extensions>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def output1 = new File( basedir, "output1.txt").text
assert output1 =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*\Q1.0\E\s+->\s+\Q3.1\E/
assert output1 =~ /\Qlocalhost:dummy-api\E\s*\.*\s*\Q1.0\E\s+->\s+\Q3.0\E/

def output2 = new File( basedir, "output2.txt")
assert !output2.exists()

def output3 = new File( basedir, "output3.txt").text
assert output3 =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*\Q1.0\E\s+->\s+\Q3.1\E/
assert !( output3 =~ /\Qlocalhost:dummy-api\E\s*\.*\s*\Q1.0\E\s+->\s+\Q3.0\E/ )

0 comments on commit 852bdda

Please sign in to comment.