Skip to content

Commit

Permalink
Use update policy DAILY instead of NEVER for remote repositories
Browse files Browse the repository at this point in the history
by default plugin central remote repositories has set update policy as NEVER
for update checking it can be not the best way

So every update policy NEVER - will be changed to DAILY
  • Loading branch information
slawekjaranowski committed May 17, 2023
1 parent 9dc1d03 commit 82e2450
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 19 deletions.
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));
}
}

0 comments on commit 82e2450

Please sign in to comment.