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

[MRESOLVER-432] Introduce Session Supplier similar to System Supplier #362

Merged
merged 5 commits into from
Nov 14, 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 @@ -22,6 +22,7 @@
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.DependencyGraphTransformer;
Expand Down Expand Up @@ -94,6 +95,11 @@ interface CloseableSession extends RepositorySystemSession, Closeable {
/**
* Builder for building {@link CloseableSession} instances. Builder instances can be created with
* {@link RepositorySystem#createSessionBuilder()} method. Instances are not thread-safe nor immutable.
* <p>
* Important: if you set a stateful member on builder (for example {@link SessionData} or {@link RepositoryCache}),
* the builder will create session instances using same provided stateful members, that may lead to unexpected side
* effects. Solution for these cases is to not reuse builder instances, or, keep reconfiguring it, or ultimately
* provide suppliers that create new instance per each call.
*
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
Expand Down Expand Up @@ -384,6 +390,14 @@ interface SessionBuilder {
*/
SessionBuilder setData(SessionData data);

/**
* Sets the custom session data supplier associated with this session.
*
* @param dataSupplier The session data supplier, may not be {@code null}.
* @return This session for chaining, never {@code null}.
*/
SessionBuilder setSessionDataSupplier(Supplier<SessionData> dataSupplier);

/**
* Sets the cache the repository system may use to save data for future reuse during the session.
*
Expand All @@ -392,14 +406,22 @@ interface SessionBuilder {
*/
SessionBuilder setCache(RepositoryCache cache);

/**
* Sets the cache supplier for the repository system may use to save data for future reuse during the session.
*
* @param cacheSupplier The repository cache supplier, may not be {@code null}.
* @return This session for chaining, never {@code null}.
*/
SessionBuilder setRepositoryCacheSupplier(Supplier<RepositoryCache> cacheSupplier);

/**
* Shortcut method to set up local repository manager directly onto builder. There must be at least one non-null
* {@link File} passed in this method. In case multiple files, session builder will use chained local repository
* manager.
*
* @param baseDirectories The local repository base directories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
* @see #withLocalRepositories(LocalRepository...)
*/
SessionBuilder withLocalRepositoryBaseDirectories(File... baseDirectories);

Expand All @@ -410,7 +432,7 @@ interface SessionBuilder {
*
* @param baseDirectories The local repository base directories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
* @see #withLocalRepositories(List)
*/
SessionBuilder withLocalRepositoryBaseDirectories(List<File> baseDirectories);

Expand All @@ -421,7 +443,6 @@ interface SessionBuilder {
*
* @param localRepositories The local repositories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
*/
SessionBuilder withLocalRepositories(LocalRepository... localRepositories);

Expand All @@ -432,7 +453,6 @@ interface SessionBuilder {
*
* @param localRepositories The local repositories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
*/
SessionBuilder withLocalRepositories(List<LocalRepository> localRepositories);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import java.util.Collections;

import org.apache.maven.resolver.examples.util.Booter;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
Expand Down Expand Up @@ -51,34 +52,34 @@ public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(DependencyHierarchyWithRanges.class.getSimpleName());

RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) {
SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system);
sessionBuilder.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_IGNORE); // to not bother with checksums
sessionBuilder.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
sessionBuilder.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
try (CloseableSession session = sessionBuilder.build()) {
// this artifact is in "remote" repository in src/main/remote-repository
Artifact artifact = new DefaultArtifact("org.apache.maven.resolver.demo.mresolver345:a:1.0");

DefaultRepositorySystemSession session = Booter.newRepositorySystemSession(system);
File remoteRepoBasedir = new File("src/main/remote-repository");

session.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_IGNORE); // to not bother with checksums
session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
session.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
descriptorRequest.setArtifact(artifact);
descriptorRequest.setRepositories(Collections.singletonList(new RemoteRepository.Builder(
"remote", "default", remoteRepoBasedir.toURI().toASCIIString())
.build()));
ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);

// this artifact is in "remote" repository in src/main/remote-repository
Artifact artifact = new DefaultArtifact("org.apache.maven.resolver.demo.mresolver345:a:1.0");
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRootArtifact(descriptorResult.getArtifact());
collectRequest.setDependencies(descriptorResult.getDependencies());
collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
collectRequest.setRepositories(descriptorRequest.getRepositories());

File remoteRepoBasedir = new File("src/main/remote-repository");
CollectResult collectResult = system.collectDependencies(session, collectRequest);

ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
descriptorRequest.setArtifact(artifact);
descriptorRequest.setRepositories(Collections.singletonList(new RemoteRepository.Builder(
"remote", "default", remoteRepoBasedir.toURI().toASCIIString())
.build()));
ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);

CollectRequest collectRequest = new CollectRequest();
collectRequest.setRootArtifact(descriptorResult.getArtifact());
collectRequest.setDependencies(descriptorResult.getDependencies());
collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
collectRequest.setRepositories(descriptorRequest.getRepositories());

CollectResult collectResult = system.collectDependencies(session, collectRequest);

collectResult.getRoot().accept(Booter.DUMPER_SOUT);
collectResult.getRoot().accept(Booter.DUMPER_SOUT);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import org.apache.maven.resolver.examples.util.Booter;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.deployment.DeployRequest;
Expand All @@ -43,27 +43,27 @@ public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(DeployArtifacts.class.getSimpleName());

RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
CloseableSession session =
Booter.newRepositorySystemSession(system).build()) {
Artifact jarArtifact =
new DefaultArtifact("test", "org.apache.maven.aether.examples", "", "jar", "0.1-SNAPSHOT");
jarArtifact = jarArtifact.setFile(new File("src/main/data/demo.jar"));

RepositorySystemSession session = Booter.newRepositorySystemSession(system);
Artifact pomArtifact = new SubArtifact(jarArtifact, "", "pom");
pomArtifact = pomArtifact.setFile(new File("pom.xml"));

Artifact jarArtifact =
new DefaultArtifact("test", "org.apache.maven.aether.examples", "", "jar", "0.1-SNAPSHOT");
jarArtifact = jarArtifact.setFile(new File("src/main/data/demo.jar"));
RemoteRepository distRepo = new RemoteRepository.Builder(
"org.apache.maven.aether.examples",
"default",
new File("target/dist-repo").toURI().toString())
.build();

Artifact pomArtifact = new SubArtifact(jarArtifact, "", "pom");
pomArtifact = pomArtifact.setFile(new File("pom.xml"));
DeployRequest deployRequest = new DeployRequest();
deployRequest.addArtifact(jarArtifact).addArtifact(pomArtifact);
deployRequest.setRepository(distRepo);

RemoteRepository distRepo = new RemoteRepository.Builder(
"org.apache.maven.aether.examples",
"default",
new File("target/dist-repo").toURI().toString())
.build();

DeployRequest deployRequest = new DeployRequest();
deployRequest.addArtifact(jarArtifact).addArtifact(pomArtifact);
deployRequest.setRepository(distRepo);

system.deploy(session, deployRequest);
system.deploy(session, deployRequest);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import org.apache.maven.resolver.examples.util.Booter;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.VersionRangeRequest;
Expand All @@ -43,20 +43,20 @@ public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(FindAvailableVersions.class.getSimpleName());

RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
CloseableSession session =
Booter.newRepositorySystemSession(system).build()) {
Artifact artifact = new DefaultArtifact("org.apache.maven.resolver:maven-resolver-util:[0,)");

RepositorySystemSession session = Booter.newRepositorySystemSession(system);
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(artifact);
rangeRequest.setRepositories(Booter.newRepositories(system, session));

Artifact artifact = new DefaultArtifact("org.apache.maven.resolver:maven-resolver-util:[0,)");
VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);

VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(artifact);
rangeRequest.setRepositories(Booter.newRepositories(system, session));
List<Version> versions = rangeResult.getVersions();

VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);

List<Version> versions = rangeResult.getVersions();

System.out.println("Available versions " + versions);
System.out.println("Available versions " + versions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.apache.maven.resolver.examples.util.Booter;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.VersionRangeRequest;
Expand All @@ -40,21 +40,21 @@ public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(FindNewestVersion.class.getSimpleName());

RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
CloseableSession session =
Booter.newRepositorySystemSession(system).build()) {
Artifact artifact = new DefaultArtifact("org.apache.maven.resolver:maven-resolver-util:[0,)");

RepositorySystemSession session = Booter.newRepositorySystemSession(system);
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(artifact);
rangeRequest.setRepositories(Booter.newRepositories(system, session));

Artifact artifact = new DefaultArtifact("org.apache.maven.resolver:maven-resolver-util:[0,)");
VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);

VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(artifact);
rangeRequest.setRepositories(Booter.newRepositories(system, session));
Version newestVersion = rangeResult.getHighestVersion();

VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);

Version newestVersion = rangeResult.getHighestVersion();

System.out.println(
"Newest version " + newestVersion + " from repository " + rangeResult.getRepository(newestVersion));
System.out.println(
"Newest version " + newestVersion + " from repository " + rangeResult.getRepository(newestVersion));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package org.apache.maven.resolver.examples;

import org.apache.maven.resolver.examples.util.Booter;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
Expand All @@ -44,28 +45,28 @@ public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(GetDependencyHierarchy.class.getSimpleName());

RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) {
SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system);
sessionBuilder.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
sessionBuilder.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
try (CloseableSession session = sessionBuilder.build()) {
Artifact artifact = new DefaultArtifact("org.apache.maven:maven-resolver-provider:3.6.1");

DefaultRepositorySystemSession session = Booter.newRepositorySystemSession(system);
ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
descriptorRequest.setArtifact(artifact);
descriptorRequest.setRepositories(Booter.newRepositories(system, session));
ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);

session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
session.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRootArtifact(descriptorResult.getArtifact());
collectRequest.setDependencies(descriptorResult.getDependencies());
collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
collectRequest.setRepositories(descriptorRequest.getRepositories());

Artifact artifact = new DefaultArtifact("org.apache.maven:maven-resolver-provider:3.6.1");
CollectResult collectResult = system.collectDependencies(session, collectRequest);

ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
descriptorRequest.setArtifact(artifact);
descriptorRequest.setRepositories(Booter.newRepositories(system, session));
ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);

CollectRequest collectRequest = new CollectRequest();
collectRequest.setRootArtifact(descriptorResult.getArtifact());
collectRequest.setDependencies(descriptorResult.getDependencies());
collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
collectRequest.setRepositories(descriptorRequest.getRepositories());

CollectResult collectResult = system.collectDependencies(session, collectRequest);

collectResult.getRoot().accept(Booter.DUMPER_SOUT);
collectResult.getRoot().accept(Booter.DUMPER_SOUT);
}
}
}
}
Loading