Skip to content

Commit

Permalink
Refactor some usages of TychoProjectUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Dec 11, 2023
1 parent a350281 commit 7cd82de
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 75 deletions.
2 changes: 2 additions & 0 deletions tycho-api/src/main/java/org/eclipse/tycho/TychoConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface TychoConstants {

public static final String ECLIPSE_LATEST = "https://download.eclipse.org/releases/2023-12/";

public static final String TYCHO_NOT_CONFIGURED = "Tycho build extension not configured for ";

static final String ANY_QUALIFIER = "qualifier";

static final boolean USE_SMART_BUILDER = Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.TychoProject;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.osgitools.OsgiBundleProject;
import org.eclipse.tycho.core.utils.TychoProjectUtils;

/**
* This mojo could be added to a build if validation of the classpath is desired before the
Expand All @@ -48,7 +48,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
TychoProject projectType = projectTypes.get(project.getPackaging());
if (projectType instanceof OsgiBundleProject bundleProject) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
if (TychoProjectUtils.getOptionalDependencyArtifacts(reactorProject).isPresent()) {
if (reactorProject.getContextValue(TychoConstants.CTX_DEPENDENCY_ARTIFACTS) != null) {
bundleProject.getClasspath(reactorProject);
} else {
getLog().info("Skipped classpath validation as project is currently not resolved");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
Expand All @@ -31,9 +30,13 @@
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.repository.WorkspaceRepository;
import org.eclipse.tycho.*;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.DependencyArtifacts;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.utils.TychoProjectUtils;

@Component(role = WorkspaceReader.class, hint = "TychoWorkspaceReader")
public class TychoWorkspaceReader implements MavenWorkspaceReader {
Expand All @@ -48,6 +51,9 @@ public class TychoWorkspaceReader implements MavenWorkspaceReader {
@Requirement
private ModelWriter modelWriter;

@Requirement
private TychoProjectManager projectManager;

public TychoWorkspaceReader() {
repository = new WorkspaceRepository("tycho", null);
}
Expand Down Expand Up @@ -137,13 +143,12 @@ private File findP2Artifact(final Artifact artifact) {
if (session != null) {
final MavenProject currentProject = session.getCurrentProject();
final ReactorProject reactorProject = DefaultReactorProject.adapt(currentProject);
final Optional<DependencyArtifacts> dependencyMetadata =
TychoProjectUtils.getOptionalDependencyArtifacts(reactorProject);

if (dependencyMetadata.isPresent()) {
final DependencyArtifacts dependencyMetadata = (DependencyArtifacts) reactorProject
.getContextValue(TychoConstants.CTX_DEPENDENCY_ARTIFACTS);
if (dependencyMetadata != null) {
logger.debug("Attempting to resolve " + artifact + " for project " + currentProject);

for (final ArtifactDescriptor descriptor : dependencyMetadata.get().getArtifacts()) {
for (final ArtifactDescriptor descriptor : dependencyMetadata.getArtifacts()) {
if (isArtifactMatch(descriptor.getKey(), artifact)) {
return descriptor.getLocation(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ protected static ReactorProject getCachedValue(MavenProject project) {
}

public static List<ReactorProject> adapt(MavenSession session) {
if (session == null) {
return List.of();
}
ArrayList<ReactorProject> result = new ArrayList<>();
for (MavenProject project : session.getProjects()) {
ReactorProject reactorProject = adapt(project, session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.eclipse.tycho.p2.publisher.AuthoredIUAction;
import org.eclipse.tycho.p2.resolver.ResolverException;
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;
import org.eclipse.tycho.targetplatform.P2TargetPlatform;

public class P2ResolverImpl implements P2Resolver {
Expand All @@ -88,13 +89,13 @@ public class P2ResolverImpl implements P2Resolver {

private final List<IRequirement> additionalRequirements = new ArrayList<>();

private TargetPlatformFactoryImpl targetPlatformFactory;
private TargetPlatformFactory targetPlatformFactory;

private PomDependencies pomDependencies = PomDependencies.ignore;

private P2ResolverFactoryImpl p2ResolverFactoryImpl;

public P2ResolverImpl(TargetPlatformFactoryImpl targetPlatformFactory, P2ResolverFactoryImpl p2ResolverFactoryImpl,
public P2ResolverImpl(TargetPlatformFactory targetPlatformFactory, P2ResolverFactoryImpl p2ResolverFactoryImpl,
MavenLogger logger, Collection<TargetEnvironment> environments) {
this.targetPlatformFactory = targetPlatformFactory;
this.p2ResolverFactoryImpl = p2ResolverFactoryImpl;
Expand Down Expand Up @@ -162,7 +163,8 @@ public Map<TargetEnvironment, P2ResolutionResult> resolveArtifactDependencies(Ta
@Override
public P2ResolutionResult resolveMetadata(TargetPlatformConfigurationStub tpConfiguration,
ExecutionEnvironmentConfiguration eeConfig) {
P2TargetPlatform contextImpl = targetPlatformFactory.createTargetPlatform(tpConfiguration, eeConfig, null);
P2TargetPlatform contextImpl = (P2TargetPlatform) targetPlatformFactory.createTargetPlatform(tpConfiguration,
eeConfig, null);

ResolutionDataImpl data = new ResolutionDataImpl(contextImpl.getEEResolutionHints());
data.setAvailableIUs(contextImpl.getInstallableUnits());
Expand All @@ -189,7 +191,7 @@ public P2ResolutionResult resolveMetadata(TargetPlatformConfigurationStub tpConf
@Override
public P2ResolutionResult getTargetPlatformAsResolutionResult(TargetPlatformConfigurationStub tpConfiguration,
String eeName) {
P2TargetPlatform targetPlatform = targetPlatformFactory.createTargetPlatform(tpConfiguration,
P2TargetPlatform targetPlatform = (P2TargetPlatform) targetPlatformFactory.createTargetPlatform(tpConfiguration,
new ExecutionEnvironmentConfigurationStub(eeName), null);

MetadataOnlyP2ResolutionResult result = new MetadataOnlyP2ResolutionResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -32,6 +33,7 @@
import org.eclipse.tycho.core.shared.MavenLogger;
import org.eclipse.tycho.p2.repository.LocalArtifactRepository;
import org.eclipse.tycho.p2.repository.LocalMetadataRepository;
import org.eclipse.tycho.p2.repository.ProviderOnlyArtifactRepository;

public class PreliminaryTargetPlatformImpl extends TargetPlatformBaseImpl {

Expand All @@ -53,6 +55,8 @@ public class PreliminaryTargetPlatformImpl extends TargetPlatformBaseImpl {

private final boolean includeLocalRepo;

private IArtifactRepository artifactRepository;

public PreliminaryTargetPlatformImpl(Map<IInstallableUnit, ReactorProjectIdentities> reactorProjectIUs,
Collection<IInstallableUnit> externalIUs, ExecutionEnvironmentResolutionHints executionEnvironment,
TargetPlatformFilterEvaluator filter, LocalMetadataRepository localMetadataRepository,
Expand All @@ -65,6 +69,7 @@ public PreliminaryTargetPlatformImpl(Map<IInstallableUnit, ReactorProjectIdentit
this.localMetadataRepository = localMetadataRepository;
this.includeLocalRepo = includeLocalRepo;
this.logger = logger;
this.artifactRepository = new ProviderOnlyArtifactRepository(artifacts, null, URI.create("preliminary:/"));
}

public static LinkedHashSet<IInstallableUnit> collectAllInstallableUnits(
Expand Down Expand Up @@ -128,8 +133,7 @@ public IMetadataRepository getMetadataRepository() {

@Override
public IArtifactRepository getArtifactRepository() {
// the preliminary TP shall not be used to create build results, so this method is not needed
throw new UnsupportedOperationException();
return artifactRepository;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.tycho.core.TargetPlatformConfiguration;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.utils.TychoProjectUtils;
import org.eclipse.tycho.p2.repository.RepositoryBlackboardKey;
import org.eclipse.tycho.p2.tools.RepositoryReferences;

Expand Down Expand Up @@ -114,8 +113,8 @@ private void addTargetPlatformRepository(RepositoryReferences sources, MavenSess
repositoryLocation.mkdirs();
try (FileOutputStream stream = new FileOutputStream(new File(repositoryLocation, "content.xml"))) {

ReactorProject reactorProject = DefaultReactorProject.adapt(project);
TargetPlatform targetPlatform = TychoProjectUtils.getTargetPlatform(reactorProject);
TargetPlatform targetPlatform = projectManager.getTargetPlatform(project)
.orElseThrow(() -> new MojoFailureException(TychoConstants.TYCHO_NOT_CONFIGURED + project));

TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(project);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.DependencyArtifacts;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.TychoProject;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.utils.TychoProjectUtils;
import org.eclipse.tycho.p2maven.InstallableUnitGenerator;

/**
Expand All @@ -60,28 +60,31 @@ public class DependenciesTreeMojo extends AbstractMojo {
@Component
private LegacySupport legacySupport;

@Component
private TychoProjectManager projectManager;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
//TODO maybe we can compute a org.apache.maven.shared.dependency.graph.DependencyNode and reuse org.apache.maven.plugins.dependency.tree.TreeMojo wich has a getSerializingDependencyNodeVisitor

ReactorProject reactorProject = DefaultReactorProject.adapt(project);
Optional<DependencyArtifacts> optional = TychoProjectUtils.getOptionalDependencyArtifacts(reactorProject);
if (optional.isEmpty()) {
Optional<TychoProject> tychoProject = projectManager.getTychoProject(project);
if (tychoProject.isEmpty()) {
return;
}

Set<String> written = new HashSet<String>();
written.add(project.getId());
getLog().info(project.getId());

List<ArtifactDescriptor> artifacts = optional.get().getArtifacts();
List<ArtifactDescriptor> artifacts = tychoProject.get()
.getDependencyArtifacts(DefaultReactorProject.adapt(project)).getArtifacts();
Map<IInstallableUnit, Set<ReactorProject>> projectMap = artifacts.stream()
.filter(a -> a.getMavenProject() != null).flatMap(a -> {
return a.getInstallableUnits().stream().map(iu -> new SimpleEntry<>(iu, a.getMavenProject()));
})
.collect(Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toSet())));
Set<IInstallableUnit> units = artifacts.stream().flatMap(d -> d.getInstallableUnits().stream())
.collect(Collectors.toCollection(HashSet::new));
reactorProject.getDependencyMetadata();
List<IInstallableUnit> initial;
try {
initial = new ArrayList<IInstallableUnit>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.MatchPattern;
import org.eclipse.tycho.FileLockService;
Expand Down Expand Up @@ -332,9 +333,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

reactorProject.setContextValue(TychoConstants.CTX_METADATA_ARTIFACT_LOCATION, categoriesDirectory);
RepositoryReferences sources = repositoryReferenceTool.getVisibleRepositories(getProject(), getSession(),
MavenProject project = getProject();
RepositoryReferences sources = repositoryReferenceTool.getVisibleRepositories(project, getSession(),
RepositoryReferenceTool.REPOSITORIES_INCLUDE_CURRENT_MODULE);
sources.setTargetPlatform(TychoProjectUtils.getTargetPlatform(getReactorProject()));
sources.setTargetPlatform(projectManager.getTargetPlatform(project)
.orElseThrow(() -> new MojoFailureException(TychoConstants.TYCHO_NOT_CONFIGURED + project)));

List<RepositoryReference> repositoryReferences = getCategories(categoriesDirectory).stream()//
.map(Category::getRepositoryReferences)//
Expand All @@ -344,14 +347,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Predicate<String> autoReferencesFilter = buildRepositoryReferenceLocationFilter();
List<RepositoryReference> autoRepositoryRefeferences = new ArrayList<>();
if (addPomRepositoryReferences) {
getProject().getRepositories().stream() //
project.getRepositories().stream() //
.filter(pomRepo -> "p2".equals(pomRepo.getLayout()))
.filter(pomRepo -> autoReferencesFilter.test(pomRepo.getUrl()))
.map(pomRepo -> new RepositoryReference(pomRepo.getName(), pomRepo.getUrl(), true))
.forEach(autoRepositoryRefeferences::add);
}
if (addIUTargetRepositoryReferences) {
projectManager.getTargetPlatformConfiguration(getProject()).getTargets().stream()
projectManager.getTargetPlatformConfiguration(project).getTargets().stream()
.flatMap(tpFile -> tpFile.getLocations().stream())
.filter(InstallableUnitLocation.class::isInstance).map(InstallableUnitLocation.class::cast)
.flatMap(iu -> iu.getRepositories().stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.codehaus.plexus.archiver.util.DefaultFileSet;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.utils.TychoProjectUtils;
import org.eclipse.tycho.model.IU;

/**
Expand Down Expand Up @@ -56,6 +56,9 @@ public class PackageIUMojo extends AbstractTychoPackagingMojo {
@Component(role = Archiver.class, hint = "zip")
private ZipArchiver zipArchiver;

@Component
private TychoProjectManager projectManager;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
synchronized (LOCK) {
Expand Down Expand Up @@ -101,8 +104,7 @@ private void expandVersions(IU iu) throws MojoFailureException {
iuTransformer.replaceQualifierInCapabilities(iu.getProvidedCapabilites(),
DefaultReactorProject.adapt(project).getBuildQualifier());

TargetPlatform targetPlatform = TychoProjectUtils
.getTargetPlatformIfAvailable(DefaultReactorProject.adapt(project));
TargetPlatform targetPlatform = projectManager.getTargetPlatform(project).orElse(null);
if (targetPlatform == null) {
getLog().warn(
"Skipping version reference expansion in p2iu project using the deprecated -Dtycho.targetPlatform configuration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.resolver.P2ResolutionResult;
import org.eclipse.tycho.core.resolver.P2ResolutionResult.Entry;
import org.eclipse.tycho.core.resolver.P2Resolver;
import org.eclipse.tycho.core.resolver.P2ResolverFactory;
import org.eclipse.tycho.core.utils.TychoProjectUtils;
import org.eclipse.tycho.model.Feature;
import org.eclipse.tycho.model.FeatureRef;
import org.eclipse.tycho.model.PluginRef;
Expand Down Expand Up @@ -224,6 +225,9 @@ public enum MissingSourcesAction {
@Component
private Logger logger;

@Component
private TychoProjectManager projectManager;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!PackagingType.TYPE_ECLIPSE_FEATURE.equals(project.getPackaging()) || skip) {
Expand Down Expand Up @@ -321,8 +325,8 @@ private File generateSourceFeatureXml(Properties mergedSourceFeatureProps, Prope

final Feature sourceFeature = createSourceFeatureSkeleton(feature, mergedSourceFeatureProps,
sourceTemplateProps);
fillReferences(sourceFeature, feature,
TychoProjectUtils.getTargetPlatform(DefaultReactorProject.adapt(project)));
fillReferences(sourceFeature, feature, projectManager.getTargetPlatform(project)
.orElseThrow(() -> new MojoExecutionException(TychoConstants.TYCHO_NOT_CONFIGURED + project)));

Feature.write(sourceFeature, sourceFeatureXml, " ");
return sourceFeatureXml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,9 @@ public Collection<IRequirement> getAdditionalRequirements() {
}

};
DependencyArtifacts testRuntimeArtifacts = dependencyResolver.resolveDependencies(session, project, null,
DependencyArtifacts testRuntimeArtifacts = dependencyResolver.resolveDependencies(session, project,
projectManager.getTargetPlatform(project)
.orElseThrow(() -> new MojoExecutionException(TychoConstants.TYCHO_NOT_CONFIGURED + project)),
getReactorProjects(), resolverConfiguration, getTestTargetEnvironments());
if (testRuntimeArtifacts == null) {
throw new MojoExecutionException(
Expand Down
Loading

0 comments on commit 7cd82de

Please sign in to comment.