Jaxb2 regenerate classes with every mvn clean package
Asked Answered
D

2

6

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?

Doubleganger answered 26/4, 2016 at 10:48 Comment(2)
Generating into src/main/java is in general a bad idea...Epistrophe
you are right... I have fixed this thank youDoubleganger
C
7

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.

Causey answered 26/4, 2016 at 11:52 Comment(1)
While this would work, this is not the right direction. First, the maven-jaxb2-plugin has a built-in feature to regenerate only if schemas changed. Next, using profiles to turn on and off build steps will result in terrible build files. Therefore the downvote, sorry.Alert
A
7

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.

Alert answered 26/4, 2016 at 14:0 Comment(1)
Thank you I have changed from src/main/java and indeed it works as you described. When I do not use clean it does not build.Doubleganger

© 2022 - 2024 — McMap. All rights reserved.