Skip to content

Commit

Permalink
Migrate TargetPlatformArtifactResolver to Aether/Resolver + JSR330
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Sep 8, 2024
1 parent cebc548 commit 458fff3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 56 deletions.
4 changes: 4 additions & 0 deletions tycho-targetplatform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,19 @@
import java.util.Optional;

import org.apache.commons.io.FilenameUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.execution.MavenSession;
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;

/**
* This component resolves a given target artifact to a target platform file
*
*/
@Component(role = TargetPlatformArtifactResolver.class)
public class TargetPlatformArtifactResolver {
public interface TargetPlatformArtifactResolver {

public static final String TARGET_TYPE = "target";

@Requirement
private RepositorySystem repositorySystem;

/**
/**
* Resolves the target file artifact with the given coordinates, session and
* remote repositories
*
Expand All @@ -51,28 +42,8 @@ public class TargetPlatformArtifactResolver {
* @return the target file for the specified artifact
* @throws TargetResolveException if resolving the target fails
*/
public File resolveTargetFile(String groupId, String artifactId, String version, String classifier,
MavenSession session, List<ArtifactRepository> remoteRepositories) throws TargetResolveException {
//check if target is part of reactor-build
Optional<File> reactorTargetFile = getReactorTargetFile(groupId, artifactId, version, classifier, session);
if (reactorTargetFile.isPresent()) {
return reactorTargetFile.get();
}
// resolve using maven
Artifact artifact = repositorySystem.createArtifactWithClassifier(groupId, artifactId, version, TARGET_TYPE,
classifier);

ArtifactResolutionRequest request = new ArtifactResolutionRequest();
request.setArtifact(artifact);
request.setLocalRepository(session.getLocalRepository());
request.setRemoteRepositories(remoteRepositories);
repositorySystem.resolve(request);

if (artifact.isResolved()) {
return artifact.getFile();
}
throw new TargetResolveException("Could not resolve target platform specification artifact " + artifact);
}
public File resolveTargetFile(String groupId, String artifactId, String version, String classifier,
MavenSession session, List<ArtifactRepository> remoteRepositories) throws TargetResolveException;

/**
* Lookup a given target artifact in the current reactor
Expand All @@ -87,30 +58,10 @@ public File resolveTargetFile(String groupId, String artifactId, String version,
* @throws TargetResolveException
*/
public Optional<File> getReactorTargetFile(String groupId, String artifactId, String version, String classifier,
MavenSession session) throws TargetResolveException {
for (MavenProject project : session.getProjects()) {
if (groupId.equals(project.getGroupId()) && artifactId.equals(project.getArtifactId())
&& version.equals(project.getVersion())) {
if (classifier == null || classifier.isBlank()) {
return Optional.of(getMainTargetFile(project));
} else {
File target = new File(project.getBasedir(),
classifier + TargetDefinitionFile.FILE_EXTENSION);
if (TargetDefinitionFile.isTargetFile(target)) {
return Optional.of(target);
} else {
throw new TargetResolveException("target definition file '" + target
+ "' not found in project '" + project.getName() + "'.");
}
}
}
}
return Optional.empty();
}
MavenSession session) throws TargetResolveException;

public static File getMainTargetFile(MavenProject project) throws TargetResolveException {
File[] targetFiles = TargetDefinitionFile
.listTargetFiles(project.getBasedir());
File[] targetFiles = TargetDefinitionFile.listTargetFiles(project.getBasedir());
if (targetFiles == null || targetFiles.length == 0) {
throw new TargetResolveException(
"No target definition file(s) found in project '" + project.getName() + "'.");
Expand All @@ -122,7 +73,7 @@ public static File getMainTargetFile(MavenProject project) throws TargetResolveE
String baseName = FilenameUtils.getBaseName(targetFile.getName());
if (baseName.equalsIgnoreCase(project.getArtifactId())) {
return targetFile;
}
}
}
throw new TargetResolveException("One target file must be named '" + project.getArtifactId()
+ TargetDefinitionFile.FILE_EXTENSION + "' when multiple targets are present");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*******************************************************************************
* Copyright (c) 2022 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation based on DefaultTargetPlatformConfigurationReader
*******************************************************************************/
package org.eclipse.tycho.targetplatform;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

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

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;

/**
* This component resolves a given target artifact to a target platform file
*
*/
@Singleton
@Named
public class TargetPlatformArtifactResolverImpl implements TargetPlatformArtifactResolver {

@Inject
private RepositorySystem repositorySystem;
@Inject
private RepositorySystemSession repositorySystemSession;

/**
* Resolves the target file artifact with the given coordinates, session and
* remote repositories
*
* @param groupId
* @param artifactId
* @param version
* @param classifier
* @param session
* @param remoteRepositories
* @return the target file for the specified artifact
* @throws TargetResolveException if resolving the target fails
*/
@Override
public File resolveTargetFile(String groupId, String artifactId, String version, String classifier,
MavenSession session, List<ArtifactRepository> remoteRepositories) throws TargetResolveException {
// check if target is part of reactor-build
Optional<File> reactorTargetFile = getReactorTargetFile(groupId, artifactId, version, classifier, session);
if (reactorTargetFile.isPresent()) {
return reactorTargetFile.get();
}
// resolve using maven
ArtifactRequest request = new ArtifactRequest();
DefaultArtifact artifact = new DefaultArtifact(groupId, artifactId, classifier, TARGET_TYPE, version);
request.setArtifact(artifact);
List<RemoteRepository> repos = new ArrayList<>(RepositoryUtils.toRepos(remoteRepositories));
repos.add(RepositoryUtils.toRepo(session.getLocalRepository()));
request.setRepositories(repos);
try {
ArtifactResult result = repositorySystem.resolveArtifact(repositorySystemSession, request);
File file = result.getArtifact().getFile();
if (file != null && file.exists()) {
return file;
}
} catch (ArtifactResolutionException e) {
throw new TargetResolveException("Could not resolve target platform specification artifact " + artifact, e);
}
throw new TargetResolveException("Could not resolve target platform specification artifact " + artifact);
}

/**
* Lookup a given target artifact in the current reactor
*
* @param groupId
* @param artifactId
* @param version
* @param classifier
* @param session
* @return an empty optional if no reactor project matches or an optional
* describing the local file of this target artifact in the reactor
* @throws TargetResolveException
*/
@Override
public Optional<File> getReactorTargetFile(String groupId, String artifactId, String version, String classifier,
MavenSession session) throws TargetResolveException {
for (MavenProject project : session.getProjects()) {
if (groupId.equals(project.getGroupId()) && artifactId.equals(project.getArtifactId())
&& version.equals(project.getVersion())) {
if (classifier == null || classifier.isBlank()) {
return Optional.of(TargetPlatformArtifactResolver.getMainTargetFile(project));
} else {
File target = new File(project.getBasedir(), classifier + TargetDefinitionFile.FILE_EXTENSION);
if (TargetDefinitionFile.isTargetFile(target)) {
return Optional.of(target);
} else {
throw new TargetResolveException("target definition file '" + target
+ "' not found in project '" + project.getName() + "'.");
}
}
}
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public TargetResolveException(String message) {
super(message);
}

public TargetResolveException(String message, Throwable cause) {
super(message, cause);
}

}

0 comments on commit 458fff3

Please sign in to comment.