Author: tchemit Date: 2008-07-22 22:08:37 +0000 (Tue, 22 Jul 2008) New Revision: 761 Added: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractJaxxMojo.java Log: abstract Jaxx Mojo Copied: trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractJaxxMojo.java (from rev 759, trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractActionGeneratorGoal.java) =================================================================== --- trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractJaxxMojo.java (rev 0) +++ trunk/lutinjaxx/maven/src/main/java/org/codelutin/jaxx/AbstractJaxxMojo.java 2008-07-22 22:08:37 UTC (rev 761) @@ -0,0 +1,183 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.jaxx; + +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 java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Set; + +/** + * Le mojo de base pour les goals Jaxx. + * + * @author chemit + */ +public abstract class AbstractJaxxMojo extends AbstractMojo { + + /** + * @description D�pendance du projet. + * @parameter default-value="${project}" + * @readonly + */ + protected MavenProject project; + + /** + * @description R�pertoire de destination des fichiers java � g�n�rer. + * @parameter expression="${jaxx.outJava}" default-value="${basedir}/gen/java" + */ + protected File outJava; + + /** + * @description chemin du r�pertoire de g�n�ration des resources. + * @parameter expression="${jaxx.outResource}" default-value="${basedir}/gen/resources" + */ + protected File outResource; + + /** + * @description chemin du r�pertoire de compilation des resources. + * @parameter expression="${jaxx.outClass}" default-value="${basedir}/target/classes" + */ + protected File outClass; + + /** + * @description verbose + * @parameter expression="${jaxx.verbose}" default-value="${maven.verbose}" + */ + protected boolean verbose; + + + protected abstract void doExecute() throws Exception; + + public void execute() throws MojoExecutionException, MojoFailureException { + + init(); + + try { + + doExecute(); + + } catch (Exception e) { + getLog().error(e); + Throwable e2 = e; + while (e2.getCause() != null) { + e2 = e.getCause(); + } + getLog().error(e2); + + throw new MojoExecutionException(e.getMessage(), e); + } + + } + + protected void init() { + + if (!outResource.exists()) { + outResource.mkdirs(); + } + + if (!outJava.exists()) { + outJava.mkdirs(); + } + + } + + protected void fixCompileSourceRoots() { + if (!project.getCompileSourceRoots().contains(outJava.getPath())) { + project.addCompileSourceRoot(outJava.getPath()); + } + } + + @SuppressWarnings({"unchecked"}) + protected URLClassLoader initClassLoader(MavenProject project, Log log) { + URLClassLoader loader = null; + if (project != null) { + URLClassLoader result; + try { + Set<Artifact> compileClasspathElements = project.getArtifacts(); + URL[] url = new URL[compileClasspathElements.size() + 1]; + url[0] = outClass.toURI().toURL(); + int i = 1; + 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(); + } + i++; + } + //ClassLoader parent = Thread.currentThread().getContextClassLoader(); + if (compileClasspathElements.size() == 0) { + result = new URLClassLoader(url, getClass().getClassLoader()); + } else { + result = new URLClassLoader(url, getClass().getClassLoader()); + } + } catch (MalformedURLException eee) { + throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", outClass, eee.getMessage()), eee); + } catch (IOException e) { + throw new RuntimeException(_("Can't create ClassLoader for script, bad directory: {0} for reason {1}", outClass, e.getMessage()), e); + } + loader = result; + } + if (log.isDebugEnabled() && loader != null) { + for (URL entry : loader.getURLs()) { + log.info("outClass url " + entry); + } + } + return loader; + } + + public File getOutJava() { + return outJava; + } + + public void setOutJava(File outJava) { + this.outJava = outJava; + } + + public File getOutResource() { + return outResource; + } + + public void setOutResource(File outResource) { + this.outResource = outResource; + } + + public File getOutClass() { + return outClass; + } + + public void setOutClass(File outClass) { + this.outClass = outClass; + } + + public boolean isVerbose() { + return verbose; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } +} \ No newline at end of file