From 02e406ba4676cfbaa08074801c83b4b56b5590b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Tue, 8 Nov 2022 16:00:14 +0100 Subject: [PATCH] Support resolving of target projects from the reactor Currently resolving an artifact from a target-project fails if the project is not "packed" yet. With a custom workspace reader we can help maven to solve the issue. --- .../maven/TargetPlatformWorkspaceReader.java | 71 +++++++++++++++++++ .../TargetPlatformArtifactResolver.java | 64 +++++++++++------ 2 files changed, 114 insertions(+), 21 deletions(-) create mode 100644 tycho-core/src/main/java/org/eclipse/tycho/core/maven/TargetPlatformWorkspaceReader.java diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TargetPlatformWorkspaceReader.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TargetPlatformWorkspaceReader.java new file mode 100644 index 0000000000..c3be93994e --- /dev/null +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TargetPlatformWorkspaceReader.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.tycho.core.maven; + +import java.io.File; +import java.util.List; +import java.util.Optional; + +import org.apache.maven.plugin.LegacySupport; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.repository.WorkspaceReader; +import org.eclipse.aether.repository.WorkspaceRepository; +import org.eclipse.tycho.targetplatform.TargetPlatformArtifactResolver; +import org.eclipse.tycho.targetplatform.TargetResolveException; + +/** + * This component allows to resolve target artifacts from the reactor. Maven itself only supports a + * limited way of resolving types in the reactor and do not know how to handle a "target type" + * project. + */ +@Component(role = WorkspaceReader.class, hint = "TargetPlatformWorkspaceReader") +public class TargetPlatformWorkspaceReader implements WorkspaceReader { + + @Requirement + private TargetPlatformArtifactResolver platformArtifactResolver; + @Requirement + private LegacySupport legacySupport; + private WorkspaceRepository repository; + + public TargetPlatformWorkspaceReader() { + repository = new WorkspaceRepository("tycho-target-platform", null); + } + + @Override + public WorkspaceRepository getRepository() { + return repository; + } + + @Override + public File findArtifact(Artifact artifact) { + if (TargetPlatformArtifactResolver.TARGET_TYPE.equals(artifact.getExtension())) { + try { + Optional targetFile = platformArtifactResolver.getReactorTargetFile(artifact.getGroupId(), + artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier(), + legacySupport.getSession()); + return targetFile.orElse(null); + } catch (TargetResolveException e) { + // something went wrong, so we can't find the requested artifact here... + } + } + return null; + } + + @Override + public List findVersions(Artifact artifact) { + return List.of(); + } + +} 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 07291d347d..7261dac1ce 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 @@ -14,6 +14,7 @@ import java.io.File; import java.util.List; +import java.util.Optional; import org.apache.commons.io.FilenameUtils; import org.apache.maven.artifact.Artifact; @@ -32,7 +33,7 @@ @Component(role = TargetPlatformArtifactResolver.class) public class TargetPlatformArtifactResolver { - private static final String TARGET_TYPE = "target"; + public static final String TARGET_TYPE = "target"; @Requirement private RepositorySystem repositorySystem; @@ -53,10 +54,44 @@ public class TargetPlatformArtifactResolver { 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 - for (MavenProject project : session.getProjects()) { + 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); + } + + /** + * 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 + */ + 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) { + if (classifier == null || classifier.isBlank()) { File[] targetFiles = TargetDefinitionFile .listTargetFiles(project.getBasedir()); if (targetFiles == null || targetFiles.length == 0) { @@ -64,12 +99,12 @@ public File resolveTargetFile(String groupId, String artifactId, String version, "No target definition file(s) found in project '" + project.getName() + "'."); } if (targetFiles.length == 1) { - return targetFiles[0]; + return Optional.of(targetFiles[0]); } for (File targetFile : targetFiles) { String baseName = FilenameUtils.getBaseName(targetFile.getName()); if (baseName.equalsIgnoreCase(project.getArtifactId())) { - return targetFile; + return Optional.of(targetFile); } } throw new TargetResolveException("One target file must be named '" + project.getArtifactId() @@ -78,7 +113,7 @@ public File resolveTargetFile(String groupId, String artifactId, String version, File target = new File(project.getBasedir(), classifier + TargetDefinitionFile.FILE_EXTENSION); if (TargetDefinitionFile.isTargetFile(target)) { - return target; + return Optional.of(target); } else { throw new TargetResolveException("target definition file '" + target + "' not found in project '" + project.getName() + "'."); @@ -86,20 +121,7 @@ public File resolveTargetFile(String groupId, String artifactId, String version, } } } - // 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); - } + return Optional.empty(); + } }