Author: fbricon
Date: 2011-05-30 07:36:51 -0400 (Mon, 30 May 2011)
New Revision: 31629
Modified:
trunk/maven/plugins/org.jboss.tools.maven.cdi/src/org/jboss/tools/maven/cdi/configurators/CDIProjectConfigurator.java
trunk/maven/plugins/org.jboss.tools.maven.jsf/src/org/jboss/tools/maven/jsf/configurators/JSFProjectConfigurator.java
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/Activator.java
Log:
JBIDE-8755 : add CDI Facet if the project has seam3 dependencies, add JSF Facet if is has
a dependency on seam-faces.
Modified:
trunk/maven/plugins/org.jboss.tools.maven.cdi/src/org/jboss/tools/maven/cdi/configurators/CDIProjectConfigurator.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.cdi/src/org/jboss/tools/maven/cdi/configurators/CDIProjectConfigurator.java 2011-05-30
11:34:35 UTC (rev 31628)
+++
trunk/maven/plugins/org.jboss.tools.maven.cdi/src/org/jboss/tools/maven/cdi/configurators/CDIProjectConfigurator.java 2011-05-30
11:36:51 UTC (rev 31629)
@@ -10,6 +10,10 @@
************************************************************************************/
package org.jboss.tools.maven.cdi.configurators;
+import java.util.List;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
@@ -27,6 +31,7 @@
import org.eclipse.wst.common.project.facet.core.IProjectFacet;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
+import org.jboss.tools.cdi.core.CDICoreNature;
import org.jboss.tools.cdi.core.CDIUtil;
import org.jboss.tools.maven.cdi.MavenCDIActivator;
import org.jboss.tools.maven.cdi.Messages;
@@ -54,6 +59,7 @@
protected static final IProjectFacet m2Facet;
protected static final IProjectFacetVersion m2Version;
+ private static final String DEFAULT_CDI_VERSION = "1.0";
static {
dynamicWebFacet = ProjectFacetsManager.getProjectFacet("jst.web");
//$NON-NLS-1$
@@ -61,7 +67,7 @@
ejbFacet = ProjectFacetsManager.getProjectFacet("jst.ejb"); //$NON-NLS-1$
ejbVersion = ejbFacet.getVersion("3.0"); //$NON-NLS-1$
cdiFacet = ProjectFacetsManager.getProjectFacet("jst.cdi"); //$NON-NLS-1$
- cdiVersion = cdiFacet.getVersion("1.0"); //$NON-NLS-1$
+ cdiVersion = cdiFacet.getVersion(DEFAULT_CDI_VERSION); //$NON-NLS-1$
m2Facet = ProjectFacetsManager.getProjectFacet("jboss.m2"); //$NON-NLS-1$
m2Version = m2Facet.getVersion("1.0"); //$NON-NLS-1$
}
@@ -81,11 +87,15 @@
if (!configureCDI) {
return;
}
-
+ final IFacetedProject fproj = ProjectFacetsManager.create(project);
+ if (project.hasNature(CDICoreNature.NATURE_ID)
+ && (fproj == null || (fproj.hasProjectFacet(cdiFacet) &&
fproj.hasProjectFacet(m2Facet)))) {
+ //everything already installed. Since there's no support for version update -yet-
we stop here
+ return;
+ }
String packaging = mavenProject.getPackaging();
String cdiVersion = getCDIVersion(mavenProject);
if (cdiVersion != null) {
- final IFacetedProject fproj = ProjectFacetsManager.create(project);
if ( (fproj != null) && ("war".equals(packaging) ||
"ejb".equals(packaging)) ) { //$NON-NLS-1$
installDefaultFacets(fproj, cdiVersion, monitor);
}
@@ -121,6 +131,7 @@
}
+ @SuppressWarnings("unchecked")
private void installDefaultFacets(IFacetedProject fproj, String
cdiVersion,IProgressMonitor monitor) throws CoreException {
IProjectFacetVersion currentWebVersion = fproj.getProjectFacetVersion(dynamicWebFacet);
IProjectFacetVersion currentEjbVersion = fproj.getProjectFacetVersion(ejbFacet);
@@ -152,7 +163,36 @@
private String getCDIVersion(MavenProject mavenProject) {
String version = Activator.getDefault().getDependencyVersion(mavenProject,
CDI_API_GROUP_ID, CDI_API_ARTIFACT_ID);
+ if (version == null) {
+ version = inferCdiVersionFromDependencies(mavenProject);
+ }
return version;
}
+ private String inferCdiVersionFromDependencies(MavenProject mavenProject) {
+ boolean hasCandidates = false;
+ String cdiVersion = null;
+ List<ArtifactRepository> repos = mavenProject.getRemoteArtifactRepositories();
+ for (Artifact artifact : mavenProject.getArtifacts()) {
+ if (isKnownCdiExtension(artifact)) {
+ hasCandidates = true;
+ cdiVersion = Activator.getDefault().getDependencyVersion(artifact, repos,
CDI_API_GROUP_ID, CDI_API_ARTIFACT_ID);
+ if (cdiVersion != null) {
+ //TODO should probably not break and take the highest version returned from all
dependencies
+ break;
+ }
+ }
+ }
+ //Fallback to default CDI version
+ if (hasCandidates && cdiVersion == null) {
+ return DEFAULT_CDI_VERSION;
+ }
+ return cdiVersion;
+ }
+
+ private boolean isKnownCdiExtension(Artifact artifact) {
+ return artifact.getGroupId().startsWith("org.jboss.seam.")
+ && artifact.getVersion().startsWith("3.");
+ }
+
}
Modified:
trunk/maven/plugins/org.jboss.tools.maven.jsf/src/org/jboss/tools/maven/jsf/configurators/JSFProjectConfigurator.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.jsf/src/org/jboss/tools/maven/jsf/configurators/JSFProjectConfigurator.java 2011-05-30
11:34:35 UTC (rev 31628)
+++
trunk/maven/plugins/org.jboss.tools.maven.jsf/src/org/jboss/tools/maven/jsf/configurators/JSFProjectConfigurator.java 2011-05-30
11:36:51 UTC (rev 31629)
@@ -12,7 +12,10 @@
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.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
@@ -67,12 +70,13 @@
protected static final IProjectFacetVersion jsfVersion11;
protected static final IProjectFacet m2Facet;
protected static final IProjectFacetVersion m2Version;
+ private static final String JSF_VERSION_2_0 = "2.0";
static {
dynamicWebFacet = ProjectFacetsManager.getProjectFacet("jst.web");
//$NON-NLS-1$
dynamicWebVersion = dynamicWebFacet.getVersion("2.5"); //$NON-NLS-1$
jsfFacet = ProjectFacetsManager.getProjectFacet("jst.jsf"); //$NON-NLS-1$
- jsfVersion20 = jsfFacet.getVersion("2.0"); //$NON-NLS-1$
+ jsfVersion20 = jsfFacet.getVersion(JSF_VERSION_2_0);
jsfVersion12 = jsfFacet.getVersion("1.2"); //$NON-NLS-1$
jsfVersion11 = jsfFacet.getVersion("1.1"); //$NON-NLS-1$
m2Facet = ProjectFacetsManager.getProjectFacet("jboss.m2"); //$NON-NLS-1$
@@ -95,13 +99,16 @@
return;
}
+ final IFacetedProject fproj = ProjectFacetsManager.create(project);
+ if (fproj != null && fproj.hasProjectFacet(jsfFacet) &&
fproj.hasProjectFacet(m2Facet)) {
+ //everything already installed. Since there's no support for version update -yet-
we bail
+ return;
+ }
+
String packaging = mavenProject.getPackaging();
String jsfVersion = getJSFVersion(mavenProject);
- if (jsfVersion != null) {
- final IFacetedProject fproj = ProjectFacetsManager.create(project);
- if (fproj != null && "war".equals(packaging)) { //$NON-NLS-1$
- installWarFacets(fproj, jsfVersion, mavenProject, monitor);
- }
+ if (fproj != null && jsfVersion != null &&
"war".equals(packaging)) { //$NON-NLS-1$
+ installWarFacets(fproj, jsfVersion, mavenProject, monitor);
}
}
@@ -323,7 +330,38 @@
if (version == null) {
version = Activator.getDefault().getDependencyVersion(mavenProject, JSF_API2_GROUP_ID,
JSF_API_ARTIFACT_ID);
}
+ if (version == null) {
+ version = inferJsfVersionFromDependencies(mavenProject, JSF_API2_GROUP_ID,
JSF_API_ARTIFACT_ID, JSF_VERSION_2_0);
+ }
return version;
}
+ private String inferJsfVersionFromDependencies(MavenProject mavenProject, String
groupId, String artifactId, String defaultVersion) {
+ boolean hasCandidates = false;
+ String jsfVersion = null;
+ List<ArtifactRepository> repos = mavenProject.getRemoteArtifactRepositories();
+ for (Artifact artifact : mavenProject.getArtifacts()) {
+ if (isKnownJsfBasedArtifact(artifact)) {
+ hasCandidates = true;
+ jsfVersion = Activator.getDefault().getDependencyVersion(artifact, repos, groupId,
artifactId);
+ if (jsfVersion != null) {
+ //TODO should probably not break and take the highest version returned from all
dependencies
+ break;
+ }
+ }
+ }
+ //Fallback to default JSF version
+ if (hasCandidates && jsfVersion == null) {
+ return defaultVersion;
+ }
+ return jsfVersion;
+ }
+
+ private boolean isKnownJsfBasedArtifact(Artifact artifact) {
+ return artifact.getGroupId().startsWith("org.jboss.seam.") //$NON-NLS-1$
+ && artifact.getArtifactId().equals("seam-faces") //$NON-NLS-1$
+ && artifact.getVersion().startsWith("3."); //$NON-NLS-1$
+ }
+
+
}
Modified:
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/Activator.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/Activator.java 2011-05-30
11:34:35 UTC (rev 31628)
+++
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/Activator.java 2011-05-30
11:36:51 UTC (rev 31629)
@@ -10,14 +10,24 @@
************************************************************************************/
package org.jboss.tools.maven.ui;
+import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.project.MavenProject;
+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.Status;
+import org.eclipse.m2e.core.MavenPlugin;
+import org.eclipse.m2e.core.internal.embedder.MavenImpl;
+import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
@@ -136,4 +146,70 @@
return null;
}
+ @SuppressWarnings("restriction")
+ public String getDependencyVersion(Artifact artifact, List<ArtifactRepository>
remoteRepos, String gid, String aid) {
+ IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(
+ artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
+ //If the artifact is a workspace dependency, the mavenProject is already loaded
+ if (facade != null) {
+ return getDependencyVersion(facade.getMavenProject(), gid, aid);
+ }
+
+ //look at the artifact pom file and if it exists, load the corresponding
MavenProject
+ File pom = getPomFile(artifact);
+ String version = null;
+ if (pom != null) {
+ MavenProject mavenProject = null;
+ MavenImpl maven = (MavenImpl)MavenPlugin.getMaven();
+ try {
+ //Create a custom execution request
+ IProgressMonitor monitor = new NullProgressMonitor();
+ MavenExecutionRequest request = maven.createExecutionRequest(monitor);
+ for (ArtifactRepository repo : remoteRepos) {
+ request.addRemoteRepository(repo);
+ }
+ request.setPom(pom);
+ request.getProjectBuildingRequest().setResolveDependencies(true);
+
+ //Load the MavenProject
+ MavenExecutionResult result = maven.readProject(request, monitor);
+ //log errors
+ if (result.hasExceptions()) {
+ for (Throwable e : result.getExceptions()) {
+ log(e);
+ }
+ } else {
+ mavenProject = result.getProject();
+ if (mavenProject != null) {
+ //finally look at the dependency version
+ version = getDependencyVersion(mavenProject , gid, aid);
+ //Detach the mavenProject from the maven session to avoid memory leaks
+ maven.detachFromSession(mavenProject);
+ }
+ }
+ } catch (CoreException e) {
+ //Don't crash on failures to read the dependency version
+ log(e);
+ }
+ }
+ return version;
+ }
+
+ private File getPomFile(Artifact artifact) {
+ File artifactFile = artifact.getFile();
+ if (artifactFile != null) {
+ String path = artifactFile.getAbsolutePath();
+ int lastIndexOfDot = path.lastIndexOf("."); //$NON-NLS-1$
+ if (lastIndexOfDot > 0) {
+ String pomPath = path.substring(0, lastIndexOfDot)+".pom"; //$NON-NLS-1$
+ File pomFile = new File(pomPath);
+ if (pomFile.exists() && pomFile.canRead()) {
+ return pomFile;
+ }
+ }
+ //TODO look inside the artifact jar if pom isn't available
+ }
+ return null;
+ }
+
}