繁体   English   中英

具有Java和groovy源文件夹的Maven项目,如何在POM中表示

[英]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/javasrc/main/groovy分别地)。 否则,我会对src/test/groovy做同样的事情。

在包含src/main/groovy同时还包含src/main/java的正确方法是什么,以便在使用m2e导入时不需要手动将src/main/groovy添加为源文件夹?

我问,因为即使我在Eclipse的构建路径中的pom和其他项目中都具有依赖项,但除非我手动将jar作为外部依赖项添加,否则Eclipse从该项目中找不到类。

请遵循Groovy官方文档提供的建议。

编辑

官方建议在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/javasrc/main/groovy (以及测试文件夹)中混合使用groovy和Java文件,事情也会变得非常混乱,我强烈建议您不要使用Java / Groovy混合了一段时间的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM