Skip to content

Commit

Permalink
Use Maven Resolver API v2
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman committed May 28, 2024
1 parent a21bfee commit 5771a53
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.oracle.bedrock.runtime.console.CapturingApplicationConsole;
import com.oracle.bedrock.runtime.java.JavaApplication;
import com.oracle.bedrock.runtime.java.options.ClassName;
import com.oracle.bedrock.runtime.java.options.SystemProperty;
import com.oracle.bedrock.runtime.options.Console;
import org.junit.Test;

Expand Down
8 changes: 7 additions & 1 deletion bedrock-runtime-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@

<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-http</artifactId>
<artifactId>maven-resolver-supplier-mvn3</artifactId>
<version>${maven.resolver.version}</version>
</dependency>

<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-jdk-11</artifactId>
<version>${maven.resolver.version}</version>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,42 @@
import com.oracle.bedrock.runtime.java.ClassPath;
import com.oracle.bedrock.runtime.java.JavaApplication;
import com.oracle.bedrock.runtime.options.PlatformSeparators;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.DefaultSettingsBuilderFactory;
import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
import org.apache.maven.settings.building.SettingsBuilder;
import org.apache.maven.settings.building.SettingsBuildingException;
import org.eclipse.aether.DefaultRepositoryCache;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositoryException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResult;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.artifact.decorator.ArtifactDecorator;
import org.eclipse.aether.spi.artifact.decorator.ArtifactDecoratorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.transport.file.FileTransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;
import org.eclipse.aether.supplier.RepositorySystemSupplier;
import org.eclipse.aether.supplier.SessionBuilderSupplier;
import org.eclipse.aether.transport.jdk.JdkTransporterFactory;
import org.eclipse.aether.util.artifact.JavaScopes;
import org.eclipse.aether.util.filter.DependencyFilterUtils;
import org.eclipse.aether.version.Version;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -230,32 +231,44 @@ private Settings getSettings(OptionsByType optionsByType)
*/
private RepositorySystem newRepositorySystem()
{
/*
* Aether's components implement org.eclipse.aether.spi.locator.Service to ease manual wiring and using the
* pre-populated DefaultServiceLocator, we only need to register the repository connector and transporter
* factories.
*/
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();

locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, FileTransporterFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);

locator.setErrorHandler(new DefaultServiceLocator.ErrorHandler()
{
@Override
public void serviceCreationFailed(Class<?> type,
Class<?> impl,
Throwable exception)
{
exception.printStackTrace();
}
});

return locator.getService(RepositorySystem.class);
}
return new RepositorySystemSupplier() {
@Override
protected Map<String, ArtifactDecoratorFactory> createArtifactDecoratorFactories() {
Map<String, ArtifactDecoratorFactory> result = super.createArtifactDecoratorFactories();
result.put("color", new ArtifactDecoratorFactory() {
@Override
public ArtifactDecorator newInstance(RepositorySystemSession session) {
return new ArtifactDecorator() {
@Override
public Artifact decorateArtifact(ArtifactDescriptorResult artifactDescriptorResult) {
Map<String, String> properties = new HashMap<>(
artifactDescriptorResult.getArtifact().getProperties());
properties.put("color", "red");
return artifactDescriptorResult.getArtifact().setProperties(properties);
}
};
}

@Override
public float getPriority() {
return 0;
}
});
return result;
}

@Override
protected Map<String, TransporterFactory> createTransporterFactories() {
Map<String, TransporterFactory> result = super.createTransporterFactories();
result.put(
JdkTransporterFactory.NAME,
new JdkTransporterFactory(getChecksumExtractor(), getPathProcessor()));
return result;
}
}.get();
}

@SuppressWarnings("deprecation")
@Override
public void onLaunching(Platform platform,
MetaClass metaClass,
Expand All @@ -267,14 +280,22 @@ public void onLaunching(Platform platform,
perform(
(system, session, repositories, scope) -> {

// we only filter based on the scope
// we only filter based on the scope
DependencyFilter filter = DependencyFilterUtils.classpathFilter(scope);

// collect class paths for each resolved artifact
LinkedHashSet<ClassPath> artifactPaths = new LinkedHashSet<>();

for (Artifact artifact : artifacts.values())
{
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(repositories);

ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
Artifact actualArtifact = artifactResult.getArtifact();
artifactPaths.add(ClassPath.ofPath(actualArtifact.getPath()));

CollectRequest collectRequest = new CollectRequest();

collectRequest.setRoot(new Dependency(artifact, scope));
Expand All @@ -285,10 +306,10 @@ public void onLaunching(Platform platform,
List<ArtifactResult> artifactResults = system.resolveDependencies(session,
dependencyRequest)
.getArtifactResults();

for (ArtifactResult artifactResult : artifactResults)
//artifactPaths.add(ClassPath.ofPath(artifact.getPath()));
for (ArtifactResult dependencyResult : artifactResults)
{
artifactPaths.add(ClassPath.ofFile(artifactResult.getArtifact().getFile()));
artifactPaths.add(ClassPath.ofPath(dependencyResult.getArtifact().getPath()));
}
}

Expand Down Expand Up @@ -753,19 +774,24 @@ private void perform(RepositorySystemOperation operation,
RepositorySystem system = newRepositorySystem();

// ----- establish the session for the repository system -----
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
RepositorySystemSession.SessionBuilder sessionBuilder = new SessionBuilderSupplier(system).get();

session.setOffline(isOffline == null ? false : isOffline);
sessionBuilder.setOffline(isOffline == null ? false : isOffline);

session.setCache(new DefaultRepositoryCache());
sessionBuilder.setCache(new DefaultRepositoryCache());

// define the local repository
File localRepositoryLocation = new File(System.getProperty("user.home") + separators.getFileSeparator() + ".m2"
+ separators.getFileSeparator() + "repository");

LocalRepository localRepo = new LocalRepository(localRepositoryLocation);

session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));

sessionBuilder.withLocalRepositoryBaseDirectories(localRepositoryLocation.toPath());
RepositorySystemSession.CloseableSession session = sessionBuilder.build();


sessionBuilder.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));

// ----- establish the remote repositories to use from the settings -----

Expand Down
3 changes: 2 additions & 1 deletion bedrock-runtime-maven/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
requires org.apache.maven.resolver.impl;
requires org.apache.maven.resolver.spi;
requires org.apache.maven.resolver.transport.file;
requires org.apache.maven.resolver.transport.http;
requires org.apache.maven.resolver.transport.jdk;
requires org.apache.maven.resolver.util;
requires maven.resolver.provider;
requires org.apache.maven.resolver.supplier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
Expand Down Expand Up @@ -846,6 +847,26 @@ public static ClassPath ofFile(File file)
}


/**
* Obtains a {@link ClassPath} containing only absolute path of the specified File
*
* @param path the Path to the artifact
*
* @return a {@link ClassPath} of a file
*/
public static ClassPath ofPath(Path path)
{
if (path == null)
{
throw new NullPointerException("Path must not be null");
}
else
{
return new ClassPath(path.toString());
}
}


/**
* Obtains Java System class-path.
*
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<maven.dependency-check.plugin.version>8.2.1</maven.dependency-check.plugin.version>

<!-- dependency versions -->
<maven.resolver.version>1.9.8</maven.resolver.version>
<maven.resolver.version>2.0.0-alpha-11</maven.resolver.version>
<glassfish.el.version>5.0.0-M1</glassfish.el.version>
<hamcrest.version>2.2</hamcrest.version>
<jakarta.el.version>5.0.0-RC1</jakarta.el.version>
Expand Down

0 comments on commit 5771a53

Please sign in to comment.