users@jaxb.java.net

patch for maven-jaxb2-plugin

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Fri, 26 Jan 2007 15:15:17 -0800

Hi,

I made a patch for maven-jaxb2-plugin so that XJC plugins can be
specified as a configuration of the mojo. See below for an example.

Would it be possible to integrate that to the plugin? If it's OK, I'd
happy to commit. I'd also like to then release a new version of this
plugin, so that I can use this in another project of mine.


> <plugin>
> <groupId>org.jvnet.jaxb2.maven2</groupId>
> <artifactId>maven-jaxb2-plugin</artifactId>
> <executions>
> <execution>
> <!--phase>generate-sources</phase-->
> <goals>
> <goal>generate</goal>
> </goals>
> <configuration>
> <!-- if you want to put DTD somewhere else
> <schemaDirectory>src/main/jaxb</schemaDirectory>
> -->
> <extension>true</extension>
> <schemaLanguage>DTD</schemaLanguage>
> <schemaIncludes>
> <schemaInclude>*.dtd</schemaInclude>
> </schemaIncludes>
> <bindingIncludes>
> <bindingInclude>*.jaxb</bindingInclude>
> </bindingIncludes>
> <args>
> <arg>-Xinject-listener-code</arg>
> </args>
> <plugins>
> <plugin>
> <groupId>org.jvnet.jaxb2-commons</groupId>
> <artifactId>property-listener-injector</artifactId>
> <version>1.0</version>
> </plugin>
> </plugins>
> </configuration>
> </execution>
> </executions>
> </plugin>



-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com


Index: plugin/src/main/java/org/jvnet/jaxb2/maven2/AbstractXJC2Mojo.java
===================================================================
--- plugin/src/main/java/org/jvnet/jaxb2/maven2/AbstractXJC2Mojo.java (revision 49)
+++ plugin/src/main/java/org/jvnet/jaxb2/maven2/AbstractXJC2Mojo.java (working copy)
@@ -164,6 +164,8 @@
     // since this property should be present during classloading.
     Options xjcOpts = new Options();
 
+ xjcOpts.classpaths.addAll(this.getPluginURLs());
+
     xjcOpts.verbose = this.isVerbose();
     xjcOpts.debugMode = this.isDebug();
 
@@ -834,6 +836,8 @@
 
   public abstract MavenProject getProject();
 
+ public abstract List getPluginURLs() throws MojoExecutionException;
+
   protected class JaxbErrorReceiver4Mvn extends ErrorReceiver {
 
     public String stage = "processing";
Index: plugin/src/main/java/org/jvnet/jaxb2/maven2/XJC2Mojo.java
===================================================================
--- plugin/src/main/java/org/jvnet/jaxb2/maven2/XJC2Mojo.java (revision 49)
+++ plugin/src/main/java/org/jvnet/jaxb2/maven2/XJC2Mojo.java (working copy)
@@ -16,9 +16,17 @@
 
 import java.io.File;
 import java.util.List;
+import java.util.ArrayList;
+import java.net.MalformedURLException;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.*;
 
 /**
  * A mojo that uses JAXB 1.x XJC compiler to generate java source classes from
@@ -283,8 +291,35 @@
          * @readonly
          */
         protected MavenProject project;
-
- /**
+
+ /**
+ * XJC plugins to be made available to XJC.
+ * They still need to be activated by using &lt;args> and
+ * enable plugin activation option.
+ *
+ * @parameter
+ */
+ protected Artifact[] plugins;
+
+ /**
+ * Used internally to resolve {_at_link #plugins} to their jar files.
+ *
+ * @component
+ */
+ protected ArtifactResolver artifactResolver;
+
+ /**
+ * @component
+ */
+ protected ArtifactFactory artifactFactory;
+
+ /**
+ * @parameter expression="${localRepository}"
+ * @required
+ */
+ protected ArtifactRepository localRepository;
+
+ /**
          * Execute the maven2 mojo to invoke the xjc2 compiler based on any
          * configuration settings.
          *
@@ -478,4 +513,26 @@
                 return project;
         }
 
+ public List getPluginURLs() throws MojoExecutionException {
+ List urls = new ArrayList();
+ if(plugins!=null) {
+ for( int i=0; i<plugins.length; i++ ) {
+ try {
+ org.apache.maven.artifact.Artifact a = plugins[i].toArtifact(artifactFactory);
+ artifactResolver.resolve(a,
+ project.getRemoteArtifactRepositories(), localRepository);
+ urls.add( a.getFile().toURL() );
+ } catch (ArtifactResolutionException e) {
+ throw new MojoExecutionException("Error attempting to download the plugin: " + plugins[i], e);
+ } catch (ArtifactNotFoundException e) {
+ throw new MojoExecutionException("Plugin doesn't exist: " + plugins[i], e);
+ } catch (MalformedURLException e) {
+ // don't think can happen
+ throw new MojoExecutionException("Failed to obtain a plugin URL",e);
+ }
+ }
+ }
+ return urls;
+ }
+
 }
Index: plugin/src/main/java/org/jvnet/jaxb2/maven2/Artifact.java
===================================================================
--- plugin/src/main/java/org/jvnet/jaxb2/maven2/Artifact.java (revision 0)
+++ plugin/src/main/java/org/jvnet/jaxb2/maven2/Artifact.java (revision 0)
@@ -0,0 +1,40 @@
+package org.jvnet.jaxb2.maven2;
+
+import org.apache.maven.artifact.factory.ArtifactFactory;
+
+/**
+ * @author Kohsuke Kawaguchi
+ */
+public class Artifact {
+ private String groupId;
+ private String artifactId;
+ private String version;
+
+ public String getGroupId() {
+ return groupId;
+ }
+
+ public void setGroupId(String groupId) {
+ this.groupId = groupId;
+ }
+
+ public String getArtifactId() {
+ return artifactId;
+ }
+
+ public void setArtifactId(String artifactId) {
+ this.artifactId = artifactId;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public void setVersion(String version) {
+ this.version = version;
+ }
+
+ public org.apache.maven.artifact.Artifact toArtifact(ArtifactFactory factory) {
+ return factory.createArtifact(groupId,artifactId,version,null,"jar");
+ }
+}

Property changes on: plugin\src\main\java\org\jvnet\jaxb2\maven2\Artifact.java
___________________________________________________________________
Name: svn:eol-style
   + native