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
andrzejj0 authored and slawekjaranowski committed May 9, 2023
1 parent 4b9f45e commit b3a4037
Show file tree
Hide file tree
Showing 30 changed files with 1,316 additions and 71 deletions.
34 changes: 34 additions & 0 deletions versions-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,40 @@
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<configuration>
<models>
<model>src/main/mdo/core-extensions.mdo</model>
</models>
<version>1.1.0</version>
</configuration>
<executions>
<execution>
<id>generate-java-classes</id>
<goals>
<goal>xpp3-reader</goal>
<goal>java</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<!-- Exclude packages generated by Modello in JavaDocs -->
<excludePackageNames>org.codehaus.mojo.versions.model,
org.codehaus.mojo.versions.model.io.xpp3,
org.codehaus.mojo.versions.utils</excludePackageNames>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
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
@@ -1,22 +1,18 @@
package org.codehaus.mojo.versions.filtering;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
* 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
* 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.
* 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.Collection;
Expand Down Expand Up @@ -66,10 +62,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
@@ -0,0 +1,64 @@
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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.apache.maven.model.Extension;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.model.io.xpp3.CoreExtensionsXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* Utilities for reading and handling core extensions.
*
* @author Andrzej Jarmoniuk
* @since 2.15.0
*/
public final class CoreExtensionUtils {
/**
* Reads the core extensions (not build extensions) configured for the given project
* from the {@code ${project}/.mvn/extensions.xml} file.
*
* @param project {@link MavenProject} instance
* @return stream of core extensions defined in the {@code ${project}/.mvn/extensions.xml} file
* @throws IOException thrown if a file I/O operation fails
* @throws XmlPullParserException thrown if the file cannot be parsed
* @since 2.15.0
*/
public static Stream<Extension> getCoreExtensions(MavenProject project) throws IOException, XmlPullParserException {
Path extensionsFile = project.getBasedir().toPath().resolve(".mvn/extensions.xml");
if (!Files.isRegularFile(extensionsFile)) {
return Stream.empty();
}

try (Reader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(extensionsFile)))) {
return new CoreExtensionsXpp3Reader()
.read(reader).getExtensions().stream().map(ex -> ExtensionBuilder.newBuilder()
.withGroupId(ex.getGroupId())
.withArtifactId(ex.getArtifactId())
.withVersion(ex.getVersion())
.build());
}
}
}
Loading

0 comments on commit b3a4037

Please sign in to comment.