gpt4 book ai didi

java - 带有 java 和 groovy 源文件夹的 Maven 项目,如何在 POM 中表示

转载 作者:行者123 更新时间:2023-11-29 03:06:26 25 4
gpt4 key购买 nike

我在我的一个 poms 中发现了这个来更改 Groovy 的测试目录。

<build>
<testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>
...
</build>

问题是 src/main/ 中同时存在 Java 和 Groovy 类(src/main/javasrc/main/groovy分别)。否则我会对 src/test/groovy 做同样的事情。

在包含 src/main/groovy 的同时还包含 src/main/java 的正确方法是什么,这样当我用 m2e 导入它时我不需要手动添加 src/main/groovy 作为源文件夹?

我问是因为即使我在 pom 和 Eclipse 的构建路径中有其他项目的依赖项,Eclipse 也无法从该项目中找到类,除非我手动将 jar 添加为外部依赖项。

最佳答案

按照官方给出的建议Groovy documentation .

编辑

官方推荐在 2015 年的某个时候发生了变化:他们现在推荐使用 Ant Run 插件

将以下内容添加到您的 pom(在插件部分)并从您的 pom 中删除 testSourceDirectory

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<configuration>
<tasks>
<mkdir dir="${basedir}/src/test/groovy"/>
<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc">
<classpath refid="maven.test.classpath"/>
</taskdef>
<mkdir dir="${project.build.testOutputDirectory}"/>
<groovyc destdir="${project.build.testOutputDirectory}"
srcdir="${basedir}/src/test/groovy/" listfiles="true">
<classpath refid="maven.test.classpath"/>
</groovyc>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

同时为 src/main/groovy 目录和 Groovy 文件可能所在的任何其他位置添加一个 compile 执行。


旧答案

选择one of their 4 options要解决这个问题,请从您的 pom 中删除此 testSourceDirectory

在您的情况下,我会使用最后一个最冗长的选项,因为它保持标准 Maven 生命周期的完整性,并使其真正明确地说明正在发生的事情...

因此,在删除之前的配置后,将其添加到 pom 中:

<build>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
...

请注意,即使您甚至可以在 src/main/javasrc/main/groovy(以及测试文件夹)中混合使用 groovy 和 java 文件,事情会变得非常困惑,我强烈建议不要使用 Java/Groovy 混合代码一段时间。

关于java - 带有 java 和 groovy 源文件夹的 Maven 项目,如何在 POM 中表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32014078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com