From 43fa7eb6d1d07b00bc1f57e9c269ae1da1f2675f Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 14 Nov 2008 20:58:05 +0000 Subject: Add explicit classpath entries for test/runtime scoped dependencies git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@714135 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/tools/bundle/plugin/EclipsePluginMojo.java | 102 ++++++++++++++++++++- 1 file changed, 97 insertions(+), 5 deletions(-) (limited to 'branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main') diff --git a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/EclipsePluginMojo.java b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/EclipsePluginMojo.java index 5ed0ed3a48..c40020aee8 100644 --- a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/EclipsePluginMojo.java +++ b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/EclipsePluginMojo.java @@ -29,6 +29,8 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -235,12 +237,71 @@ public class EclipsePluginMojo extends AbstractMojo { */ private static final String ATTR_SRC = "src"; + /** + * Attribute value for kind: var + */ + private static final String ATTR_VAR = "var"; + + /** + * Attribute value for kind: lib + */ + private static final String ATTR_LIB = "lib"; + + /** + * Eclipse build path variable M2_REPO + */ + private static final String M2_REPO = "M2_REPO"; + /** * Element for classpathentry. */ private static final String ELT_CLASSPATHENTRY = "classpathentry"; private static final String ELT_CLASSPATH = "classpath"; + /** + * @parameter expression="${localRepository}" + * @required + * @readonly + */ + private ArtifactRepository localRepository; + + /** + * If the executed project is a reactor project, this will contains the full list of projects in the reactor. + * + * @parameter expression="${reactorProjects}" + * @required + * @readonly + */ + protected List reactorProjects; + + /** + * Utility method that locates a project producing the given artifact. + * + * @param artifact the artifact a project should produce. + * @return true if the artifact is produced by a reactor project. + */ + private boolean isAvailableAsAReactorProject(Artifact artifact) { + if (reactorProjects != null) { + for (Iterator iter = reactorProjects.iterator(); iter.hasNext();) { + MavenProject reactorProject = (MavenProject)iter.next(); + + if (reactorProject.getGroupId().equals(artifact.getGroupId()) && reactorProject.getArtifactId() + .equals(artifact.getArtifactId())) { + if (reactorProject.getVersion().equals(artifact.getVersion())) { + return true; + } else { + getLog().info("Artifact " + artifact.getId() + + " already available as a reactor project, but with different version. Expected: " + + artifact.getVersion() + + ", found: " + + reactorProject.getVersion()); + } + } + } + } + return false; + } + private static String getCanonicalPath(File file) throws MojoExecutionException { try { return file.getCanonicalPath(); @@ -289,7 +350,7 @@ public class EclipsePluginMojo extends AbstractMojo { * @readonly */ private MavenProject project; - + /** * Skip the operation when true. * @@ -297,7 +358,6 @@ public class EclipsePluginMojo extends AbstractMojo { */ private boolean skip; - private EclipseSourceDir[] buildDirectoryList() throws MojoExecutionException { File buildOutputDirectory = new File(project.getBuild().getOutputDirectory()); File basedir = project.getBasedir(); @@ -469,7 +529,7 @@ public class EclipsePluginMojo extends AbstractMojo { List specialSources = new ArrayList(); // Map> - Map> byOutputDir = new HashMap>(); + Map> byOutputDir = new HashMap>(); for (int j = 0; j < dirs.length; j++) { EclipseSourceDir dir = dirs[j]; @@ -526,7 +586,7 @@ public class EclipsePluginMojo extends AbstractMojo { writer.print(" <" + ELT_CLASSPATHENTRY); - writer.print(" " + ATTR_KIND + "=\"src\""); + writer.print(" " + ATTR_KIND + "=\"" + ATTR_SRC + "\""); writer.print(" " + ATTR_PATH + "=\"" + dir.getPath() + "\""); if (!isSpecial && dir.getOutput() != null && !defaultOutput.equals(dir.getOutput())) { @@ -551,7 +611,39 @@ public class EclipsePluginMojo extends AbstractMojo { writer.println("/>"); } - writer.println(" "); + + for (Object a : project.getArtifacts()) { + Artifact artifact = (Artifact)a; + if (Artifact.SCOPE_TEST.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact.getScope())) { + if (getLog().isDebugEnabled()) { + getLog().debug("Adding explict classpath entry: " + artifact.toString()); + } + + String path = "", kind = ""; + if (isAvailableAsAReactorProject(artifact)) { + path = "/" + artifact.getArtifactId(); + kind = ATTR_SRC; + } else { + String fullPath = artifact.getFile().getPath(); + String relativePath = + toRelativeAndFixSeparator(new File(localRepository.getBasedir()), new File(fullPath), false); + + if (!new File(relativePath).isAbsolute()) { + path = M2_REPO + "/" + relativePath; + kind = ATTR_VAR; + } else { + path = relativePath; + kind = ATTR_LIB; + } + } + writer.print(" <" + ELT_CLASSPATHENTRY); + + writer.print(" " + ATTR_KIND + "=\"" + kind + "\""); + writer.print(" " + ATTR_PATH + "=\"" + path + "\""); + writer.println("/>"); + } + } + writer.println(" "); writer.println(""); writer.close(); } -- cgit v1.2.3