Skip to content

Commit

Permalink
[backport] 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 5a8d666 commit 907d87c
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 151 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
Loading

0 comments on commit 907d87c

Please sign in to comment.