From 6f6cabbc77b495900b6470685389c53e14a38bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 8 Sep 2024 19:49:31 +0200 Subject: [PATCH] Migrate TargetPlatformArtifactResolver to Aether/Resolver + JSR330 --- tycho-targetplatform/pom.xml | 4 + .../TargetPlatformArtifactResolver.java | 63 +-------- .../TargetPlatformArtifactResolverImpl.java | 120 ++++++++++++++++++ .../TargetResolveException.java | 4 + 4 files changed, 135 insertions(+), 56 deletions(-) create mode 100644 tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolverImpl.java diff --git a/tycho-targetplatform/pom.xml b/tycho-targetplatform/pom.xml index 1059297fae..1a8f78501e 100644 --- a/tycho-targetplatform/pom.xml +++ b/tycho-targetplatform/pom.xml @@ -37,6 +37,10 @@ org.codehaus.plexus plexus-component-metadata + + org.eclipse.sisu + sisu-maven-plugin + \ No newline at end of file diff --git a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java index 12afcbf124..6d5d5ead1a 100644 --- a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java +++ b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java @@ -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 * @@ -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 remoteRepositories) throws TargetResolveException { - //check if target is part of reactor-build - Optional 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 remoteRepositories) throws TargetResolveException; /** * Lookup a given target artifact in the current reactor @@ -87,30 +58,10 @@ public File resolveTargetFile(String groupId, String artifactId, String version, * @throws TargetResolveException */ public Optional 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() + "'."); @@ -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"); diff --git a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolverImpl.java b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolverImpl.java new file mode 100644 index 0000000000..92190d27ce --- /dev/null +++ b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolverImpl.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * 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.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; + + /** + * 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 remoteRepositories) throws TargetResolveException { + // check if target is part of reactor-build + Optional 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 repos = new ArrayList<>(RepositoryUtils.toRepos(remoteRepositories)); + repos.add(RepositoryUtils.toRepo(session.getLocalRepository())); + request.setRepositories(repos); + try { + ArtifactResult result = repositorySystem.resolveArtifact(session.getRepositorySession(), 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 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(); + } + +} diff --git a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetResolveException.java b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetResolveException.java index 09895a32a3..ef069e1f48 100644 --- a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetResolveException.java +++ b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetResolveException.java @@ -20,4 +20,8 @@ public TargetResolveException(String message) { super(message); } + public TargetResolveException(String message, Throwable cause) { + super(message, cause); + } + }