How to include automatically xmlbeans generated code into maven jar?

白昼怎懂夜的黑 提交于 2019-12-09 08:38:07

问题


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


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/3037472/how-to-include-automatically-xmlbeans-generated-code-into-maven-jar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!