Search code examples
maven-2xmlbeans

How to include automatically xmlbeans generated code into maven jar?


I have a project which uses Apache Xmlbeans for databinding. Currently it is very simple it only has some Schema-Files in src/main/xsd and xsdconfig in src/main/xsdconfig.

I want to include the generated Classes into the generated jar-File. It works if I specify the xmlbeans goal: "mvn xmlbeans:xmlbeans package" --> Creates a Jar with the xmlbeans classes

But I want to do this within the normal build cycle: "mvn package" --> should create a jar with the xmlbeans classes, but won't.

The pom is the following:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.test</groupId>
  <artifactId>xmlbeans-maven-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <build>
   <pluginManagement>
    <plugins>
     <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>maven-xmlbeans-plugin</artifactId>
          <version>2.3.3</version>
     </plugin>
    </plugins>
   </pluginManagement>
  </build>


  <dependencies>
    <dependency>
      <groupId>org.apache.xmlbeans</groupId>
      <artifactId>xmlbeans</artifactId>
      <version>2.4.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

I tried to bind it manually to the "generate-sources" (And to the "compile" phase, too) phase, but it does not work.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>de.leradon</groupId>
  <artifactId>xmlbeans-maven</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <build>
   <pluginManagement>
    <plugins>
     <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>maven-xmlbeans-plugin</artifactId>
          <version>2.3.3</version>
          <executions>
             <execution>
                <phase>generate-sources</phase>
                <goals>
                  <goal>xmlbeans</goal>
                </goals>
             </execution>
          </executions>
     </plugin>

    </plugins>
   </pluginManagement>
  </build>


  <dependencies>
    <dependency>
      <groupId>org.apache.xmlbeans</groupId>
      <artifactId>xmlbeans</artifactId>
      <version>2.4.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

How can I configure the plugin, so that when I run "mvn package" all the generated classes are packaged into the jar?

Greetings, lerad


Solution

  • If you configure the plugin under pluginManagement, you still need to declare it under plugins. To simplify, I'm not using the pluginManagement in the pom.xml below:

    <project>
      ...
      <dependencies>
        ...
        <dependency>
          <groupId>org.apache.xmlbeans</groupId>
          <artifactId>xmlbeans</artifactId>
          <version>2.4.0</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
      <build>
        <plugins>
          ...
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>xmlbeans-maven-plugin</artifactId>
            <version>2.3.3</version>
            <executions>
              <execution>
                <phase>generate-sources</phase>
                <goals>
                  <goal>xmlbeans</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    With this POM (and some XSD in src/main/xsd which is the default location), running mvn clean package just works (i.e. sources are generated from the XSD, compiled and packaged as part of the build).