Skip to content

Commit

Permalink
Migrate RepositoryArtifactCoordinateResolver to JSR330/Resolver API
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Sep 23, 2024
1 parent 118f09a commit 6c80860
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
4 changes: 4 additions & 0 deletions tycho-packaging-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,35 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.tycho.core.DependencyResolutionException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;

/**
* This resolves against the maven (local) repository if the jar carry a
* suitable pom.properties file...
*/
@Component(role = ArtifactCoordinateResolver.class, hint = "local")
@Singleton
@Named("local")
public class RepositoryArtifactCoordinateResolver implements ArtifactCoordinateResolver {

@Requirement
@Inject
private RepositorySystem repositorySystem;

@Requirement
@Inject
private Logger log;

@Override
Expand All @@ -63,42 +67,33 @@ public Optional<Dependency> resolve(Dependency dep, MavenProject project, MavenS
String version = properties.getProperty("version");
if (artifactId != null && groupId != null && version != null) {
String type = FilenameUtils.getExtension(path.getFileName().toString());
Dependency dependency = new Dependency();
dependency.setGroupId(groupId);
dependency.setArtifactId(artifactId);
dependency.setType(type);
dependency.setVersion(version);
Artifact artifact = repositorySystem.createDependencyArtifact(dependency);
ArtifactResolutionRequest request = new ArtifactResolutionRequest();
request.setArtifact(artifact);
request.setOffline(session.isOffline());
request.setLocalRepository(session.getLocalRepository());
repositorySystem.injectMirror(request.getRemoteRepositories(), session.getSettings().getMirrors());
repositorySystem.injectProxy(request.getRemoteRepositories(), session.getSettings().getProxies());
repositorySystem.injectAuthentication(request.getRemoteRepositories(),
session.getSettings().getServers());
ArtifactResolutionResult resolveResult = repositorySystem.resolve(request);
if (resolveResult.hasExceptions()) {
DependencyResolutionException exception = new DependencyResolutionException(
"Resolving " + artifact + " failed", resolveResult.getExceptions());
log.debug("Resolving " + artifact + " failed because of: " + exception, exception);
return Optional.empty();
Artifact artifact = new DefaultArtifact(groupId, artifactId, type, version);
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(RepositoryUtils.toRepos(project.getRemoteArtifactRepositories()));
ArtifactResult artifactResult = repositorySystem.resolveArtifact(session.getRepositorySession(),
artifactRequest);
if (artifactResult.isResolved()) {

} else {
log.debug("Resolving " + artifact + " failed because of: " + artifactResult.getExceptions());
return Optional.empty();
}
return resolveResult.getArtifacts().stream().filter(a -> a.getFile() != null).filter(a -> {
try {
return FileUtils.contentEquals(a.getFile(), path.toFile());
} catch (IOException e) {
return false;
}
}).map(a -> {
Dependency result = new Dependency();
result.setGroupId(a.getGroupId());
result.setArtifactId(a.getArtifactId());
result.setVersion(a.getVersion());
result.setType(a.getType());
return result;
}).findFirst();
return Optional.ofNullable(artifactResult.getArtifact()).filter(a -> a.getFile() != null)
.filter(a -> {
try {
return FileUtils.contentEquals(a.getFile(), path.toFile());
} catch (IOException e) {
return false;
}
}).map(a -> {
Dependency result = new Dependency();
result.setGroupId(a.getGroupId());
result.setArtifactId(a.getArtifactId());
result.setVersion(a.getVersion());
result.setType(a.getExtension());
return result;
});
}

} catch (Exception e) {
Expand Down

0 comments on commit 6c80860

Please sign in to comment.