maven can't add files in generated-sources for compilation phase

ぃ、小莉子 提交于 2019-11-29 01:27:08
Pascal Thivent

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.

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