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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:09:01  来源:igfitidea点击:

How to add an extra source directory for maven to compile and include in the build jar?

javamavenmaven-2

提问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

NOTE: This solution will just move the java source files to the target/classes directory and will not compile the sources.注意:此解决方案只会将 java 源文件移动到 target/classes 目录,而不会编译源文件。

Update the pom.xmlas -

更新pom.xml为 -

<project>   
 ....
    <build>
      <resources>
        <resource>
          <directory>src/main/config</directory>
        </resource>
      </resources>
     ...
    </build>
...
</project>

回答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-pluginis not required if the folder that contains the source code to add in the build is located in the targetfolder 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/foowill 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