From d5aa6d0a1a2ed5f56e2df61094a541fb92ada771 Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Tue, 12 Jun 2012 10:38:51 -0400 Subject: [PATCH] Implement a custom resolver for Tycho in local mode. When running in local mode, dependencies should be resolved by looking on the local system. Remote repositories should be ignored unless offline mode is disabled. Automatically create a local p2 repository of all bundles on the system so that they may be used for local dependency resolution. This is done using a modified version of Eclipse's copy-platform script. Since Fedora 17, we need an Execution Environment of at least JavaSE-1.6 for Eclipse bundles. Eclipse Juno platform bundles depend on javax.annotation. In Fedora this is provided by geronimo-annotation, but has a dependency on javax.lang.model (since 1.6). Ignore all environments defined in target-platform-configuration when running in local mode. Change-Id: Ia1ece07ece2412bc4a88901631f3f651ad2b634b --- .../tycho/p2/target/TargetDefinitionResolver.java | 10 ++++-- .../tycho/p2/target/TargetPlatformBuilderImpl.java | 6 ++++ .../core/maven/TychoMavenLifecycleParticipant.java | 16 +++++++++ .../core/osgitools/EclipseFeatureProject.java | 22 ++++++++++++ .../tycho/core/osgitools/OsgiBundleProject.java | 42 ++++++++++++++++++++-- .../DefaultTargetPlatformConfigurationReader.java | 5 ++- .../tycho/osgi/runtime/TychoP2RuntimeLocator.java | 15 ++++++++ .../p2/resolver/P2TargetPlatformResolver.java | 10 ++++++ 8 files changed, 120 insertions(+), 6 deletions(-) diff --git a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetDefinitionResolver.java b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetDefinitionResolver.java index 5fdc6a1..838e267 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetDefinitionResolver.java +++ b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetDefinitionResolver.java @@ -24,6 +24,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.URIUtil; import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.metadata.IInstallableUnit; @@ -103,9 +104,12 @@ public class TargetDefinitionResolver { List metadataRepositories = new ArrayList(); for (Repository repository : iuLocationDefinition.getRepositories()) { - repositoryIdManager.addMapping(repository.getId(), repository.getLocation()); - artifactRepositories.add(repository.getLocation()); - metadataRepositories.add(loadRepository(repository)); + // We cannot resolve a non-file URI in local mode + if (System.getProperty("maven.local.mode") == null || URIUtil.isFileURI(repository.getLocation())) { + repositoryIdManager.addMapping(repository.getId(), repository.getLocation()); + artifactRepositories.add(repository.getLocation()); + metadataRepositories.add(loadRepository(repository)); + } } IQueryable locationUnits = new CompoundQueryable( diff --git a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetPlatformBuilderImpl.java b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetPlatformBuilderImpl.java index fa1cfab..23cdb5d 100644 --- a/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetPlatformBuilderImpl.java +++ b/tycho-bundles/org.eclipse.tycho.p2.resolver.impl/src/main/java/org/eclipse/tycho/p2/target/TargetPlatformBuilderImpl.java @@ -223,6 +223,12 @@ public class TargetPlatformBuilderImpl implements TargetPlatformBuilder { IMetadataRepository metadataRepository = null; IArtifactRepository artifactRepository = null; + // We cannot resolve a non-file URI in local mode while offline + if (System.getProperty("maven.local.mode") != null && offline && !URIUtil.isFileURI(location.getURL())) { + return; + } + + try { remoteRepositoryIdManager.addMapping(location.getId(), location.getURL()); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java index 0dd2fd4..15669a6 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java @@ -11,6 +11,7 @@ package org.eclipse.tycho.core.maven; import java.io.File; +import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -55,6 +56,21 @@ public class TychoMavenLifecycleParticipant extends AbstractMavenLifecyclePartic registerExecutionListener(session); configureComponents(session); + // Create a system p2 repository for local dependency resolution + if (System.getProperty("maven.local.mode") != null) { + try { + String[] cmd = new String[] { "/usr/share/java/tycho/copy-platform-all", + System.getProperty("user.dir") + "/.m2/p2/repo" }; + System.out.println("Building system local p2 repository..."); + Process p = Runtime.getRuntime().exec(cmd); + p.waitFor(); + } catch (IOException e) { + // Continue + } catch (InterruptedException e) { + // Continue + } + } + for (MavenProject project : projects) { resolver.setupProject(session, project, DefaultReactorProject.adapt(project)); } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/EclipseFeatureProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/EclipseFeatureProject.java index 0fe42a6..1bc3a6e 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/EclipseFeatureProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/EclipseFeatureProject.java @@ -21,6 +21,9 @@ import org.eclipse.tycho.core.ArtifactDependencyVisitor; import org.eclipse.tycho.core.ArtifactDependencyWalker; import org.eclipse.tycho.core.TychoProject; import org.eclipse.tycho.core.facade.TargetEnvironment; +import org.eclipse.tycho.core.UnknownEnvironmentException; +import org.eclipse.tycho.core.utils.ExecutionEnvironment; +import org.eclipse.tycho.core.utils.ExecutionEnvironmentUtils; import org.eclipse.tycho.model.Feature; @Component(role = TychoProject.class, hint = org.eclipse.tycho.ArtifactKey.TYPE_ECLIPSE_FEATURE) @@ -49,4 +52,23 @@ public class EclipseFeatureProject extends AbstractArtifactBasedProject { Feature.loadFeature(project.getBasedir()); } + @Override + public ExecutionEnvironment getExecutionEnvironment(MavenProject project) { + ExecutionEnvironment ee = super.getExecutionEnvironment(project); + + if (System.getProperty("maven.local.mode") != null && ee != null) { + try { + // EE must be at least JavaSE-1.6 + final ExecutionEnvironment javaSE16 = ExecutionEnvironmentUtils.getExecutionEnvironment("JavaSE-1.6"); + if (ee.compareTo(javaSE16) < 0) { + ee = javaSE16; + } + } catch (UnknownEnvironmentException e) { + // Continue + } + } + + return ee; + } + } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java index c4b783e..e83dd72 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java @@ -525,12 +525,50 @@ public class OsgiBundleProject extends AbstractTychoProject implements BundlePro if (envs.isEmpty()) { return null; } - return Collections.min(envs); + + ExecutionEnvironment manifestMinimalEE = Collections.min(envs); + ExecutionEnvironment tmp; + + if (System.getProperty("maven.local.mode") != null) { + try { + // EE must be at least JavaSE-1.6 + final ExecutionEnvironment javaSE16 = ExecutionEnvironmentUtils.getExecutionEnvironment("JavaSE-1.6"); + while (!envs.isEmpty()) { + tmp = Collections.min(envs); + if (tmp.compareTo(javaSE16) >= 0) { + manifestMinimalEE = tmp; + break; + } + envs.remove(tmp); + } + + if (envs.isEmpty()) { + return javaSE16; + } + } catch (UnknownEnvironmentException e) { + // Continue + } + } + return manifestMinimalEE; } protected ExecutionEnvironment getExecutionEnvironment(MavenProject project, String profile) { try { - return ExecutionEnvironmentUtils.getExecutionEnvironment(profile); + ExecutionEnvironment ee = ExecutionEnvironmentUtils.getExecutionEnvironment(profile); + + if (System.getProperty("maven.local.mode") != null && ee != null) { + try { + // EE must be at least JavaSE-1.6 + final ExecutionEnvironment javaSE16 = ExecutionEnvironmentUtils.getExecutionEnvironment("JavaSE-1.6"); + if (ee.compareTo(javaSE16) < 0) { + ee = javaSE16; + } + } catch (UnknownEnvironmentException e) { + // Continue + } + } + + return ee; } catch (UnknownEnvironmentException e) { throw new RuntimeException("Unknown execution environment specified in build.properties of project " + project, e); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java index c2997bc..9c1f5c8 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java @@ -66,7 +66,10 @@ public class DefaultTargetPlatformConfigurationReader { + configuration.toString()); } - addTargetEnvironments(result, project, configuration); + // Ignore defined environments in local mode + if (System.getProperty("maven.local.mode") == null) { + addTargetEnvironments(result, project, configuration); + } setTargetPlatformResolver(result, configuration); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/osgi/runtime/TychoP2RuntimeLocator.java b/tycho-core/src/main/java/org/eclipse/tycho/osgi/runtime/TychoP2RuntimeLocator.java index ff4c14a..de02644 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/osgi/runtime/TychoP2RuntimeLocator.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/osgi/runtime/TychoP2RuntimeLocator.java @@ -22,6 +22,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.artifact.resolver.JavadirWorkspaceReader; import org.apache.maven.artifact.resolver.ResolutionErrorHandler; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Dependency; @@ -38,6 +39,7 @@ import org.eclipse.sisu.equinox.embedder.EquinoxRuntimeLocator; import org.eclipse.tycho.dev.DevWorkspaceResolver; import org.eclipse.tycho.locking.facade.FileLockService; import org.eclipse.tycho.locking.facade.FileLocker; +import org.sonatype.aether.util.artifact.DefaultArtifact; @Component(role = EquinoxRuntimeLocator.class) public class TychoP2RuntimeLocator implements EquinoxRuntimeLocator { @@ -189,6 +191,19 @@ public class TychoP2RuntimeLocator implements EquinoxRuntimeLocator { Artifact artifact = repositorySystem.createArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getType()); + // If we are in local mode, find the artifact on the system + if (System.getProperty("maven.local.mode") != null) { + JavadirWorkspaceReader wReader = new JavadirWorkspaceReader(); + DefaultArtifact newArtifact = new DefaultArtifact(artifact.getGroupId() + + ":" + artifact.getArtifactId() + + ":" + artifact.getType() + + ":" + artifact.getVersion()); + File file = wReader.findArtifact(newArtifact); + if (file != null) { + artifact.setFile(file); + } + } + ArtifactResolutionRequest request = new ArtifactResolutionRequest(); request.setArtifact(artifact); request.setResolveRoot(true).setResolveTransitively(false); diff --git a/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2TargetPlatformResolver.java b/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2TargetPlatformResolver.java index 4db1bd4..b45a4ea 100644 --- a/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2TargetPlatformResolver.java +++ b/tycho-p2/tycho-p2-facade/src/main/java/org/eclipse/tycho/p2/resolver/P2TargetPlatformResolver.java @@ -193,6 +193,16 @@ public class P2TargetPlatformResolver extends AbstractTargetPlatformResolver imp tpBuilder.setProjectLocation(project.getBasedir()); tpBuilder.setIncludePackedArtifacts(configuration.isIncludePackedArtifacts()); + // Add Fedora Local P2 Repository when running in local mode + if (System.getProperty("maven.local.mode") != null) { + String uri = "file:" + System.getProperty("user.dir") + "/.m2/p2/repo"; + try { + tpBuilder.addP2Repository(new MavenRepositoryLocation(uri, new URI(uri))); + } catch (URISyntaxException e) { + getLogger().warn("Unable to resolve repository URI : " + uri, e); + } + } + addThisReactorProjectToTargetPlatform(session, project, configuration, tpBuilder); addOtherReactorProjectsToTargetPlatform(project, reactorProjects, tpBuilder); -- 1.7.11.2