How to include automatically xmlbeans generated code into maven jar?
Asked Answered
D

2

13

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

Doughy answered 14/6, 2010 at 13:16 Comment(0)
B
16

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).

Butcherbird answered 14/6, 2010 at 15:55 Comment(2)
Do we have newer way of doing this? Still this plugin (its quite old) works but it may break in future since no new developments has been carried outPinprick
I've just committed the plugin. So XmlBeans 5.0.0 will integrate that feature.Piranesi
S
-3

Try this.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xmlbeans-maven-plugin</artifactId>
    <version>2.3.2</version>
    <executions>
        <execution>
            <id />
            <phase>generate-sources</phase>
            <goals>
                <goal>xmlbeans</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <schemaDirectory>src/main/xsd</schemaDirectory>
        <staleFile>${project.build.directory}/generated-sources/xmlbeans/.staleFlag</staleFile>
        <verbose>false</verbose>
        <quiet>false</quiet>
        <javaSource>1.6</javaSource>                    
    </configuration>
</plugin>
Stercoricolous answered 1/3, 2011 at 3:7 Comment(1)
An answer with sample code should always include why that code will work where the original poster's didn't.Antionetteantioxidant

© 2022 - 2024 — McMap. All rights reserved.