[Lutingenerator-commits] r430 - in maven-generator-plugin/trunk: . src/main/java/org/codelutin/generator/plugin
Author: tchemit Date: 2009-02-11 19:31:11 +0000 (Wed, 11 Feb 2009) New Revision: 430 Modified: maven-generator-plugin/trunk/changelog.txt maven-generator-plugin/trunk/src/main/java/org/codelutin/generator/plugin/GeneratorPlugin.java Log: add testPhase property in mojo to specify when using it in test phase Modified: maven-generator-plugin/trunk/changelog.txt =================================================================== --- maven-generator-plugin/trunk/changelog.txt 2009-02-11 15:40:34 UTC (rev 429) +++ maven-generator-plugin/trunk/changelog.txt 2009-02-11 19:31:11 UTC (rev 430) @@ -1,4 +1,5 @@ 0.64 ?? 200812?? + * 20090211 [chatellier] add testPhase property in mojo to specify when using it in test phase * 20090210 [chatellier] add info and debug maven log messages * 20090210 [chatellier] add default value on all non required parameters * 20090209 [chemit] fix bug when using sibling dependencies in a multi-module project Modified: maven-generator-plugin/trunk/src/main/java/org/codelutin/generator/plugin/GeneratorPlugin.java =================================================================== --- maven-generator-plugin/trunk/src/main/java/org/codelutin/generator/plugin/GeneratorPlugin.java 2009-02-11 15:40:34 UTC (rev 429) +++ maven-generator-plugin/trunk/src/main/java/org/codelutin/generator/plugin/GeneratorPlugin.java 2009-02-11 19:31:11 UTC (rev 430) @@ -17,6 +17,7 @@ package org.codelutin.generator.plugin; +import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -42,10 +43,9 @@ * * @author ruchaud * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - * + * <p/> + * Last update: $Date$ + * by : $Author$ * @goal generate * @projectRequired true */ @@ -132,6 +132,14 @@ */ protected String encoding; + /** + * A flag to mark themojo to be used in a test phase. This will permits to add generated sources in test compile roots. + * + * @parameter expression="${generator.testPhase}" default-value="false" + * @since 0.64 + */ + protected boolean testPhase; + @Override public void execute() throws MojoExecutionException, MojoFailureException { @@ -174,7 +182,7 @@ generator.setIncludes(includes); generator.setExcludeTemplates(excludeTemplates == null || excludeTemplates.length == 0 ? java.util.Collections - .<String> emptyList() : java.util.Arrays + .<String>emptyList() : java.util.Arrays .asList(excludeTemplates)); ClassLoader loader = fixClassLoader(); @@ -188,17 +196,32 @@ * de compilation du projet Maven. */ protected void fixCompileSourceRoots() { + if (project==null) { + // no project defined, can not fix anything + // this case could appears if we wanted to do some tests of the plugin + return; + } - if (project != null - && !project.getCompileSourceRoots().contains( - destDirGen.getPath())) { - getLog().info("Add compile source root : " + destDirGen); - project.addCompileSourceRoot(destDirGen.getPath()); - Resource resources = new Resource(); - resources.setDirectory(destDirGen.getAbsolutePath()); - resources.setExcludes(Arrays.asList("**/*.java")); - getLog().info("Add resource root :" + resources); - project.addResource(resources); + if (testPhase) { + if (!project.getTestCompileSourceRoots().contains(destDirGen.getPath())) { + getLog().info("Add test compile source root : " + destDirGen); + project.addTestCompileSourceRoot(destDirGen.getPath()); + Resource resources = new Resource(); + resources.setDirectory(destDirGen.getAbsolutePath()); + resources.setExcludes(Arrays.asList("**/*.java")); + getLog().info("Add test resource root :" + resources); + project.addTestResource(resources); + } + } else { + if (!project.getCompileSourceRoots().contains(destDirGen.getPath())) { + getLog().info("Add compile source root : " + destDirGen); + project.addCompileSourceRoot(destDirGen.getPath()); + Resource resources = new Resource(); + resources.setDirectory(destDirGen.getAbsolutePath()); + resources.setExcludes(Arrays.asList("**/*.java")); + getLog().info("Add resource root :" + resources); + project.addResource(resources); + } } } @@ -215,29 +238,45 @@ protected ClassLoader fixClassLoader() throws MojoExecutionException { try { List<URL> urls = new ArrayList<URL>(); + ClassLoader loader = null; if (extraClassPathDirectory != null) { getLog().info("Add extra directory in generator's classLoader : " - + extraClassPathDirectory); + + extraClassPathDirectory); urls.add(extraClassPathDirectory.toURI().toURL()); } if (project.getProjectReferences() != null) { + // this case is for multi-module when calling from a parent module for (Object o : project.getProjectReferences().entrySet()) { Entry entry = (Entry) o; MavenProject relatedProject = (MavenProject) entry .getValue(); getLog().info("Add project reference in generator's classLoader : '" - + relatedProject.getArtifact() + "'"); + + relatedProject.getArtifact() + "'"); //TODO il faudrait peut-etre aussi ajouter les dependances ? urls.add(relatedProject.getArtifact().getFile().toURI() .toURL()); } } - // we ask to add the directory in classloader - ClassLoader loader = getClass().getClassLoader(); - if (!urls.isEmpty()) { - loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), - loader); + if (!project.getArtifacts().isEmpty()) { + // this is a special case when artifacts were resolved (for example in site phase) + getLog().info("Use resolved artifacts to build class-path"); + for (Object o : project.getArtifacts()) { + Artifact a = (Artifact) o; + if (!a.getScope().equals("provided")) { + urls.add(a.getFile().toURI().toURL()); + } + } + // using the strict class loader with only resolved artifacts + //loader = new URLClassLoader(urls.toArray(new URL[urls.size()])); } + //if (loader == null) { + // we ask to add the directory in classloader + loader = getClass().getClassLoader(); + if (!urls.isEmpty()) { + loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), + loader); + } + //} return loader; } catch (MalformedURLException e) { throw new MojoExecutionException(e.getMessage());
participants (1)
-
tchemit@users.labs.libre-entreprise.org