Skip to content

Commit

Permalink
Use remotePluginRepositories for resolving plugins - fix #537
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 25, 2024
1 parent 1a5ab9d commit 7720870
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 19 deletions.
64 changes: 64 additions & 0 deletions src/it/issue-499-plugin-repositories/pom-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017 Slawomir Jaranowski
~
~ 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.
-->
<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>

<parent>
<groupId>test</groupId>
<artifactId>it-test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>

<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<!-- only plugin repository are available -->
<pluginRepositories>
<pluginRepository>
<id>groovy-plugins-release</id>
<url>https://groovy.jfrog.io/artifactory/plugins-release</url>
</pluginRepository>
</pluginRepositories>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>3.9.0</version>
</plugin>
<plugin>
<groupId>org.simplify4u.plugins</groupId>
<artifactId>pgpverify-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<verifyPlugins>true</verifyPlugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
7 changes: 7 additions & 0 deletions src/it/issue-499-plugin-repositories/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


def buildLog = new File( basedir, 'build.log' )

// only check that plugin was resolved
assert buildLog.text.contains('org.codehaus.groovy:groovy-eclipse-compiler:jar:3.9.0')
assert buildLog.text.contains('org.codehaus.groovy:groovy-eclipse-compiler:pom:3.9.0')
58 changes: 39 additions & 19 deletions src/main/java/org/simplify4u/plugins/ArtifactResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.vavr.control.Try;
import org.apache.maven.RepositoryUtils;
Expand Down Expand Up @@ -82,7 +83,9 @@ public class ArtifactResolver {

private final RepositorySystemSession repositorySession;

private final List<RemoteRepository> remoteRepositories;
private final List<RemoteRepository> remoteProjectRepositories;

private final List<RemoteRepository> remotePluginRepositories;

/**
* Copy of remote repositories with check sum policy set to ignore, we need it for pgp signature resolving.
Expand All @@ -93,25 +96,38 @@ public class ArtifactResolver {

@Inject
ArtifactResolver(MavenSession session, RepositorySystem repositorySystem) {
this.remoteRepositories = requireNonNull(session.getCurrentProject().getRemoteProjectRepositories());
this.remoteRepositoriesIgnoreCheckSum = repositoriesIgnoreCheckSum(remoteRepositories);
this.remoteProjectRepositories = requireNonNull(session.getCurrentProject().getRemoteProjectRepositories());
this.remotePluginRepositories = requireNonNull(session.getCurrentProject().getRemotePluginRepositories());
this.repositorySystem = requireNonNull(repositorySystem);
this.repositorySession = requireNonNull(session.getRepositorySession());
this.remoteRepositoriesIgnoreCheckSum = repositoriesIgnoreCheckSum(remoteProjectRepositories,
remotePluginRepositories);
}

/**
* Wrap remote repository with ignore check sum policy.
*
* @param repositories list to wrap
* @param remoteProjectRepositories project repositories to wrap
* @param remotePluginRepositories plugin repositories to wrap
* @return wrapped repository list
*/
private static List<RemoteRepository> repositoriesIgnoreCheckSum(List<RemoteRepository> repositories) {
private List<RemoteRepository> repositoriesIgnoreCheckSum(List<RemoteRepository> remoteProjectRepositories,
List<RemoteRepository> remotePluginRepositories) {

return Optional.ofNullable(repositories)
Stream<RemoteRepository> remoteProjectRepositoryStream = Optional.ofNullable(remoteProjectRepositories)
.orElse(Collections.emptyList())
.stream()
.map(ArtifactResolver::repositoryIgnoreCheckSum)
.collect(Collectors.toList());
.stream();

Stream<RemoteRepository> remotePluginsRepositoryStream = Optional.ofNullable(remotePluginRepositories)
.orElse(Collections.emptyList())
.stream();

List<RemoteRepository> remoteRepositories =
Stream.concat(remoteProjectRepositoryStream, remotePluginsRepositoryStream)
.map(ArtifactResolver::repositoryIgnoreCheckSum)
.collect(Collectors.toList());

return repositorySystem.newResolutionRepositories(repositorySession, remoteRepositories);
}

private static RemoteRepository repositoryIgnoreCheckSum(RemoteRepository repository) {
Expand Down Expand Up @@ -196,7 +212,7 @@ private List<Artifact> resolvePlugin(Plugin plugin, Configuration config) {
List<org.eclipse.aether.artifact.Artifact> result;
if (config.verifyPluginDependencies) {
// we need resolve all transitive dependencies
result = resolveArtifactsTransitive(pArtifact, plugin.getDependencies(), config.verifyPomFiles);
result = resolvePluginArtifactsTransitive(pArtifact, plugin.getDependencies(), config.verifyPomFiles);
} else {
// only resolve plugin artifact
List<org.eclipse.aether.artifact.Artifact> aeArtifacts = new ArrayList<>();
Expand All @@ -206,17 +222,18 @@ private List<Artifact> resolvePlugin(Plugin plugin, Configuration config) {
.map(Dependency::getArtifact)
.collect(Collectors.toList()));

result = resolveArtifacts(aeArtifacts, config.verifyPomFiles);
result = resolveArtifacts(aeArtifacts, remotePluginRepositories, config.verifyPomFiles);
}

return result.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toList());
}

private List<org.eclipse.aether.artifact.Artifact> resolveArtifactsTransitive(
private List<org.eclipse.aether.artifact.Artifact> resolvePluginArtifactsTransitive(
org.eclipse.aether.artifact.Artifact artifact,
List<org.apache.maven.model.Dependency> dependencies, boolean verifyPomFiles) {

CollectRequest collectRequest = new CollectRequest(new Dependency(artifact, "runtime"), remoteRepositories);
CollectRequest collectRequest = new CollectRequest(new Dependency(artifact, "runtime"),
remotePluginRepositories);

dependencies.stream().map(d -> RepositoryUtils.toDependency(d, repositorySession.getArtifactTypeRegistry()))
.forEach(collectRequest::addDependency);
Expand All @@ -236,13 +253,14 @@ private List<org.eclipse.aether.artifact.Artifact> resolveArtifactsTransitive(
.collect(Collectors.toList()));

if (verifyPomFiles) {
resolvePoms(result);
resolvePoms(result, remotePluginRepositories);
}
return result;
}

private List<org.eclipse.aether.artifact.Artifact> resolveArtifacts(
List<org.eclipse.aether.artifact.Artifact> artifacts,
List<RemoteRepository> remoteRepositories,
boolean verifyPomFiles) {

List<ArtifactRequest> requestList = artifacts.stream()
Expand All @@ -260,20 +278,21 @@ private List<org.eclipse.aether.artifact.Artifact> resolveArtifacts(
.collect(Collectors.toList()));

if (verifyPomFiles) {
resolvePoms(result);
resolvePoms(result, remoteRepositories);
}

return result;

}

private void resolvePoms(List<org.eclipse.aether.artifact.Artifact> result) {
private void resolvePoms(List<org.eclipse.aether.artifact.Artifact> result,
List<RemoteRepository> remoteRepositories) {
List<org.eclipse.aether.artifact.Artifact> poms =
result.stream().filter(a -> !"pom".equals(a.getExtension()))
.map(a -> new SubArtifact(a, null, "pom"))
.collect(Collectors.toList());

result.addAll(resolveArtifacts(poms, false));
result.addAll(resolveArtifacts(poms, remoteRepositories, false));
}

private static org.eclipse.aether.artifact.Artifact toArtifact(Plugin plugin) {
Expand Down Expand Up @@ -408,7 +427,7 @@ private Set<Artifact> resolveProjectArtifacts(Iterable<Artifact> artifacts, Skip
}

List<ArtifactRequest> requestList = collection.stream()
.map(a -> new ArtifactRequest(a, remoteRepositories, null))
.map(a -> new ArtifactRequest(a, remoteProjectRepositories, null))
.collect(Collectors.toList());

List<ArtifactResult> artifactResults =
Expand Down Expand Up @@ -439,7 +458,8 @@ private Set<Artifact> resolveProjectArtifacts(Iterable<Artifact> artifacts, Skip
public List<Artifact> resolveArtifact(Artifact artifact, boolean verifyPomFiles) {

List<org.eclipse.aether.artifact.Artifact> artifacts =
resolveArtifacts(Collections.singletonList(RepositoryUtils.toArtifact(artifact)), verifyPomFiles);
resolveArtifacts(Collections.singletonList(RepositoryUtils.toArtifact(artifact)),
remoteProjectRepositories, verifyPomFiles);

return artifacts.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toList());
}
Expand Down

0 comments on commit 7720870

Please sign in to comment.