[英]Maven project with java and groovy source folders, how to denote in POM
我在其中之一中找到了要更改Groovy的測試目錄的地方。
<build>
<testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>
...
</build>
的問題是,有Java和在Groovy類src/main/
( src/main/java
和src/main/groovy
分別地)。 否則,我會對src/test/groovy
做同樣的事情。
在包含src/main/groovy
同時還包含src/main/java
的正確方法是什么,以便在使用m2e導入時不需要手動將src/main/groovy
添加為源文件夾?
我問,因為即使我在Eclipse的構建路徑中的pom和其他項目中都具有依賴項,但除非我手動將jar作為外部依賴項添加,否則Eclipse從該項目中找不到類。
編輯
官方建議在2015年有所更改:他們現在建議使用Ant Run插件
將以下內容添加到pom中(在“插件”部分中), testSourceDirectory
從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
執行。
老答案
選擇4個選項之一來解決此問題,然后從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/java
和src/main/groovy
(以及測試文件夾)中混合使用groovy和Java文件,事情也會變得非常混亂,我強烈建議您不要使用Java / Groovy混合了一段時間的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.