diff options
author | Fred Bricon | 2012-09-08 12:49:49 +0000 |
---|---|---|
committer | Fred Bricon | 2012-12-05 18:30:26 +0000 |
commit | 20707859f37a73e10e91b6beae1a43b371af4640 (patch) | |
tree | bf0f334110297a6bc98650a343cc14928250d916 | |
parent | f62aae5a9f3f90676e40f1b7403ed0ec075828cb (diff) | |
download | m2e-core-20707859f37a73e10e91b6beae1a43b371af4640.tar.gz m2e-core-20707859f37a73e10e91b6beae1a43b371af4640.tar.xz m2e-core-20707859f37a73e10e91b6beae1a43b371af4640.zip |
374742 : limit conversion to specific packagings
AbstractProjectConversionParticipant can now declare a set of supported
Maven packagings, contributed via an extension point.
The conversion manager now checks the requested packaging is compatible
with a given conversion participant. If no restrictions are defined or the
given packaging is listed in the restricted packagings, then it's considered
compatible.
For JDT project conversion, only the jar packaging is
defined by default for org.eclipse.m2e.jdt.javaProjectConversionParticipant.
So if a Java Project is converted to an eclipse-plugin for instance, since
eclipse-plugin is not declared as compatible packaging for
org.eclipse.m2e.jdt.javaProjectConversionParticipant by default, no
maven-compiler-plugin configuration will be added during the project
conversion.
More compatible packagings can be declared, if necessary, by 3rd party
plugins, using the <conversionParticipantConfiguration> element.
<compatiblePackagings> is optional since the extension point could be used
to add additional restriction policies
(https://bugs.eclipse.org/bugs/show_bug.cgi?id=388981).
It's recommended AbstractProjectConversionParticipant implementations
declare at least one restricted packaging, as 3rd party plugins could
introduce restrictions when none were originally declared.
Signed-off-by: Fred Bricon <fbricon@gmail.com>
5 files changed, 153 insertions, 10 deletions
diff --git a/org.eclipse.m2e.core/schema/projectConversionParticipants.exsd b/org.eclipse.m2e.core/schema/projectConversionParticipants.exsd index 74c727c3..c57d859f 100644 --- a/org.eclipse.m2e.core/schema/projectConversionParticipants.exsd +++ b/org.eclipse.m2e.core/schema/projectConversionParticipants.exsd @@ -86,6 +86,25 @@ </complexType> </element> + <element name="conversionParticipantConfiguration"> + <complexType> + <attribute name="conversionParticipantId" type="string" use="required"> + <annotation> + <documentation> + project conversion participant id this configuration applies to + </documentation> + </annotation> + </attribute> + <attribute name="compatiblePackagings" type="string"> + <annotation> + <documentation> + Optional comma-separated list of Maven packagings that should be added to the project conversion participant's restricted packagings. + </documentation> + </annotation> + </attribute> + </complexType> + </element> + <annotation> <appInfo> <meta.section type="since"/> diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/project/conversion/ProjectConversionManager.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/project/conversion/ProjectConversionManager.java index 8b3893d3..80b08c8c 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/project/conversion/ProjectConversionManager.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/project/conversion/ProjectConversionManager.java @@ -12,7 +12,13 @@ package org.eclipse.m2e.core.internal.project.conversion; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +43,7 @@ import org.eclipse.m2e.core.project.conversion.IProjectConversionManager; * Looks up for {@link AbstractProjectConversionParticipant} contributed by 3rd party eclipse plugins. * * @author Fred Bricon + * @since 1.1 */ public class ProjectConversionManager implements IProjectConversionManager { @@ -49,17 +56,32 @@ public class ProjectConversionManager implements IProjectConversionManager { IExtensionRegistry registry = Platform.getExtensionRegistry(); IExtensionPoint conversionExtensionPoint = registry.getExtensionPoint(CONVERSION_PARTICIPANTS_EXTENSION_POINT); + Map<String, Set<String>> restrictedPackagings = new HashMap<String, Set<String>>(); if(conversionExtensionPoint != null) { IExtension[] archetypesExtensions = conversionExtensionPoint.getExtensions(); for(IExtension extension : archetypesExtensions) { IConfigurationElement[] elements = extension.getConfigurationElements(); for(IConfigurationElement element : elements) { - try { - if(project.hasNature(element.getAttribute("nature"))) { - participants.add((AbstractProjectConversionParticipant) element.createExecutableExtension("class")); + if ("projectConversionParticipant".equals(element.getName())) { + try { + if(project.hasNature(element.getAttribute("nature"))) { + AbstractProjectConversionParticipant projectConversionParticipant = (AbstractProjectConversionParticipant) element.createExecutableExtension("class"); + participants.add(projectConversionParticipant); + } + } catch(CoreException ex) { + log.debug("Can not load IProjectConversionParticipant", ex); } - } catch(CoreException ex) { - log.debug("Can not load IProjectConversionParticipant", ex); + } else if ("conversionParticipantConfiguration".equals(element.getName())) { + setRestrictedPackagings(restrictedPackagings, element); + } + } + } + + for (AbstractProjectConversionParticipant cp : participants) { + Set<String> newPackagings = restrictedPackagings.get(cp.getId()); + if (newPackagings != null) { + for (String p : newPackagings) { + cp.addRestrictedPackaging(p); } } } @@ -67,11 +89,40 @@ public class ProjectConversionManager implements IProjectConversionManager { return participants; } + private static void setRestrictedPackagings(Map<String, Set<String>> restrictedPackagings, + IConfigurationElement element) { + String pid = element.getAttribute("conversionParticipantId"); + String packagesAsString = element.getAttribute("compatiblePackagings"); + if (pid != null && packagesAsString != null) { + try { + String[] packagingsArray = packagesAsString.split(","); + Set<String> packagings = new HashSet<String>(packagingsArray.length); + for (String packaging : packagingsArray) { + String p = packaging.trim(); + if (p.length() > 0) { + packagings.add(p); + } + } + + Set<String> allPackages = restrictedPackagings.get(pid); + if (allPackages == null) { + allPackages = new HashSet<String>(); + restrictedPackagings.put(pid, allPackages); + } + + allPackages.addAll(packagings); + + } catch (Exception e) { + log.debug("Cannot parse restricted packagings ",e); + } + } + } + public void convert(IProject project, Model model, IProgressMonitor monitor) throws CoreException { if(model == null) { return; } - List<AbstractProjectConversionParticipant> participants = getConversionParticipants(project); + List<AbstractProjectConversionParticipant> participants = getConversionParticipants(project, model.getPackaging()); if(participants != null) { for(AbstractProjectConversionParticipant participant : participants) { participant.convert(project, model, monitor); @@ -79,17 +130,25 @@ public class ProjectConversionManager implements IProjectConversionManager { } } + @Deprecated public List<AbstractProjectConversionParticipant> getConversionParticipants(IProject project) throws CoreException { + return getConversionParticipants(project, null); + } + + public List<AbstractProjectConversionParticipant> getConversionParticipants(IProject project, String packaging) throws CoreException { List<AbstractProjectConversionParticipant> allParticipants = lookupConversionParticipants(project); List<AbstractProjectConversionParticipant> participants = new ArrayList<AbstractProjectConversionParticipant>(); if(allParticipants != null) { for(AbstractProjectConversionParticipant participant : allParticipants) { + if (packaging != null && !participant.isPackagingCompatible(packaging)){ + continue; + } if(participant.accept(project)) { participants.add(participant); } } } - return participants; + return Collections.unmodifiableList(participants); } } diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/AbstractProjectConversionParticipant.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/AbstractProjectConversionParticipant.java index 64553a9b..5562d13e 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/AbstractProjectConversionParticipant.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/AbstractProjectConversionParticipant.java @@ -10,6 +10,10 @@ *******************************************************************************/ package org.eclipse.m2e.core.project.conversion; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; @@ -22,6 +26,7 @@ import org.apache.maven.model.Model; * Used to convert existing Eclipse project configuration to the corresponding Maven Model. * * @author Fred Bricon + * @since 1.1 */ public abstract class AbstractProjectConversionParticipant implements IExecutableExtension { @@ -29,7 +34,10 @@ public abstract class AbstractProjectConversionParticipant implements IExecutabl public static final String ATTR_NAME = "name"; //$NON-NLS-1$ + protected Set<String> restrictedPackagings; + private String name; + private String id; public String getName() { @@ -52,7 +60,7 @@ public abstract class AbstractProjectConversionParticipant implements IExecutabl * Checks if this participant can change the Maven Model from this Eclipse project configuration */ public abstract boolean accept(IProject project) throws CoreException; - + /** * Converts existing Eclipse project configuration to Maven model */ @@ -62,4 +70,42 @@ public abstract class AbstractProjectConversionParticipant implements IExecutabl public String toString() { return (name == null)?getId():name; } + + /** + * Returns all the Maven packagings this conversion participant is restricted to. + * + * @return an unmodifiable {@link Set} copy of Maven packagings, can be <code>null</code>. + * @since 1.3 + */ + public Set<String> getRestrictedPackagings() { + return restrictedPackagings == null? null : Collections.unmodifiableSet(restrictedPackagings); + } + + /** + * Checks if this conversion participant allows the given Maven packaging to be converted :<br/> + * If there are no packaging restrictions or the packaging restrictions contain this packaging, + * then it's considered compatible. + * + * @param packaging the Maven packaging to check + * @return <code>true</code> if the packaging is compatible with this conversion participant. + */ + public boolean isPackagingCompatible(String packaging) { + boolean isCompatible = restrictedPackagings == null || restrictedPackagings.isEmpty() //no restrictions + || restrictedPackagings.contains(packaging); + return isCompatible; + } + + /** + * Adds a Maven packaging to the set of restricted, compatible packagings for this converter. + * @param packaging the compatible Maven packaging to add + */ + public void addRestrictedPackaging(String packaging) { + if (packaging != null) { + if (restrictedPackagings == null) { + restrictedPackagings = new HashSet<String>(); + } + restrictedPackagings.add(packaging); + } + } + } diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/IProjectConversionManager.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/IProjectConversionManager.java index 3febc39d..d4b39f20 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/IProjectConversionManager.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/conversion/IProjectConversionManager.java @@ -24,6 +24,7 @@ import org.apache.maven.model.Model; * Manages conversion of existing Eclipse projects into Maven ones. * * @author Fred Bricon + * @since 1.1 */ public interface IProjectConversionManager { @@ -33,8 +34,20 @@ public interface IProjectConversionManager { void convert(IProject project, Model model, IProgressMonitor monitor) throws CoreException; /** - * Returns an unmodifiable list of all {@link AbstractProjectConversionParticipant}s applying to this project + * Returns an unmodifiable list of all {@link AbstractProjectConversionParticipant}s applying + * to the given project. + * Packaging restrictions on {@link AbstractProjectConversionParticipant}s will be ignored. + * + * @deprecated since 1.3 Use {@link #getConversionParticipants(IProject, String)} instead. */ + @Deprecated List<AbstractProjectConversionParticipant> getConversionParticipants(IProject project) throws CoreException; + /** + * Returns an unmodifiable list of all {@link AbstractProjectConversionParticipant}s applying + * to the given project and packaging. + * + * @since 1.3 + */ + List<AbstractProjectConversionParticipant> getConversionParticipants(IProject project, String packaging) throws CoreException; } diff --git a/org.eclipse.m2e.jdt/plugin.xml b/org.eclipse.m2e.jdt/plugin.xml index 710dc209..698fd2b2 100644 --- a/org.eclipse.m2e.jdt/plugin.xml +++ b/org.eclipse.m2e.jdt/plugin.xml @@ -365,6 +365,12 @@ id="org.eclipse.m2e.jdt.javaProjectConversionParticipant" name="%java.conversion.participant.name" nature="org.eclipse.jdt.core.javanature"> - </projectConversionParticipant> + </projectConversionParticipant> + + <conversionParticipantConfiguration + conversionParticipantId="org.eclipse.m2e.jdt.javaProjectConversionParticipant" + compatiblePackagings="jar" + /> </extension> + </plugin> |