Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use update policy DAILY instead of NEVER for remote repositories #957

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.repository.AuthenticationContext;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.VersionRangeRequest;
Expand Down Expand Up @@ -119,25 +120,25 @@ public class DefaultVersionsHelper implements VersionsHelper {
*/
private RuleSet ruleSet;

private RepositorySystem repositorySystem;
private final RepositorySystem repositorySystem;

private org.eclipse.aether.RepositorySystem aetherRepositorySystem;
private final org.eclipse.aether.RepositorySystem aetherRepositorySystem;

/**
* The {@link Log} to send log messages to.
*
* @since 1.0-alpha-3
*/
private Log log;
private final Log log;

/**
* The maven session.
*
* @since 1.0-beta-1
*/
private MavenSession mavenSession;
private final MavenSession mavenSession;

private MojoExecution mojoExecution;
private final MojoExecution mojoExecution;

/**
* A cache mapping artifacts to their best fitting rule, since looking up
Expand All @@ -146,10 +147,82 @@ public class DefaultVersionsHelper implements VersionsHelper {
* @since 2.12
*/
private final Map<String, Rule> artifactBestFitRule = new HashMap<>();

private final List<RemoteRepository> remoteProjectRepositories;

private final List<RemoteRepository> remotePluginRepositories;

private final List<RemoteRepository> remoteRepositories;

/**
* Private constructor used by the builder
*/
private DefaultVersionsHelper() {}
private DefaultVersionsHelper(
RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenSession mavenSession,
MojoExecution mojoExecution,
Log log) {
this.repositorySystem = repositorySystem;
this.aetherRepositorySystem = aetherRepositorySystem;
this.mavenSession = mavenSession;
this.mojoExecution = mojoExecution;
this.log = log;

this.remoteProjectRepositories = Optional.ofNullable(mavenSession)
.map(MavenSession::getCurrentProject)
.map(MavenProject::getRemoteProjectRepositories)
.map(DefaultVersionsHelper::adjustRemoteRepositoriesRefreshPolicy)
.orElseGet(Collections::emptyList);

this.remotePluginRepositories = Optional.ofNullable(mavenSession)
.map(MavenSession::getCurrentProject)
.map(MavenProject::getRemotePluginRepositories)
.map(DefaultVersionsHelper::adjustRemoteRepositoriesRefreshPolicy)
.orElseGet(Collections::emptyList);

this.remoteRepositories = Stream.concat(remoteProjectRepositories.stream(), remotePluginRepositories.stream())
.distinct()
.collect(Collectors.toList());
}

static List<RemoteRepository> adjustRemoteRepositoriesRefreshPolicy(List<RemoteRepository> remoteRepositories) {
return remoteRepositories.stream()
.map(DefaultVersionsHelper::adjustRemoteRepositoryRefreshPolicy)
.collect(Collectors.toList());
}

static RemoteRepository adjustRemoteRepositoryRefreshPolicy(RemoteRepository remoteRepository) {
RepositoryPolicy snapshotPolicy = remoteRepository.getPolicy(true);
RepositoryPolicy releasePolicy = remoteRepository.getPolicy(false);

RepositoryPolicy newSnapshotPolicy = null;
RepositoryPolicy newReleasePolicy = null;

if (snapshotPolicy.isEnabled()
&& RepositoryPolicy.UPDATE_POLICY_NEVER.equals(snapshotPolicy.getUpdatePolicy())) {
newSnapshotPolicy = new RepositoryPolicy(
true, RepositoryPolicy.UPDATE_POLICY_DAILY, snapshotPolicy.getChecksumPolicy());
}

if (releasePolicy.isEnabled() && RepositoryPolicy.UPDATE_POLICY_NEVER.equals(releasePolicy.getUpdatePolicy())) {
newReleasePolicy =
new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY, releasePolicy.getChecksumPolicy());
}

if (newSnapshotPolicy != null || newReleasePolicy != null) {
RemoteRepository.Builder builder = new RemoteRepository.Builder(remoteRepository);
if (newSnapshotPolicy != null) {
builder.setSnapshotPolicy(newSnapshotPolicy);
}
if (newReleasePolicy != null) {
builder.setReleasePolicy(newReleasePolicy);
}
return builder.build();
} else {
return remoteRepository;
}
}

static boolean exactMatch(String wildcardRule, String value) {
Pattern p = Pattern.compile(RegexUtils.convertWildcardsToRegex(wildcardRule, true));
Expand Down Expand Up @@ -190,19 +263,16 @@ public ArtifactVersions lookupArtifactVersions(

final List<RemoteRepository> repositories;
if (usePluginRepositories && !useProjectRepositories) {
repositories = mavenSession.getCurrentProject().getRemotePluginRepositories();
repositories = remotePluginRepositories;
} else if (!usePluginRepositories && useProjectRepositories) {
repositories = mavenSession.getCurrentProject().getRemoteProjectRepositories();
repositories = remoteProjectRepositories;
} else if (usePluginRepositories) {
repositories = Stream.concat(
mavenSession.getCurrentProject().getRemoteProjectRepositories().stream(),
mavenSession.getCurrentProject().getRemotePluginRepositories().stream())
.distinct()
.collect(Collectors.toList());
repositories = remoteRepositories;
} else {
// testing?
repositories = emptyList();
}

return new ArtifactVersions(
artifact,
aetherRepositorySystem
Expand Down Expand Up @@ -911,10 +981,8 @@ public Builder withWagonMap(Map<String, Wagon> wagonMap) {
* @throws MojoExecutionException should the constructor with the RuleSet retrieval doesn't succeed
*/
public DefaultVersionsHelper build() throws MojoExecutionException {
DefaultVersionsHelper instance = new DefaultVersionsHelper();
instance.repositorySystem = repositorySystem;
instance.mavenSession = mavenSession;
instance.mojoExecution = mojoExecution;
DefaultVersionsHelper instance = new DefaultVersionsHelper(
repositorySystem, aetherRepositorySystem, mavenSession, mojoExecution, log);
if (ruleSet != null) {
if (!isBlank(rulesUri)) {
log.warn("rulesUri is ignored if rules are specified in pom or as parameters");
Expand All @@ -928,8 +996,6 @@ public DefaultVersionsHelper build() throws MojoExecutionException {
if (ignoredVersions != null && !ignoredVersions.isEmpty()) {
instance.ruleSet = enrichRuleSet(ignoredVersions, instance.ruleSet);
}
instance.aetherRepositorySystem = aetherRepositorySystem;
instance.log = log;
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.codehaus.mojo.versions.ordering.VersionComparators;
import org.codehaus.mojo.versions.utils.VersionStub;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResult;
import org.eclipse.aether.version.Version;
Expand All @@ -67,8 +69,10 @@
import static java.util.Collections.singletonMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsIterableContaining.hasItems;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.any;
Expand Down Expand Up @@ -328,4 +332,51 @@ public void testIgnoredVersionsShouldExtendTheRuleSet() throws MojoExecutionExce
.collect(Collectors.toList()),
containsInAnyOrder(".*-M.", ".*-SNAPSHOT", "1.0.0"));
}

@Test
public void testRemoteRepositoryWithNeverUpdatePolicyShouldBeChangToDaily() {

RemoteRepository repo1 = new RemoteRepository.Builder("id1", "", "")
.setSnapshotPolicy(new RepositoryPolicy(
true, RepositoryPolicy.UPDATE_POLICY_NEVER, RepositoryPolicy.CHECKSUM_POLICY_IGNORE))
.setReleasePolicy(new RepositoryPolicy(
true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_IGNORE))
.build();

RemoteRepository repo2 = new RemoteRepository.Builder("id2", "", "")
.setSnapshotPolicy(new RepositoryPolicy(
false, RepositoryPolicy.UPDATE_POLICY_NEVER, RepositoryPolicy.CHECKSUM_POLICY_IGNORE))
.setReleasePolicy(new RepositoryPolicy(
true, RepositoryPolicy.UPDATE_POLICY_NEVER, RepositoryPolicy.CHECKSUM_POLICY_IGNORE))
.build();

RemoteRepository repo3 = new RemoteRepository.Builder("id3", "", "")
.setSnapshotPolicy(new RepositoryPolicy(
true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_IGNORE))
.setReleasePolicy(new RepositoryPolicy(
true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_IGNORE))
.build();

List<RemoteRepository> remoteRepositories =
DefaultVersionsHelper.adjustRemoteRepositoriesRefreshPolicy(Arrays.asList(repo1, repo2, repo3));

assertThat(remoteRepositories, hasSize(3));
assertThat(remoteRepositories.get(0), not(is(repo1)));
assertThat(remoteRepositories.get(1), not(is(repo2)));
assertThat(remoteRepositories.get(2), is(repo3));

assertThat(
remoteRepositories.get(0).getPolicy(true).getUpdatePolicy(),
equalTo(RepositoryPolicy.UPDATE_POLICY_DAILY));
assertThat(
remoteRepositories.get(0).getPolicy(false).getUpdatePolicy(),
equalTo(RepositoryPolicy.UPDATE_POLICY_DAILY));

assertThat(
remoteRepositories.get(1).getPolicy(true).getUpdatePolicy(),
equalTo(RepositoryPolicy.UPDATE_POLICY_NEVER));
assertThat(
remoteRepositories.get(1).getPolicy(false).getUpdatePolicy(),
equalTo(RepositoryPolicy.UPDATE_POLICY_DAILY));
}
}