Java 如何为 maven 添加额外的源目录以编译并包含在构建 jar 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9752972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add an extra source directory for maven to compile and include in the build jar?
提问by chrisapotek
In addition to the src/main/java, I am adding a src/bootstrap directory that I want to include in my build process, in other words, I want maven to compile and include the sources there in my build. How!?
除了 src/main/java,我还添加了一个 src/bootstrap 目录,我想将它包含在我的构建过程中,换句话说,我希望 maven 编译并将其中的源代码包含在我的构建中。如何!?
采纳答案by Péter T?r?k
You can use the Build Helper Plugin, e.g:
您可以使用Build Helper Plugin,例如:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
回答by Arun
You can add the directories for your build process like:
您可以为构建过程添加目录,例如:
...
<resources>
<resource>
<directory>src/bootstrap</directory>
</resource>
</resources>
...
The src/main/java is the default path which is not needed to be mentioned in the pom.xml
src/main/java 是默认路径,在 pom.xml 中不需要提及
回答by Saikat
Update the pom.xml
as -
更新pom.xml
为 -
<project>
....
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
...
</build>
...
</project>
回答by Kalpesh Soni
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
also see
另见
回答by davidxxx
With recent Maven versions (3) and recent version of the maven compiler plugin (3.7.0), I notice that adding a source folder with the build-helper-maven-plugin
is not required if the folder that contains the source code to add in the build is located in the target
folder or a subfolder of it.
It seems that the compiler maven plugin compiles any java source code located inside this folder whatever the directory that contains them.
For example having some (generated or no) source code in target/a
, target/generated-source/foo
will be compiled and added in the outputDirectory : target/classes
.
使用最近的 Maven 版本 (3) 和最新版本的 maven 编译器插件 (3.7.0),我注意到build-helper-maven-plugin
如果包含要添加到构建中的源代码的文件夹位于target
文件夹或其子文件夹。
似乎编译器 maven 插件会编译位于此文件夹内的任何 Java 源代码,无论包含它们的目录是什么。
例如,在target/a
, 中有一些(生成或没有)源代码target/generated-source/foo
将被编译并添加到 outputDirectory : 中target/classes
。