Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jaxb2 regenerate classes with every mvn clean package

My pom looks like

<plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.12.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaLanguage>WSDL</schemaLanguage>
                <generateDirectory>src/main/java</generateDirectory>
                <schemaDirectory>src/main/resources/wsdl/</schemaDirectory>
                <schemaIncludes>
                    <include>*.xsd</include>
                    <include>draw/*.xsd</include>
                </schemaIncludes>
            </configuration>
        </plugin>

    </plugins>

When I do a mvn clean package it is built according to the configuration. However after I have built it once I don't want to build it every time I do a mvn clean package unless the XSD has been modified somehow.

How do I achieve this?

like image 1000
idipous Avatar asked Apr 26 '16 10:04

idipous


2 Answers

The default phase declared in this plugin for the goal <goal>generate</goal> is generate-resources. referring to Maven Default Lifecycle you can't skip phases. The mvn phase-x command always means "run all phases until phase-x, inclusive".

The option you may use is profile. so you can profile your project to be build with or without generating resources.

Example:

  <profiles>
    <profile>
        <id>GEN-RESOURCES</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.12.3</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <schemaLanguage>WSDL</schemaLanguage>
                        <generateDirectory>src/main/java</generateDirectory>
                        <schemaDirectory>src/main/resources/wsdl/</schemaDirectory>
                        <schemaIncludes>
                            <include>*.xsd</include>
                            <include>draw/*.xsd</include>
                        </schemaIncludes>
                    </configuration>
                </plugin>

            </plugins>
        </build>
    </profile>
</profiles>

to activate the profile use -P

mvn clean package -PGEN-RESOURCES

otherwise no thing will be generated Maven profile explained here

I hope this helps.

like image 164
Hisham Khalil Avatar answered Oct 18 '22 21:10

Hisham Khalil


Disclaimer: I'm the author of the maven-jaxb2-plugin.

First, do not generate into src/main/java.

Next, if configured correctly, the plugin will not regenerate unless something changes. The plugin gathers "source" and "target" files and generates if and only if timestamp of "source" file is newer that of the "target" files.

See Up-to-Date Checks in the documentation for more information on how it works.

This, however, assumes that the plugin can cleanly determine timestamps of both "source" and "target" files. This is one of the reasons why you should generate your code into target/generated-source/<dir> and not in src/main/java.

Next, this definitely does not work with mvn *clean* package as *clean* removes target and therefore target/generated-source/<dir>. So just run mvn package instead of mvn clean package.

like image 29
lexicore Avatar answered Oct 18 '22 20:10

lexicore