Maven plugin execution order and classes bundling

594 views Asked by At

I've been banging my head with maven plugin execution order and packaging of generated classes.

I've a requirement of generating pojos from XSD with annotation and for that I'm using maven-jaxb2-plugin which consumes a binding.xjb (a xml file consisting fully classified field names which is to be annotated) file and add annotations to generated pojos. Along with this I've a mechanism to generate binding.xjb file dynamically from a mapping file, the purpose is to just provide mapping in yaml file format instead of having a big binding.xjb because it's easy to maintain a yml file, for this I've a standalone spring boot app which reads yaml file and generate binding.xjb file and I'm using exec-maven-plugin to invoke my app before maven-jaxb2-plugin so that the binding.xjb gets generated beforehand and can be used by maven-jaxb2-plugin to annotate pojos.

And I know that to maintain plugin execution order they must be declared in same phase, so as to avoid hassle I'm using process-classes phase and with the help of this the pojos are generated with annotation.

But the issue is that the generated classes are not packed into jar also I would like to exclude my binding.xjb generation logic from generated jar file.

My build configuration looks like below:-

<build>
   <defaultGoal>install</defaultGoal>
   <plugins>
       <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>exec-maven-plugin</artifactId>
           <version>1.6.0</version>
           <executions>
               <execution>
                   <phase>process-classes</phase>
                   <goals>
                       <goal>java</goal>
                   </goals>
               </execution>
           </executions>
           <configuration>
               <mainClass>com.abc.GeneratorApp</mainClass>
           </configuration>
       </plugin>
       <plugin>
           <groupId>org.jvnet.jaxb2.maven2</groupId>
           <artifactId>maven-jaxb2-plugin</artifactId>
           <version>${maven-jaxb2-plugin.version}</version>
           <executions>
               <execution>
                   <phase>process-classes</phase>
                   <goals>
                       <goal>generate</goal>
                   </goals>
               </execution>
           </executions>

           <configuration>
               <extension>true</extension>
               <args>
                   <arg>-XautoNameResolution</arg>
                   <arg>-Xannotate</arg>
               </args>
               <schemas>
                   <schema>
                       <fileset>
                           <directory>src/main/xsd</directory>
                           <includes>
                               <include>PATH-TO-XSD/my.xsd</include>
                           </includes>
                       </fileset>
                   </schema>
               </schemas>
               <bindings>
                   <binding>
                       <fileset>
                           <directory>src/main/xsd</directory>
                           <includes>
                               <include>*.xjb</include>
                           </includes>
                       </fileset>
                   </binding>
               </bindings>
               <plugins>
                   <plugin>
                       <groupId>org.jvnet.jaxb2_commons</groupId>
                       <artifactId>jaxb2-basics-annotate</artifactId>
                       <version>0.6.4</version>
                   </plugin>
                   <plugin>
                       <groupId>com.abc.xyz</groupId>
                       <artifactId>my-api</artifactId>
                       <version>1.2.3</version>
                   </plugin>
               </plugins>
           </configuration>
       </plugin>
   </plugins>

Can someone please point me to mistake which I'm making or provide a suggestion to solve this issue, thanks in advance.

1

There are 1 answers

3
lexicore On

Code generation is normally done in generate-sources phase. Not sure why you prefer process-classes.

The order of plugin in the same phase should actually be the order in which they are listed in pom.xml. So exec-maven-plugin should be executed before maven-jaxb2-plugin. I thing there was bug with this in early versions of Maven 3, should have been fixed in 3.0.3+.

If nothing helps, move exec-maven-plugin to an earlier phase like generate-sources and maven-jaxb2-plugin to process-sources.