Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup TychoProjectUtils and use ProjectManager instead #3196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -33,9 +33,11 @@ public interface DependencyResolver {

PomDependencyCollector resolvePomDependencies(MavenSession session, MavenProject project);

public TargetPlatform computePreliminaryTargetPlatform(MavenSession session, MavenProject project,
public TargetPlatform computePreliminaryTargetPlatform(ReactorProject project,
List<ReactorProject> reactorProjects);

public TargetPlatform computePreliminaryTargetPlatform(MavenProject project, List<ReactorProject> reactorProjects);

/**
* @param targetPlatform
* The candidate artifacts which may be used to resolve dependencies. If
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 @@ -81,8 +81,8 @@ public DependencyArtifacts getDependencyArtifacts(ReactorProject reactorProject)
MavenSession mavenSession = getMavenSession(reactorProject);
MavenProject mavenProject = getMavenProject(reactorProject);
List<ReactorProject> reactorProjects = DefaultReactorProject.adapt(mavenSession);
TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession,
mavenProject, reactorProjects);
TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenProject,
reactorProjects);
TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(mavenProject);
DependencyResolverConfiguration resolverConfiguration = configuration.getDependencyResolverConfiguration();
DependencyArtifacts dependencyArtifacts = dependencyResolver.resolveDependencies(mavenSession, mavenProject,
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 @@ -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 = getCurrentTargetPlatform(project);
String type = mavenArtifactKey.getType();
String resolvedType = PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE.equals(type) ? ArtifactType.TYPE_ECLIPSE_PLUGIN
: type;
Expand Down Expand Up @@ -116,6 +115,16 @@ public Optional<ResolvedArtifactKey> resolveMavenBundle(MavenProject project, Ma
return Optional.empty();
}

private TargetPlatform getCurrentTargetPlatform(MavenProject project) {
//As this method is also called while the preliminary phase we can not simply use
// projectManager.getTargetPlatform(project).orElse(null);
if (DefaultReactorProject.adapt(project).getContextValue(
TargetPlatform.FINAL_TARGET_PLATFORM_KEY) instanceof TargetPlatform finalTargetPlatform) {
return finalTargetPlatform;
}
return null;
}

public Optional<ResolvedArtifactKey> resolveMavenBundle(MavenProject project, MavenSession mavenSession,
String groupId, String artifactId, String version) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ public Collection<IRequirement> getAdditionalRequirements() {
return resolverConfiguration.getAdditionalRequirements();
}
};
TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession,
mavenProject, reactorProjects);
TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenProject,
reactorProjects);
return dependencyResolver.resolveDependencies(mavenSession, mavenProject, preliminaryTargetPlatform,
reactorProjects, testResolverConfiguration, configuration.getEnvironments());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ public class DefaultTargetPlatformService implements TargetPlatformService {
private DependencyResolver dependencyResolver;

@Requirement
ReactorRepositoryManager repositoryManager;
private ReactorRepositoryManager repositoryManager;

@Requirement
private P2ResolverFactory p2ResolverFactory;

@Requirement
P2ResolverFactory p2ResolverFactory;
private TargetPlatformFactory tpFactory;

@Override
Expand All @@ -76,11 +78,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 +89,7 @@ public Optional<TargetPlatform> getTargetPlatform(ReactorProject project) throws
TargetPlatform finalTargetPlatform = computeFinalTargetPlatform(project, upstreamProjects,
pomDependenciesCollector);
return Optional.ofNullable(finalTargetPlatform);
}
});
}

/**
Expand All @@ -106,31 +104,16 @@ public Optional<TargetPlatform> getTargetPlatform(ReactorProject project) throws
*/
private TargetPlatform computeFinalTargetPlatform(ReactorProject project,
List<? extends ReactorProjectIdentities> upstreamProjects, PomDependencyCollector pomDependencyCollector) {
synchronized (project) {
PreliminaryTargetPlatformImpl preliminaryTargetPlatform = getRegisteredPreliminaryTargetPlatform(project);
if (preliminaryTargetPlatform == null) {
MavenSession session = project.adapt(MavenSession.class);
if (session == null) {
session = legacySupport.getSession();
if (session == null) {
return null;
}
}
MavenProject mavenProject = project.adapt(MavenProject.class);
if (mavenProject == null) {
return null;
}
preliminaryTargetPlatform = (PreliminaryTargetPlatformImpl) dependencyResolver
.computePreliminaryTargetPlatform(session, mavenProject, DefaultReactorProject.adapt(session));

}
List<PublishingRepository> upstreamProjectResults = getBuildResults(upstreamProjects);
TargetPlatform result = getTpFactory().createTargetPlatformWithUpdatedReactorContent(
preliminaryTargetPlatform, upstreamProjectResults, pomDependencyCollector);

project.setContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, result);
return result;
MavenSession session = project.adapt(MavenSession.class);
if (session == null) {
session = legacySupport.getSession();
}
PreliminaryTargetPlatformImpl preliminaryTargetPlatform = (PreliminaryTargetPlatformImpl) dependencyResolver
.computePreliminaryTargetPlatform(project, DefaultReactorProject.adapt(session));
List<PublishingRepository> upstreamProjectResults = getBuildResults(upstreamProjects);
TargetPlatform result = tpFactory.createTargetPlatformWithUpdatedReactorContent(preliminaryTargetPlatform,
upstreamProjectResults, pomDependencyCollector);
return result;
}

private List<PublishingRepository> getBuildResults(List<? extends ReactorProjectIdentities> projects) {
Expand All @@ -141,20 +124,6 @@ private List<PublishingRepository> getBuildResults(List<? extends ReactorProject
return results;
}

public synchronized TargetPlatformFactory getTpFactory() {
if (tpFactory == null) {
tpFactory = p2ResolverFactory.getTargetPlatformFactory();
}
return tpFactory;
}

private PreliminaryTargetPlatformImpl getRegisteredPreliminaryTargetPlatform(ReactorProject project) {
return project.getContextValue(
TargetPlatform.PRELIMINARY_TARGET_PLATFORM_KEY) instanceof PreliminaryTargetPlatformImpl preliminaryTargetPlatformImpl
? preliminaryTargetPlatformImpl
: null;
}

private List<ReactorProjectIdentities> getReferencedTychoProjects(ReactorProject reactorProject)
throws DependencyResolutionException {
List<ReactorProjectIdentities> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.p2.target.facade.PomDependencyCollector;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;

public interface P2ResolverFactory {

Expand All @@ -31,9 +30,6 @@ public interface P2ResolverFactory {
// TODO move to a PomDependencyCollectorFactory interface?
public PomDependencyCollector newPomDependencyCollector(ReactorProject project);

// TODO directly register as service
public TargetPlatformFactory getTargetPlatformFactory();

/**
* Create a resolver for the given environments
*
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 @@ -36,6 +36,7 @@
import org.eclipse.tycho.core.resolver.P2ResolverFactory;
import org.eclipse.tycho.core.resolver.shared.IncludeSourceMode;
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;

/**
* Component that resolves all the bundles that make up an Eclipse Application to run from a given
Expand All @@ -51,6 +52,9 @@ public class EclipseApplicationFactory {
@Requirement
private P2ResolverFactory resolverFactory;

@Requirement
private TargetPlatformFactory platformFactory;

@Requirement
private Logger logger;

Expand Down Expand Up @@ -88,8 +92,7 @@ public TargetPlatform createTargetPlatform(Collection<MavenRepositoryLocation> l
ExecutionEnvironmentConfiguration eeConfiguration = new ExecutionEnvironmentConfigurationImpl(logger, false,
toolchainManager, mavenSession);
eeConfiguration.setProfileConfiguration("JavaSE-" + javaVersion, "tycho-eclipse-application-resolver");
TargetPlatform targetPlatform = resolverFactory.getTargetPlatformFactory().createTargetPlatform(tpConfiguration,
eeConfiguration, null);
TargetPlatform targetPlatform = platformFactory.createTargetPlatform(tpConfiguration, eeConfiguration, null);
return targetPlatform;
}

Expand Down
Loading
Loading