Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java diff -u maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java:1.4 maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java:1.5 --- maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java:1.4 Tue Jan 1 17:21:31 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java Thu Jan 3 05:46:45 2008 @@ -20,13 +20,24 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import static org.codelutin.i18n.I18n._; import org.codelutin.log.LutinLogFactory; import org.codelutin.util.StringUtil; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Set; +import java.util.jar.JarFile; + /** * Classe de base pour une goal de génération * @@ -42,6 +53,13 @@ /** * @description Dépendance du projet. + * @parameter default-value="${project}" + * @readonly + */ + protected MavenProject project; + + /** + * @description Dépendance du projet. * @parameter expression="${commandline.prefix}" * @required */ @@ -54,6 +72,13 @@ protected String i18nPrefix; /** + * @description classpath du projet qui utilise le plugin. + * @parameter expression="${commandline.cp}" default-value="${basedir}/target/classes" + * @required + */ + protected File cp; + + /** * @description flag pour afficher verbeusement ou non les logs * @parameter expression="${commandline.verbose}" default-value="${maven.verbose}" */ @@ -62,6 +87,8 @@ /** timestamp au démarrage (est positionné juste après l'init i18n) */ protected long t0; + protected ClassLoader loader; + protected AbstractGeneratorGoal() { } @@ -76,7 +103,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { t0 = System.currentTimeMillis(); - if (i18nPrefix==null) { + if (i18nPrefix == null) { i18nPrefix = prefix.toLowerCase(); } try { @@ -91,6 +118,8 @@ logger.setLevel(Level.INFO); } } + initClassLoader(); + initAction(); // do implented action @@ -102,9 +131,43 @@ } } + @SuppressWarnings({"unchecked"}) + protected ClassLoader initClassLoader() { + if (loader == null) { + ClassLoader result; + try { + Set compileClasspathElements = project.getArtifacts(); + URL[] url = new URL[compileClasspathElements.size()]; + int i = 0; + for (Artifact artifact : compileClasspathElements) { + File file = new File(artifact.getFile().getAbsolutePath()); + if (file.getName().endsWith(".jar")) { + url[i] = new URL("jar", "", file.toURI().toURL().toString()+"!/"); + } else { + url[i] = file.toURI().toURL(); + } + System.out.println("add url " + url[i]); + i++; + } + //ClassLoader parent = Thread.currentThread().getContextClassLoader(); + if (compileClasspathElements.size() == 0) { + result = new URLClassLoader(url, getClass().getClassLoader()); + } else { + result = new URLClassLoader(url); + } + } catch (MalformedURLException eee) { + throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", cp, eee.getMessage()), eee); + } catch (IOException e) { + throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", cp, e.getMessage()), e); + } + loader = result; + } + return loader; + } + protected void checkInstanceOf(String givenClass, Class expectedClass) { try { - Class clazz = Class.forName(givenClass,true,expectedClass.getClassLoader()); + Class clazz = Class.forName(givenClass, true, loader); if (!expectedClass.isAssignableFrom(clazz)) { throw new IllegalArgumentException("required a " + expectedClass + " class but found a " + clazz); }