0

This question, just to be sure my interpretation is correct :

I'm using Mojohaus jaxb2-maven-plugin to generate java classes from .xsd files, and by default it puts them in target/generated-sources

Now, I want to get track of these classes in source control (target is of course excluded), and I may one day slightly customize one with an annotation or a line of code, and I may even change my class generation plugin, so what do is I copy these classes and packages in src/main/java

This upsets Maven when I try to compile because he considers "target/generated-sources" as a source directory and he finds all clases twice. For what I understand, I can exclude classes inside a source directory, but I can't remove a source directory from Maven build, am I right ?

So the only solution would be to configure my jaxb2 plugin to generate the classes elsewhere, right ?

UPDATE :

Ok, this doesn't work as I thought, if I change the outputDirectory of my jaxb plugin, it's still included as a source directory by Maven, and I have no clue why.

    <configuration>      
                <outputDirectory>${project.build.directory}/tatata/jaxb</outputDirectory>
    </configuration>

UPDATE 2 : The explanation is the plugin is adding the outputDirectory as a maven source directory during the generate-sources phase of the build, and it's not optionnal or customizable.

Tristan
  • 8,733
  • 7
  • 48
  • 96
  • Please clarify which plugin you use, `maven-jaxb2-plugin` or the Codehaus `jaxb2-maven-plugin`. – lexicore Mar 05 '18 at 18:30
  • 1
    You mean something more clear than "I'm using Mojohaus jaxb2-maven-plugin to generate java classes" which was in the original question ? ;) Codehaus has shutdown 3 years ago : http://www.mojohaus.org/ – Tristan Mar 05 '18 at 21:03
  • yes, something slightly more clear. The name of the Mojohaus plugin is `maven-jaxb2-plugin` , whereas you specified `jaxb2-maven-plugin` , which is the name of the Codehaus plugin. So you can understand the request for clarification by @lexicore. The codehaus plugin is still available even if Codehaus was shutdown. See also, here: https://stackoverflow.com/a/9199036/48082 – Cheeso May 10 '18 at 21:10

3 Answers3

4

First things first, do not add generation code to source control. Do not modify it manually. You will get into trouble. Believe me, I've seen it too many times. A new version of the schema and you're lost.


Ok, now to your question.

With maven-jaxb2-plugin you could turn off adding generation directory as a compile source root with:

<configuration>
    <addCompileSourceRoot>false</addCompileSourceRoot>
</configuration>

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

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Yes I have to say your plugin looks more flexible than the other, and I may switch to it. Now about versionning the generated code, it's only because the generation part will just stop after a few iterations, and all the minor changes which would occur afterwards will be dealt manually. Just a productive way to work and avoid too much unnecessary binding conf, and a way to keep track of working sources if any maven plugin upgrade or maven upgrade breaks the build (seen too many times ;) – Tristan Mar 05 '18 at 20:59
0

The answer from Lexicore is an interesting lead but my question was about the plugin I'm currently using, not how to do it with an other plugin.

So here is the workaround for the Mojohaus plugin : you can just skip the generate-sources by default (no need to do this task at every build when your model changes once in a week, then once in a year), and trigger it only when needed using a dedicated maven profile : How to skip generate-sources in Maven

Tristan
  • 8,733
  • 7
  • 48
  • 96
-2

you can always specify the target directory(generateDirectory) in pom config file as below. Hope it helps

`

<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>${basedir}/src/main/java</generateDirectory>
                            <generatePackage>com.myproj.proxy</generatePackage>
                            <schemas>
                                <schema>
                                    <!-- <url>${project.basedir}/src/main/resources/wsdl/test.wsdl</url> -->
                                    <fileset>
                                        <!-- Defaults to schemaDirectory. -->
                                        <directory>${basedir}/src/main/resources/wsdl</directory>
                                        <!-- Defaults to schemaIncludes. -->
                                        <includes>
                                            <include>*.wsdl</include>
                                        </includes>
                                    </fileset>
                                </schema>
                            </schemas>
                        </configuration>
                    </plugin>

`

java1977
  • 398
  • 4
  • 12
  • 25
  • Yes you can, but does it solve the main problem ? I've just done it (see UPDATE), and it doesn't change anything. – Tristan Mar 05 '18 at 17:20
  • You probably dont want to generate the stubs for every maven build, you can have the generate goal as a different profile. And when you run this profile clean your target directory. – java1977 Mar 05 '18 at 17:58
  • 2
    Note that `maven-jaxb2-plugin` is not `jaxb2-maven-plugin`. – lexicore Mar 05 '18 at 18:29