Skip to content

Commit

Permalink
Cleanup TychoProjectUtils and use ProjectManager instead
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Dec 9, 2023
1 parent b193066 commit 40ca269
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 135 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-09/";

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 All @@ -157,9 +162,8 @@ private File findP2Artifact(final Artifact artifact) {
private boolean isArtifactMatch(final ArtifactKey artifactKey, final Artifact artifact) {
final String groupId = artifact.getGroupId();
final String type = groupId.substring(TychoConstants.P2_GROUPID_PREFIX.length()).replace('.', '-');
return artifactKey.getType().equals(type) &&
artifactKey.getId().equals(artifact.getArtifactId()) &&
artifactKey.getVersion().equals(artifact.getVersion());
return artifactKey.getType().equals(type) && artifactKey.getId().equals(artifact.getArtifactId())
&& artifactKey.getVersion().equals(artifact.getVersion());
}

private File findMavenArtifact(@SuppressWarnings("unused") final Artifact artifact) {
Expand All @@ -176,16 +180,8 @@ private File getFileForArtifact(final Artifact artifact) {
final RepositorySystemSession repositorySession = legacySupport.getRepositorySession();
final LocalRepository localRepository = repositorySession.getLocalRepository();
final File basedir = localRepository.getBasedir();
final String repositoryPath = "p2/osgi/bundle/" +
artifact.getArtifactId() +
"/" +
artifact.getVersion() +
"/" +
artifact.getArtifactId() +
"-" +
artifact.getVersion() +
"." +
artifact.getExtension();
final String repositoryPath = "p2/osgi/bundle/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/"
+ artifact.getArtifactId() + "-" + artifact.getVersion() + "." + artifact.getExtension();

return new File(basedir, repositoryPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.maven.MavenDependenciesResolver;
import org.eclipse.tycho.core.utils.TychoProjectUtils;

/**
* {@link MavenBundleResolver} helps in gathering bundles that are living in P2 and maven world and
Expand Down Expand Up @@ -71,7 +70,7 @@ public Optional<ResolvedArtifactKey> resolveMavenBundle(MavenProject project, Ma
if (project == null) {
return Optional.empty();
}
TargetPlatform tp = TychoProjectUtils.getTargetPlatformIfAvailable(DefaultReactorProject.adapt(project));
TargetPlatform tp = projectManager.getTargetPlatform(project).orElse(null);
String type = mavenArtifactKey.getType();
String resolvedType = PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE.equals(type) ? ArtifactType.TYPE_ECLIPSE_PLUGIN
: type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ public Optional<TargetPlatform> getTargetPlatform() throws DependencyResolutionE

@Override
public Optional<TargetPlatform> getTargetPlatform(ReactorProject project) throws DependencyResolutionException {
synchronized (project) {
Object contextValue = project.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY);
if (contextValue instanceof TargetPlatform) {
return Optional.of((TargetPlatform) contextValue);
}
return project.computeContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, () -> {
MavenSession session = legacySupport.getSession();
if (repositoryManager == null || session == null) {
return Optional.empty();
Expand All @@ -91,7 +87,7 @@ public Optional<TargetPlatform> getTargetPlatform(ReactorProject project) throws
TargetPlatform finalTargetPlatform = computeFinalTargetPlatform(project, upstreamProjects,
pomDependenciesCollector);
return Optional.ofNullable(finalTargetPlatform);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,21 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.eclipse.tycho.DependencyArtifacts;
import org.eclipse.tycho.PlatformPropertiesUtils;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.resolver.shared.DependencySeed;

public class TychoProjectUtils {
private static final String TYCHO_NOT_CONFIGURED = "Tycho build extension not configured for ";

public static final String TYCHO_ENV_OSGI_WS = "tycho.env.osgi.ws";
public static final String TYCHO_ENV_OSGI_OS = "tycho.env.osgi.os";
public static final String TYCHO_ENV_OSGI_ARCH = "tycho.env.osgi.arch";

public static Optional<DependencyArtifacts> getOptionalDependencyArtifacts(ReactorProject project) {
DependencyArtifacts resolvedDependencies = (DependencyArtifacts) project
.getContextValue(TychoConstants.CTX_DEPENDENCY_ARTIFACTS);
return Optional.ofNullable(resolvedDependencies);
}

/**
* Returns the final target platform of the given project.
*/
public static TargetPlatform getTargetPlatform(ReactorProject project) {
TargetPlatform targetPlatform = getTargetPlatformIfAvailable(project);
if (targetPlatform == null) {
throw new IllegalStateException(TYCHO_NOT_CONFIGURED + project.toString());
}
return targetPlatform;
}

/**
* Returns the final target platform of the given project, or <code>null</code> if the target
* platform is not available.
*
* Projects with -Dtycho.targetPlatform use the legacy LocalDependencyResolver, which doesn't
* provide the {@link TargetPlatform} interface.
*/
public static TargetPlatform getTargetPlatformIfAvailable(ReactorProject project) {
return (TargetPlatform) project.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY);
}

/**
* Returns the (editable) list of {@link DependencySeed}s for the given project.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
Expand Down Expand Up @@ -83,7 +84,6 @@
import org.eclipse.tycho.core.resolver.P2Resolver;
import org.eclipse.tycho.core.resolver.P2ResolverFactory;
import org.eclipse.tycho.core.resolver.shared.PomDependencies;
import org.eclipse.tycho.core.utils.TychoProjectUtils;
import org.eclipse.tycho.helper.PluginRealmHelper;
import org.eclipse.tycho.p2.metadata.DependencyMetadataGenerator;
import org.eclipse.tycho.p2.metadata.PublisherOptions;
Expand Down Expand Up @@ -306,17 +306,9 @@ private void addEntireP2RepositoryToTargetPlatform(ArtifactRepository repository
public DependencyArtifacts resolveDependencies(final MavenSession session, final MavenProject project,
TargetPlatform targetPlatform, List<ReactorProject> reactorProjects,
DependencyResolverConfiguration resolverConfiguration, List<TargetEnvironment> environments) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
if (targetPlatform == null) {
targetPlatform = TychoProjectUtils.getTargetPlatform(reactorProject);
}

// TODO 364134 For compatibility reasons, target-platform-configuration includes settings for the dependency resolution
// --> split this information logically, e.g. through two distinct interfaces
Objects.requireNonNull(targetPlatform);
TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(project);

P2Resolver osgiResolverImpl = resolverFactory.createResolver(environments);

return doResolveDependencies(session, project, reactorProjects, resolverConfiguration, targetPlatform,
osgiResolverImpl, configuration);
}
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 40ca269

Please sign in to comment.