Skip to content

Commit

Permalink
Support resolving of target projects from the reactor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laeubi committed Nov 8, 2022
1 parent f6de0c4 commit 02e406b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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<File> 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<String> findVersions(Artifact artifact) {
return List.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -53,23 +54,57 @@ public class TargetPlatformArtifactResolver {
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
for (MavenProject project : session.getProjects()) {
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);
}

/**
* 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<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) {
if (classifier == null || classifier.isBlank()) {
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() + "'.");
}
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()
Expand All @@ -78,28 +113,15 @@ 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() + "'.");
}
}
}
}
// 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();
}

}

0 comments on commit 02e406b

Please sign in to comment.