Java Maven 使用多个 src 目录编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/270445/
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
Maven compile with multiple src directories
提问by sal
Is there a way to compile multiple java source directories in a single maven project?
有没有办法在单个 maven 项目中编译多个 java 源目录?
采纳答案by sal
You can add a new source directory with build-helper:
您可以使用 build-helper 添加新的源目录:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
回答by sal
This worked for me
这对我有用
<build>
<sourceDirectory>.</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>src/main/java/**/*.java</include>
<include>src/main2/java/**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
回答by arntg
Used the build-helper-maven-plugin from the post - and update src/main/generated. And mvn clean compile works on my ../common/src/main/java, or on ../common, so kept the latter. Then yes, confirming that IntelliJ IDEA (ver 10.5.2) level of the compilation failed as David Phillips mentioned. The issue was that IDEA did not add another source root to the project. Adding it manually solved the issue. It's not nice as editing anything in the project should come from maven and not from direct editing of IDEA's project options. Yet I will be able to live with it until they support build-helper-maven-plugin directly such that it will auto add the sources.
使用了 post 中的 build-helper-maven-plugin - 并更新 src/main/generated。并且 mvn clean compile 适用于我的 ../common/src/main/java 或 ../common,所以保留了后者。然后是的,确认编译的 IntelliJ IDEA(版本 10.5.2)级别失败,正如 David Phillips 提到的那样。问题是 IDEA 没有向项目添加另一个源根。手动添加它解决了这个问题。这并不好,因为编辑项目中的任何内容都应该来自 maven 而不是直接编辑 IDEA 的项目选项。然而,我将能够忍受它,直到他们直接支持 build-helper-maven-plugin 以便它会自动添加源。
Then needed another workaround to make this work though. Since each time IDEA re-imported maven settings after a pom change me newly added source was kept on module, yet it lost it's Source Folders selections and was useless. So for IDEA - need to set these once:
然后需要另一种解决方法来完成这项工作。由于每次在 pom 更改后 IDEA 重新导入 maven 设置时,我都会将新添加的源保留在模块中,但它丢失了源文件夹选择并且无用。所以对于 IDEA - 需要设置一次:
- Select - Project Settings / Maven / Importing / keep source and test folders on reimport.
- Add - Project Structure / Project Settings / Modules / {Module} / Sources / Add Content Root.
- 选择 - 项目设置/Maven/导入/重新导入时保留源和测试文件夹。
- 添加 - 项目结构/项目设置/模块/{模块}/来源/添加内容根。
Now keeping those folders on import is not the best practice in the world either, ..., but giving it a try.
现在将这些文件夹保持在导入状态也不是世界上的最佳实践,...,但可以尝试一下。
回答by domi.vds
to make it work in intelliJ, you can also add
要使其在 intelliJ 中工作,您还可以添加
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
to maven-compiler-plugin
到 maven-compiler-plugin
回答by ursa
This can be done in two steps:
这可以分两步完成:
- For each source directory you should create own module.
- In all modules you should specify the same build directory:
${build.directory}
- 对于每个源目录,您应该创建自己的模块。
- 在所有模块中,您应该指定相同的构建目录:
${build.directory}
If you work with started Jetty (jetty:run
), then recompilation of any class in any module (with Maven, IDEA or Eclipse) will lead to Jetty's restart. The same behavior you'll get for modified resources.
如果您使用已启动的 Jetty ( jetty:run
),那么在任何模块(使用 Maven、IDEA 或 Eclipse)中重新编译任何类都将导致 Jetty 重新启动。对于修改后的资源,您将获得相同的行为。
回答by sendon1982
This also works with maven by defining the resources tag. You can name your src folder names whatever you like.
这也适用于 maven 通过定义资源标签。您可以随意命名您的 src 文件夹名称。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/generated</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
回答by comeGetSome
I naively do it this way :
我天真地这样做:
<build>
<finalName>osmwse</finalName>
<sourceDirectory>src/main/java, src/interfaces, src/services</sourceDirectory>
</build>
回答by Prabhu
In the configuration, you can use <compileSourceRoots>
.
在配置中,您可以使用<compileSourceRoots>
.
oal: org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-cli)
[DEBUG] Style: Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<basedir default-value="${basedir}"/>
<buildDirectory default-value="${project.build.directory}"/>
<compilePath default-value="${project.compileClasspathElements}"/>
<compileSourceRoots default-value="${project.compileSourceRoots}"/>
<compilerId default-value="javac">${maven.compiler.compilerId}</compilerId>
<compilerReuseStrategy default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}</compilerReuseStrategy>
<compilerVersion>${maven.compiler.compilerVersion}</compilerVersion>
<debug default-value="true">${maven.compiler.debug}</debug>
<debuglevel>${maven.compiler.debuglevel}</debuglevel>
<encoding default-value="${project.build.sourceEncoding}">${encoding}</encoding>
<executable>${maven.compiler.executable}</executable>
<failOnError default-value="true">${maven.compiler.failOnError}</failOnError>
<failOnWarning default-value="false">${maven.compiler.failOnWarning}</failOnWarning>
<forceJavacCompilerUse default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
<fork default-value="false">${maven.compiler.fork}</fork>
<generatedSourcesDirectory default-value="${project.build.directory}/generated-sources/annotations"/>
<maxmem>${maven.compiler.maxmem}</maxmem>
<meminitial>${maven.compiler.meminitial}</meminitial>
<mojoExecution default-value="${mojoExecution}"/>
<optimize default-value="false">${maven.compiler.optimize}</optimize>
<outputDirectory default-value="${project.build.outputDirectory}"/>
<parameters default-value="false">${maven.compiler.parameters}</parameters>
<project default-value="${project}"/>
<projectArtifact default-value="${project.artifact}"/>
<release>${maven.compiler.release}</release>
<session default-value="${session}"/>
<showDeprecation default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
<showWarnings default-value="false">${maven.compiler.showWarnings}</showWarnings>
<skipMain>${maven.main.skip}</skipMain>
<skipMultiThreadWarning default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
<source default-value="1.6">${maven.compiler.source}</source>
<staleMillis default-value="0">${lastModGranularityMs}</staleMillis>
<target default-value="1.6">${maven.compiler.target}</target>
<useIncrementalCompilation default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
<verbose default-value="false">${maven.compiler.verbose}</verbose>
</configuration>
these are all the configurations available for 3.8.1 version of compiler plugin. Different versions have different configurations which you can find by running your code with -X
after the general mvn command. Like
以上是3.8.1版编译器插件的所有配置。不同的版本有不同的配置,您可以通过-X
在通用 mvn 命令之后运行代码来找到这些配置。喜欢
mvn clean install -X
mvn compiler:compile -X
and search with id or goal or plugin name This may help with other plugins too. Eclipse, intelliJ may not show all configurations as suggestions.
并使用 id 或目标或插件名称进行搜索 这可能对其他插件也有帮助。Eclipse、intelliJ 可能不会将所有配置都显示为建议。
回答by Maksym Pecheniuk
This worked for with maven 3.5.4 and now Intellij Idea see this code as source:
这适用于 maven 3.5.4,现在 Intellij Idea 将此代码视为源代码:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
</configuration>
</plugin>
回答by radzimir
While the answer from evokk is basically correct, it is missing test classes. You must add test classes with goal add-test-source:
虽然 evokk 的答案基本上是正确的,但它缺少test classes。您必须使用目标add-test-source添加测试类:
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated/some-test-classes</source>
</sources>
</configuration>
</execution>