18

I use Apache Thrift to generate code in target/generated-sources.

The Thrift compiler produces a directory named gen-java which contains all the Java code. When I execute mvn compile, the code is generated correctly in target/generated-source/gen-java, but in compilation phase, it complains can't find the classes which defined in gen-java.

In my understanding, Maven 2 automatically adds generated sources, is that right?

And what if my testing code also depends on the generated-sources, do I have to manually specified the compiler includes?

Sled
  • 18,541
  • 27
  • 119
  • 168
SunLiWei
  • 1,313
  • 4
  • 11
  • 30
  • possible duplicate of [Why is Maven skipping over my custom generate-sources executions?](http://stackoverflow.com/questions/18721684/why-is-maven-skipping-over-my-custom-generate-sources-executions) – Sled Jan 30 '14 at 18:11
  • [Why is Maven skipping over my custom generate-sources executions?](http://stackoverflow.com/q/18721684/1523648) is about a configuration error of the antrun plugin, this question isn't. – oberlies Jan 31 '14 at 08:49

2 Answers2

22

In my understanding, maven 2 automatically add generated sources, is that right?

Nothing automatic, plugins generating source code typically handle that by adding their output directory (something like target/generated-sources/<tool> by convention) as source directory to the POM so that it will be included later during the compile phase.

Some less well implemented plugins don't do that for you and you have to add the directory yourself, for example using the Build Helper Maven Plugin.

And since you didn't provide any POM snippet, any link, I can't say anything more.

And what if my testing code also depends on the generated-sources, do I have to manually specified the compiler includes?

As I said, generated sources are usually added as source directory and compiled and are thus available on the test classpath without you having to do anything.

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 5
    _plugins generating source code typically handle that by adding their output directory as source directory to the POM_ Would you have any example of such a configuration ? I'm trying to create a not so "less well implemented plugin" which generate code but I cannot find the best way to include the output directory without having to configure it into the "client" side. – Remi Mélisson Sep 18 '12 at 10:57
  • 4
    @Pascal What about `/target/generated-sources/` itself, does that need to be added as well to the build path? – Sled Jan 30 '14 at 18:10
5

Generated sources are not compiled or packaged automatically. Some IDEs (i.e. IntelliJ) will however show them as source folders.

To make generated sources visible to maven add a add-source-step to the build/plugins node of your pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/gen-java</source><!-- adjust folder name to your needs -->
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
slartidan
  • 20,403
  • 15
  • 83
  • 131