【发布时间】:2019-01-09 11:48:14
【问题描述】:
我在 src/main/groovy 中有一个带有 groovy 文件的项目,这些文件在 src/main/java 中使用,并且我收到 groovy 文件引用错误。我无法在类路径中看到我的 groovy 文件。 我的 pom.xml 有设置 groovy 源文件夹的代码
<build>
<resources>
<resource>
<targetPath>.</targetPath>
<directory>src/main/groovy</directory>
</resource>
<resource>
<targetPath>.</targetPath>
<directory>src/main/resources</directory>
</resource>
<resource>
<targetPath>.</targetPath>
<directory>src/main/config</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.9.1-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.4.3-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.9.1-01</version>
<!-- <extensions>true</extensions> -->
</plugin>
<!-- Plugin Management -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
</additionalProjectnatures>
<!-- Source includes is necessary to allow groovy files in the java
folder, else eclipse will throw a filtering exception -->
<sourceIncludes>
<sourceInclude>**/*.groovy</sourceInclude>
</sourceIncludes>
<!-- Download sources will make maven download and attach source files
where available -->
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<compilerVersion>${java-version}</compilerVersion>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</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>
</plugins>
请建议进行任何更改以将 groovy 文件源文件夹放入类路径
【问题讨论】:
-
你有 groovy 和 java 类的混合,还是只有 groovy?
-
如果你有循环依赖,那么简单的方法通常是将所有的 java 文件移动到 groovy 源根目录,因为 groovy 编译器只是将 .java 文件委托给常规 java 编译器。