【发布时间】:2010-09-21 04:34:37
【问题描述】:
有没有办法在单个 maven 项目中编译多个 java 源目录?
【问题讨论】:
有没有办法在单个 maven 项目中编译多个 java 源目录?
【问题讨论】:
您可以使用 build-helper 添加新的源目录:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<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>
【讨论】:
plugin 元素在/project/build/plugins 中,而不在/project/build/pluginManagement/plugins 中
m2e connector for build-helper-maven-plugin 以消除 pom.xml 中的错误
'build.plugins.plugin.version' for org.codehaus.mojo:build-helper-maven-plugin is missing 的警告,您需要在<plugin> 中添加标签<version>1.12</version>
我天真地这样做了:
<build>
<finalName>osmwse</finalName>
<sourceDirectory>src/main/java, src/interfaces, src/services</sourceDirectory>
</build>
【讨论】:
这对我有用
<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>
【讨论】:
sourceDirectory - 可能还有额外的sources - 作为源文件的根。在您的解决方案中,maven-compiler-plugin 是唯一知道这些实际根的插件。
要使其在 intelliJ 中工作,您还可以通过这种方式将 generatedSourcesDirectory 添加到编译器插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
</configuration>
</plugin>
【讨论】:
<generatedSourcesDirectory>${project.build.directory}/generated-sources/main/java</generatedSourcesDirectory>
通过定义资源标签,这也适用于 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>
【讨论】:
Resources are not (usually) code. They are not compiled
这适用于 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>
【讨论】:
虽然来自 evokk 的答案基本上是正确的,但它缺少 测试类。 您必须添加目标为 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>
【讨论】:
使用帖子中的 build-helper-maven-plugin - 并更新 src/main/generated。 mvn clean compile 在我的 ../common/src/main/java 或 ../common 上工作,所以保留后者。然后是的,确认 IntelliJ IDEA (ver 10.5.2) 级别的编译失败,正如 David Phillips 提到的那样。 问题是 IDEA 没有向项目添加另一个源根目录。手动添加它解决了这个问题。这不好,因为编辑项目中的任何内容都应该来自 maven,而不是直接编辑 IDEA 的项目选项。然而,我将能够忍受它,直到他们直接支持 build-helper-maven-plugin 以便它会自动添加源代码。
然后需要另一种解决方法来完成这项工作。由于每次 IDEA 在 pom 更改后重新导入 maven 设置时,我新添加的源都保留在模块上,但它丢失了它的源文件夹选择并且没用。所以对于 IDEA - 需要设置一次:
现在让这些文件夹保持在导入状态也不是世界上最好的做法,......但试试看吧。
【讨论】:
这可以分两步完成:
${build.directory}
如果您使用已启动的 Jetty (jetty:run),则重新编译任何模块中的任何类(使用 Maven、IDEA 或 Eclipse)将导致 Jetty 重新启动。修改后的资源将获得相同的行为。
【讨论】:
在配置中,可以使用<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>
这些是 3.8.1 版本的编译器插件可用的所有配置。不同的版本有不同的配置,您可以通过在通用 mvn 命令后使用 -X 运行代码来找到这些配置。喜欢
mvn clean install -X
mvn compiler:compile -X
并使用 id 或目标或插件名称进行搜索 这也可能对其他插件有所帮助。 Eclipse、intelliJ 可能不会将所有配置都显示为建议。
【讨论】: