Skip to content

Commit

Permalink
[SWARM-1173] properly propagate Maven repo policies (thorntail#425)
Browse files Browse the repository at this point in the history
Motivation
----------
Maven repository policies weren't properly propagated (they were
ignored) when converting Maven objects to Aether objects. The defaults
were used every time. One of the implications was that
the build was slower as all declared repositories were checked for new
SNAPSHOTs (even if user disabled that).

Modifications
-------------
Both snapshot and release policies are properly propagted when creating the Aether
remote repositories.

Result
------
Maven plugin no longer tries to download SNAPSHOTs from Maven Central
(or any other repository which has SNAPSHOTs disabled)
  • Loading branch information
psiroky authored and bobmcwhirter committed Mar 17, 2017
1 parent ecc87d7 commit 422c8d0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.stream.Collectors;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.Authentication;
import org.apache.maven.model.DependencyManagement;
import org.eclipse.aether.RepositorySystem;
Expand All @@ -40,6 +41,7 @@
import org.eclipse.aether.repository.LocalArtifactResult;
import org.eclipse.aether.repository.Proxy;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
Expand All @@ -57,6 +59,8 @@
*/
public class MavenArtifactResolvingHelper implements ArtifactResolvingHelper {

public static final ArtifactRepositoryPolicy DEFAULT_ARTIFACT_REPOSITORY_POLICY = new ArtifactRepositoryPolicy();

public MavenArtifactResolvingHelper(ArtifactResolver resolver,
RepositorySystem system,
RepositorySystemSession session,
Expand All @@ -67,17 +71,23 @@ public MavenArtifactResolvingHelper(ArtifactResolver resolver,
this.dependencyManagement = dependencyManagement;
this.remoteRepositories.add(buildRemoteRepository("jboss-public-repository-group",
"https://repository.jboss.org/nexus/content/groups/public/",
null));
null,
DEFAULT_ARTIFACT_REPOSITORY_POLICY,
DEFAULT_ARTIFACT_REPOSITORY_POLICY));
}

public void remoteRepository(ArtifactRepository repo) {
remoteRepository(buildRemoteRepository(repo.getId(), repo.getUrl(), repo.getAuthentication()));
remoteRepository(buildRemoteRepository(repo.getId(), repo.getUrl(), repo.getAuthentication(), repo.getReleases(), repo.getSnapshots()));
}

public void remoteRepository(RemoteRepository repo) {
this.remoteRepositories.add(repo);
}

public List<RemoteRepository> getRemoteRepositories() {
return this.remoteRepositories;
}

@Override
public ArtifactSpec resolve(ArtifactSpec spec) {
if (spec.file == null) {
Expand Down Expand Up @@ -203,7 +213,8 @@ private void resolveDependenciesInParallel(List<DependencyNode> nodes) {
}
}

private RemoteRepository buildRemoteRepository(final String id, final String url, final Authentication auth) {
private RemoteRepository buildRemoteRepository(final String id, final String url, final Authentication auth,
final ArtifactRepositoryPolicy releasesPolicy, final ArtifactRepositoryPolicy snapshotsPolicy) {
RemoteRepository.Builder builder = new RemoteRepository.Builder(id, "default", url);
if (auth != null
&& auth.getUsername() != null
Expand All @@ -213,6 +224,9 @@ private RemoteRepository buildRemoteRepository(final String id, final String url
.addPassword(auth.getPassword()).build());
}

builder.setSnapshotPolicy(new RepositoryPolicy(snapshotsPolicy.isEnabled(), snapshotsPolicy.getUpdatePolicy(), snapshotsPolicy.getChecksumPolicy()));
builder.setReleasePolicy(new RepositoryPolicy(releasesPolicy.isEnabled(), releasesPolicy.getUpdatePolicy(), releasesPolicy.getChecksumPolicy()));

RemoteRepository repository = builder.build();

final RemoteRepository mirror = session.getMirrorSelector().getMirror(repository);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2017 Red Hat, Inc, and individual 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.
*/
package org.wildfly.swarm.plugin.maven;

import java.util.List;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.MirrorSelector;
import org.eclipse.aether.repository.ProxySelector;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class MavenArtifactResolvingHelperTest {

@Test
// SWARM-1173: swarm-plugin trying to download SNAPSHOTs from Maven Central
public void propagateRepositoryPolicies() {
RepositorySystemSession sessionMock = Mockito.mock(RepositorySystemSession.class);
MirrorSelector mirrorSelectorMock = Mockito.mock(MirrorSelector.class);
Mockito.when(sessionMock.getMirrorSelector()).thenReturn(mirrorSelectorMock);
ProxySelector proxySelectorMock = Mockito.mock(ProxySelector.class);
Mockito.when(sessionMock.getProxySelector()).thenReturn(proxySelectorMock);

MavenArtifactResolvingHelper artifactResolverHelper = new MavenArtifactResolvingHelper(null, null, sessionMock, null);
ArtifactRepositoryPolicy testSnapshotPolicy = new ArtifactRepositoryPolicy(false, "hourly", "warn");
ArtifactRepositoryPolicy testReleasesPolicy = new ArtifactRepositoryPolicy(true, "never", "warn");
ArtifactRepository testingRepo = new MavenArtifactRepository("testing-repo", "http://testing-repo.org", new DefaultRepositoryLayout(), testSnapshotPolicy, testReleasesPolicy);
artifactResolverHelper.remoteRepository(testingRepo);

List<RemoteRepository> remoteRepos = artifactResolverHelper.getRemoteRepositories();

Assert.assertTrue("Remote repositories " + remoteRepos + " do not contain expected testing repo " + testingRepo,
remoteRepos.stream().anyMatch(
remoteRepo -> {
RepositoryPolicy snapshotsPolicy = remoteRepo.getPolicy(true);
RepositoryPolicy releasesPolicy = remoteRepo.getPolicy(false);
return remoteRepo.getId().equals(testingRepo.getId()) &&
!snapshotsPolicy.isEnabled() &&
snapshotsPolicy.getUpdatePolicy().equals(testSnapshotPolicy.getUpdatePolicy()) &&
snapshotsPolicy.getChecksumPolicy().equals(testSnapshotPolicy.getChecksumPolicy()) &&
releasesPolicy.isEnabled() &&
releasesPolicy.getUpdatePolicy().equals(testReleasesPolicy.getUpdatePolicy()) &&
releasesPolicy.getChecksumPolicy().equals(testReleasesPolicy.getChecksumPolicy());
})
);
}

}

0 comments on commit 422c8d0

Please sign in to comment.