diff options
author | Igor Fedorenko | 2017-11-19 23:26:34 +0000 |
---|---|---|
committer | Igor Fedorenko | 2018-05-21 13:17:14 +0000 |
commit | b68b8b26d23c41b94abd1b02a1864ab52038f6c1 (patch) | |
tree | 0a61500c5afc9f8c56a4e65b308d5f1b81bcb3b0 /org.eclipse.m2e.binaryproject | |
parent | 4c27b30f5335de0e98805a42ff83622a854daedd (diff) | |
download | m2e-core-b68b8b26d23c41b94abd1b02a1864ab52038f6c1.tar.gz m2e-core-b68b8b26d23c41b94abd1b02a1864ab52038f6c1.tar.xz m2e-core-b68b8b26d23c41b94abd1b02a1864ab52038f6c1.zip |
Bug 384065: advanced sourcelookup support
Change-Id: Ib145445c60c686bbbc187f252b32591535ac740d
Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
Diffstat (limited to 'org.eclipse.m2e.binaryproject')
10 files changed, 554 insertions, 0 deletions
diff --git a/org.eclipse.m2e.binaryproject/META-INF/MANIFEST.MF b/org.eclipse.m2e.binaryproject/META-INF/MANIFEST.MF new file mode 100644 index 00000000..4a2067de --- /dev/null +++ b/org.eclipse.m2e.binaryproject/META-INF/MANIFEST.MF @@ -0,0 +1,22 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: %Bundle-Name +Bundle-SymbolicName: org.eclipse.m2e.binaryproject;singleton:=true +Bundle-Version: 1.9.0.qualifier +Bundle-Vendor: %Bundle-Vendor +Require-Bundle: org.eclipse.m2e.core;bundle-version="[1.9.0,1.10.0)", + org.eclipse.m2e.maven.runtime;bundle-version="[1.9.0,1.10.0)", + org.eclipse.m2e.sourcelookup;bundle-version="[1.9.0,1.10.0)", + org.slf4j.api;bundle-version="1.6.1", + org.eclipse.equinox.common;bundle-version="3.6.0", + org.eclipse.m2e.jdt;bundle-version="[1.9.0,1.10.0)", + org.eclipse.core.resources;bundle-version="3.7.0", + org.eclipse.jdt.core;bundle-version="3.7.0", + org.eclipse.equinox.registry;bundle-version="3.5.0", + org.eclipse.core.runtime;bundle-version="3.7.0", + org.eclipse.debug.core;bundle-version="3.9.0", + org.eclipse.jdt.launching;bundle-version="3.10.0" +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Export-Package: org.eclipse.m2e.binaryproject.internal;x-friends:="org.eclipse.m2e.sourcelookup,org.eclipse.m2e.sourcelookup.ui" +Bundle-ActivationPolicy: lazy +Bundle-Activator: org.eclipse.m2e.binaryproject.internal.BinaryProjectPlugin diff --git a/org.eclipse.m2e.binaryproject/build.properties b/org.eclipse.m2e.binaryproject/build.properties new file mode 100644 index 00000000..52a5b4fe --- /dev/null +++ b/org.eclipse.m2e.binaryproject/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = target/classes +bin.includes = META-INF/,\ + .,\ + plugin.xml diff --git a/org.eclipse.m2e.binaryproject/plugin.xml b/org.eclipse.m2e.binaryproject/plugin.xml new file mode 100644 index 00000000..55064563 --- /dev/null +++ b/org.eclipse.m2e.binaryproject/plugin.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.4"?> +<plugin> + <extension + point="org.eclipse.m2e.core.lifecycleMappings"> + <lifecycleMapping + class="org.eclipse.m2e.binaryproject.internal.BinaryProjectLifecycleMapping" + id="org.eclipse.m2e.binaryproject" + name="Maven Binary Project"> + </lifecycleMapping> + </extension> + <extension + point="org.eclipse.jdt.launching.workspaceProjectDescribers"> + <describer + class="org.eclipse.m2e.binaryproject.internal.sourcelookup.BinaryProjectDescriber"> + </describer> + </extension> + +</plugin> diff --git a/org.eclipse.m2e.binaryproject/pom.xml b/org.eclipse.m2e.binaryproject/pom.xml new file mode 100644 index 00000000..61859cfd --- /dev/null +++ b/org.eclipse.m2e.binaryproject/pom.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (c) 2012 Igor Fedorenko + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License v1.0 + which accompanies this distribution, and is available at + http://www.eclipse.org/legal/epl-v10.html +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.eclipse.m2e</groupId> + <artifactId>m2e-core</artifactId> + <version>1.9.0-SNAPSHOT</version> + </parent> + + <artifactId>org.eclipse.m2e.binaryproject</artifactId> + <packaging>eclipse-plugin</packaging> +</project>
\ No newline at end of file diff --git a/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/AbstractBinaryProjectsImportJob.java b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/AbstractBinaryProjectsImportJob.java new file mode 100644 index 00000000..e80cb3fa --- /dev/null +++ b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/AbstractBinaryProjectsImportJob.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2012 Igor Fedorenko + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Igor Fedorenko - initial API and implementation + *******************************************************************************/ +package org.eclipse.m2e.binaryproject.internal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.m2e.core.embedder.ArtifactKey; + +public abstract class AbstractBinaryProjectsImportJob extends Job { + + public AbstractBinaryProjectsImportJob() { + super("Import binary projects"); + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + Collection<ArtifactKey> artifacts; + try { + artifacts = getArtifactKeys(monitor); + } catch (CoreException e1) { + return e1.getStatus(); + } + + if (!artifacts.isEmpty()) { + List<IStatus> errors = new ArrayList<IStatus>(); + + for (ArtifactKey artifact : artifacts) { + try { + BinaryProjectPlugin.getInstance().create(artifact.getGroupId(), artifact.getArtifactId(), + artifact.getVersion(), null, monitor); + } catch (CoreException e) { + errors.add(e.getStatus()); + } + } + } + return Status.OK_STATUS; + } + + protected abstract Collection<ArtifactKey> getArtifactKeys(IProgressMonitor monitor) throws CoreException; + +} diff --git a/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/BinaryProjectLifecycleMapping.java b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/BinaryProjectLifecycleMapping.java new file mode 100644 index 00000000..867806b9 --- /dev/null +++ b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/BinaryProjectLifecycleMapping.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2012 Igor Fedorenko + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Igor Fedorenko - initial API and implementation + *******************************************************************************/ +package org.eclipse.m2e.binaryproject.internal; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.internal.MavenPluginActivator; +import org.eclipse.m2e.core.project.IMavenProjectFacade; +import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant; +import org.eclipse.m2e.core.project.configurator.AbstractLifecycleMapping; +import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator; +import org.eclipse.m2e.core.project.configurator.ILifecycleMappingConfiguration; +import org.eclipse.m2e.core.project.configurator.MojoExecutionKey; +import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest; + +@SuppressWarnings("restriction") +public class BinaryProjectLifecycleMapping extends AbstractLifecycleMapping { + private final ClasspathConfigurator configurator; + + public BinaryProjectLifecycleMapping() { + ClasspathConfigurator configurator = new ClasspathConfigurator(); + configurator.setProjectManager(MavenPlugin.getMavenProjectRegistry()); + configurator.setMavenConfiguration(MavenPlugin.getMavenConfiguration()); + configurator.setMarkerManager(MavenPluginActivator.getDefault().getMavenMarkerManager()); + + this.configurator = configurator; + } + + @Override + public Map<MojoExecutionKey, List<AbstractBuildParticipant>> getBuildParticipants(IMavenProjectFacade project, + IProgressMonitor monitor) throws CoreException { + return Collections.emptyMap(); + } + + @Override + public List<AbstractProjectConfigurator> getProjectConfigurators(IMavenProjectFacade project, + IProgressMonitor monitor) throws CoreException { + return Collections.emptyList(); + } + + @Override + public boolean hasLifecycleMappingChanged(IMavenProjectFacade newFacade, + ILifecycleMappingConfiguration oldConfiguration, IProgressMonitor monitor) { + return false; + } + + public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException { + configurator.configure(request, monitor); + } + + public void unconfigure(ProjectConfigurationRequest request, IProgressMonitor monitor) {} +} diff --git a/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/BinaryProjectPlugin.java b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/BinaryProjectPlugin.java new file mode 100644 index 00000000..652f5c13 --- /dev/null +++ b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/BinaryProjectPlugin.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2012 Igor Fedorenko + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Igor Fedorenko - initial API and implementation + *******************************************************************************/ +package org.eclipse.m2e.binaryproject.internal; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.codehaus.plexus.util.FileUtils; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ProjectScope; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.project.IProjectConfigurationManager; +import org.eclipse.m2e.core.project.ResolverConfiguration; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.service.prefs.BackingStoreException; + +public class BinaryProjectPlugin implements BundleActivator { + + public static final String PLUGIN_ID = "org.eclipse.m2e.binaryproject"; + + public static final String LIFECYCLE_MAPPING_ID = "org.eclipse.m2e.binaryproject"; + + public static final String P_GROUPID = "groupId"; + + public static final String P_ARTIFACTID = "artifactId"; + + public static final String P_VERSION = "version"; + + public static final String P_TYPE = "type"; + + public static final String P_CLASSIFIER = "classifier"; + + /** + * Name of IProject persistent property that identifies absolute filesystem path of the target jar artifact of the + * workspace binary project. + */ + public static final QualifiedName QNAME_JAR = new QualifiedName(PLUGIN_ID, "jar"); + + private static BinaryProjectPlugin SELF; + + public static BinaryProjectPlugin getInstance() { + return SELF; + } + + private Bundle bundle; + + public IProject create(String groupId, String artifactId, String version, List<ArtifactRepository> repositories, + IProgressMonitor monitor) throws CoreException { + IMaven maven = MavenPlugin.getMaven(); + + Artifact pomArtifact = + maven.resolve(groupId, artifactId, version, "pom" /* type */, null /* classifier */, repositories, monitor); + + ResolverConfiguration resolverConfig = new ResolverConfiguration(); + resolverConfig.setLifecycleMappingId(LIFECYCLE_MAPPING_ID); + + String projectName = groupId + "_" + artifactId + "_" + version; + + IPath stateLocation = Platform.getStateLocation(bundle); + + IPath projectLocation = stateLocation.append(projectName); + projectLocation.toFile().mkdirs(); + + File pomFile = new File(projectLocation.toFile(), "pom.xml"); + + try { + FileUtils.copyFile(pomArtifact.getFile(), pomFile); + } catch (IOException e) { + throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID, "Could not create binary project", e)); + } + + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + IWorkspaceRoot root = workspace.getRoot(); + + IProject project = root.getProject(projectName); + + IProjectDescription description = workspace.newProjectDescription(projectName); + description.setLocation(projectLocation); + project.create(description, monitor); + project.open(monitor); + + IScopeContext projectScope = new ProjectScope(project); + IEclipsePreferences projectNode = projectScope.getNode(BinaryProjectPlugin.PLUGIN_ID); + + projectNode.put(P_GROUPID, groupId); + projectNode.put(P_ARTIFACTID, artifactId); + projectNode.put(P_VERSION, version); + // String type = projectNode.get( P_TYPE, "jar" ); + // String classifier = projectNode.get( P_CLASSIFIER, (String) null ); + + try { + projectNode.flush(); + } catch (BackingStoreException e) { + throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID, "Could not create binary project", e)); + } + + IProjectConfigurationManager configManager = MavenPlugin.getProjectConfigurationManager(); + + configManager.enableMavenNature(project, resolverConfig, monitor); + + return project; + } + + @Override + public void start(BundleContext context) throws Exception { + this.bundle = context.getBundle(); + BinaryProjectPlugin.SELF = this; + } + + @Override + public void stop(BundleContext context) throws Exception { + BinaryProjectPlugin.SELF = null; + this.bundle = null; + } +} diff --git a/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/ClasspathConfigurator.java b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/ClasspathConfigurator.java new file mode 100644 index 00000000..c84c9fc1 --- /dev/null +++ b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/ClasspathConfigurator.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2012 Igor Fedorenko + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Igor Fedorenko - initial API and implementation + *******************************************************************************/ +package org.eclipse.m2e.binaryproject.internal; + +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ProjectScope; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.ArtifactKey; +import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest; +import org.eclipse.m2e.jdt.IClasspathDescriptor; +import org.eclipse.m2e.jdt.IClasspathEntryDescriptor; +import org.eclipse.m2e.jdt.internal.AbstractJavaProjectConfigurator; +import org.eclipse.m2e.jdt.internal.BuildPathManager; + +@SuppressWarnings("restriction") +public class ClasspathConfigurator extends AbstractJavaProjectConfigurator { + + @Override + protected void addProjectSourceFolders(IClasspathDescriptor classpath, ProjectConfigurationRequest request, + IProgressMonitor monitor) throws CoreException {} + + @Override + protected void addJavaNature(IProject project, IProgressMonitor monitor) throws CoreException { + addNature(project, JavaCore.NATURE_ID, IResource.KEEP_HISTORY | IResource.AVOID_NATURE_CONFIG, monitor); + } + + @Override + protected void invokeJavaProjectConfigurators(IClasspathDescriptor classpath, ProjectConfigurationRequest request, + IProgressMonitor monitor) throws CoreException { + // TODO need repository information + + IProject project = request.getProject(); + + IMaven maven = MavenPlugin.getMaven(); + + IScopeContext projectScope = new ProjectScope(project); + IEclipsePreferences projectNode = projectScope.getNode(BinaryProjectPlugin.PLUGIN_ID); + + String groupId = projectNode.get(BinaryProjectPlugin.P_GROUPID, (String) null); + String artifactId = projectNode.get(BinaryProjectPlugin.P_ARTIFACTID, (String) null); + String version = projectNode.get(BinaryProjectPlugin.P_VERSION, (String) null); + String type = projectNode.get(BinaryProjectPlugin.P_TYPE, "jar"); + String classifier = projectNode.get(BinaryProjectPlugin.P_CLASSIFIER, (String) null); + + List<ArtifactRepository> repositories = null; // TODO store in project preferences + + Artifact jar = maven.resolve(groupId, artifactId, version, type, classifier, repositories, monitor); + String jarLocation = jar.getFile().getAbsolutePath(); + + project.setPersistentProperty(BinaryProjectPlugin.QNAME_JAR, jarLocation); + + Artifact sources = + maven.resolve(groupId, artifactId, version, type, getSourcesClassifier(classifier), repositories, monitor); + IClasspathEntryDescriptor libEntry = classpath.addLibraryEntry(Path.fromOSString(jarLocation)); + libEntry.setExported(true); + libEntry.setSourceAttachment(Path.fromOSString(sources.getFile().getAbsolutePath()), null); + libEntry.setArtifactKey(new ArtifactKey(groupId, artifactId, version, classifier)); + } + + @Override + protected IContainer getOutputLocation(ProjectConfigurationRequest request, IProject project) { + return project; + } + + static String getSourcesClassifier(String baseClassifier) { + return BuildPathManager.CLASSIFIER_TESTS.equals(baseClassifier) + ? BuildPathManager.CLASSIFIER_TESTSOURCES + : BuildPathManager.CLASSIFIER_SOURCES; + } + +} diff --git a/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/sourcelookup/BinaryProjectContainerResolver.java b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/sourcelookup/BinaryProjectContainerResolver.java new file mode 100644 index 00000000..f374a4fd --- /dev/null +++ b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/sourcelookup/BinaryProjectContainerResolver.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2011-2016 Igor Fedorenko + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Igor Fedorenko - initial API and implementation + *******************************************************************************/ +package org.eclipse.m2e.binaryproject.internal.sourcelookup; + +import static org.eclipse.jdt.launching.sourcelookup.advanced.AdvancedSourceLookup.getClasspath; +import static org.eclipse.m2e.binaryproject.internal.sourcelookup.BinaryProjectDescriber.getBinaryLocation; + +import java.io.File; +import java.util.Map; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.debug.core.sourcelookup.ISourceContainer; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.launching.sourcelookup.advanced.ISourceContainerResolver; +import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer; +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.ArtifactKey; +import org.eclipse.m2e.core.project.IMavenProjectFacade; +import org.eclipse.m2e.core.project.IMavenProjectRegistry; +import org.eclipse.m2e.sourcelookup.internal.launch.MavenSourceContainerResolver; + +// useful to lookup shaded binary projects like wagon-http-2.10-shaded.jar +public class BinaryProjectContainerResolver extends MavenSourceContainerResolver implements ISourceContainerResolver { + + @Override + protected ISourceContainer resolveSourceContainer(ArtifactKey artifact, IProgressMonitor monitor) { + String groupId = artifact.getGroupId(); + String artifactId = artifact.getArtifactId(); + String version = artifact.getVersion(); + + IMavenProjectRegistry projectRegistry = MavenPlugin.getMavenProjectRegistry(); + + IMavenProjectFacade mavenProject = projectRegistry.getMavenProject(groupId, artifactId, version); + if (mavenProject == null) { + return null; + } + + IProject project = mavenProject.getProject(); + + try { + final File binaryLocation = getBinaryLocation(project); + if (binaryLocation == null) { + return null; + } + + IJavaProject javaProject = JavaCore.create(project); + + Map<File, IPackageFragmentRoot> classpath = getClasspath(javaProject); + IPackageFragmentRoot binary = classpath.remove(binaryLocation); + + if (binary == null) { + return null; // this is a bug somewhere in my code + } + + return new PackageFragmentRootSourceContainer(binary); + } catch (CoreException e) { + // ignore, maybe log + } + + return null; + } +} diff --git a/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/sourcelookup/BinaryProjectDescriber.java b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/sourcelookup/BinaryProjectDescriber.java new file mode 100644 index 00000000..5619de39 --- /dev/null +++ b/org.eclipse.m2e.binaryproject/src/org/eclipse/m2e/binaryproject/internal/sourcelookup/BinaryProjectDescriber.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2011-2016 Igor Fedorenko + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Igor Fedorenko - initial API and implementation + *******************************************************************************/ +package org.eclipse.m2e.binaryproject.internal.sourcelookup; + +import static org.eclipse.jdt.launching.sourcelookup.advanced.AdvancedSourceLookup.getClasspath; + +import java.io.File; +import java.util.Map; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.launching.sourcelookup.advanced.IWorkspaceProjectDescriber; +import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer; +import org.eclipse.m2e.binaryproject.internal.BinaryProjectPlugin; + +public class BinaryProjectDescriber implements IWorkspaceProjectDescriber { + + static File getBinaryLocation(IProject project) throws CoreException { + if (!project.isOpen()) { + return null; + } + final String binaryLocation = project.getPersistentProperty(BinaryProjectPlugin.QNAME_JAR); + if (binaryLocation == null) { + return null; + } + return new File(binaryLocation); + } + + @Override + public void describeProject(IJavaProject project, IJavaProjectSourceDescription description) throws CoreException { + final File binaryLocation = getBinaryLocation(project.getProject()); + if (binaryLocation == null) { + return; + } + + Map<File, IPackageFragmentRoot> classpath = getClasspath(project); + IPackageFragmentRoot binary = classpath.remove(binaryLocation); + + if (binary == null) { + return; // this is a bug somewhere in my code + } + + description.addDependencies(classpath); + description.addLocation(binaryLocation); + description.addSourceContainerFactory(() -> new PackageFragmentRootSourceContainer(binary)); + } + +} |