Commit in maven-service-plugin/src/main/java/org/lcsim on MAIN
MavenServicePluginMojo.java+49-581.3 -> 1.4
JM: add this artifact's jar to classpath

maven-service-plugin/src/main/java/org/lcsim
MavenServicePluginMojo.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- MavenServicePluginMojo.java	29 Aug 2008 01:31:54 -0000	1.3
+++ MavenServicePluginMojo.java	10 Sep 2008 21:48:24 -0000	1.4
@@ -1,23 +1,21 @@
 package org.lcsim;
 
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.commons.io.DirectoryWalker;
-import org.apache.maven.project.MavenProject;
-import java.io.FileFilter;
+import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileFilter;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.net.URL;
-import java.util.List;
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.net.URLClassLoader;
-import java.io.BufferedReader;
-import java.io.FileReader;
-import java.util.StringTokenizer;
-import java.io.Writer;
-import java.io.FileWriter;
-import java.io.BufferedWriter;
+import java.util.List;
+
+import org.apache.commons.io.DirectoryWalker;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
 
 /**
  * A Maven plugin that generates a services file by walking a directory looking for 
@@ -48,21 +46,8 @@
 	 * @parameter expression="outputDirectory=${basedir}/src/main/resources/META-INF/services"
 	 */ 
 	private String outputDirectory;
-
-	/**
-	 * File containing full classpath that should be used by the services search.
-	 * Generate with <code>mvn dependency:build-classpath</code> saved to <code>target/classpath.txt</code>.
-	 * @parameter expression="classPathFile"
-	 * @required
-	 */
-	private String classPathFile;
 	
 	/**
-	 * @parameter expression="${project.build.directory}/${project.build.finalName}.jar"
-	 */
-	private String projectJar;
-
-	/**
 	 * The ClassLoader that will be used when instantiating classes to test against.
 	 */
 	private MyURLClassLoader loader;
@@ -76,36 +61,54 @@
 	 * Setup the custom ClassLoader with the full class path.
 	 * @throws Exception
 	 */
-	private void setupClassLoader() throws Exception
+	
+	/**
+	 * @parameter expression="${project}"
+	 */
+	private MavenProject mavenProject;
+	
+	private List<String> makeClassPathEntries() throws Exception
 	{
-		BufferedReader input =  new BufferedReader(new FileReader(new File(classPathFile)));
-		String line = null;
-		List<URL> urls = new ArrayList<URL>();        
-		while (( line = input.readLine()) != null)
+		List<String> classPathEntries = new ArrayList<String>();
+		classPathEntries.add(mavenProject.getArtifact().getFile().getCanonicalPath());
+		for (Object o : mavenProject.getArtifacts())
 		{
-			StringTokenizer tok = new StringTokenizer(line,":");
-			while(tok.hasMoreTokens()) 
-			{
-				String nextTok = tok.nextToken();
-				urls.add((new File(nextTok)).toURL());
-			}        	        	
+			Artifact artifact = (Artifact)o;
+			classPathEntries.add(artifact.getFile().getCanonicalPath());
+		}
+		return classPathEntries;
+	}
+	
+	private void setupClassLoader() throws Exception
+	{		
+		List<String> classPathEntries = makeClassPathEntries();
+		
+		List<URL> urls = new ArrayList<URL>();
+		for (String classPathEntry : classPathEntries)
+		{
+			urls.add((new File(classPathEntry)).toURL());
 		}
+				
 		URL urlsArr[] = new URL[urls.size()];
 		for (int i=0; i<urls.size(); i++)
 		{
 			urlsArr[i] = urls.get(i);
 		}        
 		loader = new MyURLClassLoader(urlsArr);
-		loader.addURL((new File(projectJar)).toURL());
+		
+		// DEBUG: print out class path entries
+		System.out.println("created class path ...");
+		for (URL url : loader.getURLs())
+		{
+			System.out.println(url.toString());
+		}		
 	}
 
 	public void execute() throws MojoExecutionException
 	{
-		if (scanDir == null) throw new RuntimeException("scanDir not set");
 		if (className == null) throw new RuntimeException("className not set");
 		if (outputDirectory == null) throw new RuntimeException("outputDirectory not set");
-		if (classPathFile == null) throw new RuntimeException("classPathFile not set");
-		
+				
 		try {
 			setupClassLoader();
 		}
@@ -159,31 +162,19 @@
 			super(filter, depth);
 		}
 
-		public void handleDirectoryStart(File directory, int depth, Collection results)
-		{
-/*			try 
-			{
-				System.out.println("adding to classpath:" + directory.toURL());
-				loader.addURL(directory.toURL());
-			}
-			catch (Exception x)
-			{
-				throw new RuntimeException(x);
-			}*/
-		}		
-
 		public void handleFile(File file, int depth, Collection results) 
 		{
 			try {		
 				// Load the class.
 				Class klass = 
 					loader.loadClass(file.getCanonicalPath().replace(baseDir.getCanonicalPath(),"").replace(".class","").replace("/",".").substring(1));
+				
 				// Check if service class is a superclass.
 				if (serviceClass.isAssignableFrom(klass))
-				{
-					// Add to results if not an inner class.
-                                        if (!klass.getName().contains("$"))
-					    results.add(klass);
+				{						
+					// Add to results if this isn't a nested class.
+					if (!klass.getName().contains("$"))
+						results.add(klass);
 				} 
 			}
 			catch (Exception x)
CVSspam 0.2.8