Skip to content

Commit

Permalink
#666 added support for Maven repository policies in CLI (#668)
Browse files Browse the repository at this point in the history
* #666 added support for Maven repository policies in CLI

* #666 fixed checkstyle violations

* #666 added documentation for repository policies
  • Loading branch information
DirkMahler authored Oct 10, 2024
1 parent d7735d2 commit d83c25c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,20 @@ private static void applyRepositorySettings(Map<String, String> properties, Sett
.collect(toList()));
for (Repository repository : repositories) {
put(repository.getUrl(), properties, Remote.PREFIX, repository.getId(), Remote.URL);
applyRepositoryPolicy(repository.getId(), repository.getReleases(), properties, Remote.RELEASES);
applyRepositoryPolicy(repository.getId(), repository.getSnapshots(), properties, Remote.SNAPSHOTS);
applyServerCredentials(repository.getId(), settings, properties, Remote.PREFIX, Remote.USERNAME, Remote.PASSWORD);
}
}

private static void applyRepositoryPolicy(String repositoryId, RepositoryPolicy policy, Map<String, String> properties, String policyPrefix) {
if (policy != null) {
put(Boolean.toString(policy.isEnabled()), properties, Remote.PREFIX, repositoryId, policyPrefix, Policy.ENABLED);
put(policy.getUpdatePolicy(), properties, Remote.PREFIX, repositoryId, policyPrefix, Policy.UPDATE_POLICY);
put(policy.getChecksumPolicy(), properties, Remote.PREFIX, repositoryId, policyPrefix, Policy.CHECKSUM_POLICY);
}
}

private static void applyServerCredentials(String serverId, Settings settings, Map<String, String> properties, String serverPropertyPrefix,
String serverUsernameProperty, String serverPasswordProperty) {
Server server = settings.getServer(serverId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.buschmais.jqassistant.commandline.configuration;

import com.buschmais.jqassistant.core.shared.annotation.Description;

import io.smallrye.config.WithDefault;

@Description("The repository policy for a Maven repository.")
public interface Policy {

String ENABLED = "enabled";

@WithDefault("true")
boolean enabled();

String UPDATE_POLICY = "update-policy";

@WithDefault("daily")
String updatePolicy();

String CHECKSUM_POLICY = "checksum-policy";

@WithDefault("warn")
String checksumPolicy();

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public interface Remote {
@Description("The password for authenticating against the repository.")
Optional<String> password();

String RELEASES = "releases";

@Description("The release repository policy.")
Policy releases();

String SNAPSHOTS = "snapshots";

@Description("The snapshot repository policy.")
Policy snapshots();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
import static java.util.Optional.of;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.eclipse.aether.repository.RepositoryPolicy.CHECKSUM_POLICY_FAIL;
import static org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_DAILY;
import static org.eclipse.aether.repository.RepositoryPolicy.*;

/**
* Factory for the {@link PluginResolver} to be used in standalone in the CLI.
Expand All @@ -47,10 +46,25 @@ public class ArtifactProviderFactory {

public static final String MAVEN_CENTRAL_URL = "https://repo1.maven.org/maven2";

private static final RepositoryPolicy SNAPSHOT_REPOSITORY_POLICY = new RepositoryPolicy(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_FAIL);

private static final String REPOSITORY_LAYOUT_DEFAULT = "default";

private static final Policy DEFAULT_REPOSITORY_POLICY = new Policy() {
@Override
public boolean enabled() {
return true;
}

@Override
public String updatePolicy() {
return UPDATE_POLICY_DAILY;
}

@Override
public String checksumPolicy() {
return CHECKSUM_POLICY_WARN;
}
};

private static final Remote MAVEN_CENTRAL = new Remote() {
@Override
public String url() {
Expand All @@ -66,6 +80,16 @@ public Optional<String> username() {
public Optional<String> password() {
return empty();
}

@Override
public Policy releases() {
return DEFAULT_REPOSITORY_POLICY;
}

@Override
public Policy snapshots() {
return DEFAULT_REPOSITORY_POLICY;
}
};

private final File jqassistantUserDir;
Expand Down Expand Up @@ -189,12 +213,17 @@ private static RemoteRepository selectMirror(RemoteRepository remoteRepository,
}

private static RemoteRepository getRemoteRepository(String id, Remote remote, ProxySelector proxySelector) {
RemoteRepository remoteRepository = new RemoteRepository.Builder(id, REPOSITORY_LAYOUT_DEFAULT, remote.url()).setSnapshotPolicy(
SNAPSHOT_REPOSITORY_POLICY)
RemoteRepository remoteRepository = new RemoteRepository.Builder(id, REPOSITORY_LAYOUT_DEFAULT, remote.url()) //
.setReleasePolicy(getRepositoryPolicy(remote.releases()))
.setSnapshotPolicy(getRepositoryPolicy(remote.snapshots()))
.build();
return getRemoteRepository(remoteRepository, proxySelector, remote.username(), remote.password());
}

private static RepositoryPolicy getRepositoryPolicy(Policy policy) {
return new RepositoryPolicy(policy.enabled(), policy.updatePolicy(), policy.checksumPolicy());
}

private static RemoteRepository getRemoteRepository(RemoteRepository remoteRepository, ProxySelector proxySelector, Optional<String> optionalUsername,
Optional<String> optionalPassword) {
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ void defaultMavenSettings() throws CliConfigurationException {
Remote publicRemote = remotes.get("public");
assertThat(publicRemote).isNotNull();
assertThat(publicRemote.url()).isEqualTo("https://public-repo.acme.com/");
Policy releasesPolicy = publicRemote.releases();
assertThat(releasesPolicy.enabled()).isTrue();
assertThat(releasesPolicy.updatePolicy()).isEqualTo("never");
assertThat(releasesPolicy.checksumPolicy()).isEqualTo("ignore");
Policy snapshotsPolicy = publicRemote.snapshots();
assertThat(snapshotsPolicy.enabled()).isFalse();
assertThat(snapshotsPolicy.updatePolicy()).isEqualTo("always");
assertThat(snapshotsPolicy.checksumPolicy()).isEqualTo("fail");

Remote privateRemote = remotes.get("private-repo");
assertThat(privateRemote).isNotNull();
Expand Down
10 changes: 10 additions & 0 deletions cli/application/src/test/resources/userhome/.m2/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
<repository>
<id>public</id>
<url>https://public-repo.acme.com/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
Expand Down
30 changes: 30 additions & 0 deletions cli/src/main/asciidoc/readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ jqassistant:
# -Djqassistant.repositories.central.password (optional)
password:
# The repository policy for releases (optional)
releases:
# If true then this repository is used to resolve releases.
#
# -Djqassistant.repositories.central.releases.enabled
enabled: true
# Determines the update policy: never|daily|always|interval:X (in minutes)
#
# -Djqassistant.repositories.central.releases.update-policy
update-policy: daily
# Determines the checksum policy: fail|warn|ignore
#
# -Djqassistant.repositories.central.releases.checksum-policy
checksum-policy: warn
# The repository policy for snapshots (optional)
snapshots:
# If true then this repository is used to resolve releases.
#
# -Djqassistant.repositories.central.snapshots.enabled
enabled: true
# Determines the update policy: never|daily|always|interval:X (in minutes)
#
# -Djqassistant.repositories.central.snapshots.update-policy
update-policy: daily
# Determines the checksum policy: fail|warn|ignore
#
# -Djqassistant.repositories.central.snapshots.checksum-policy
checksum-policy: warn
# If true (default), ignore any repositories specified by transitive dependencies.
#
# -Djqassistant.repositories.ignore-transitive-repositories (optional)
Expand Down

0 comments on commit d83c25c

Please sign in to comment.