我试图了解 Maven 中 Java 和 Groovy 项目的正确设置,并使用 groovy-eclipse-compiler 编译源文件。
根据plugin site ,如果 src/main/java
和 src/test/java
中有文件,编译器默认会找到 Java 和 Groovy。
像示例一样设置build
会使target/classes
为空。
<build>
...
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<!-- set verbose to be true if you want lots of uninteresting messages -->
<!-- <verbose>true</verbose> -->
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
</build>
我发现让它工作的唯一方法是使用build-helper-maven-plugin
。这也是原型(prototype)使用的选项:
mvn archetype:generate \
-DarchetypeGroupId=org.codehaus.groovy \
-DarchetypeArtifactId=groovy-eclipse-quickstart \
-DarchetypeVersion=2.5.2-SNAPSHOT \
-DgroupId=foo \
-DartifactId=bar \
-Dversion=1 \
-DinteractiveMode=false \
-DarchetypeRepository=https://nexus.codehaus.org/content/repositories/snapshots/
那么,该页面已过时吗? src 文件夹中的 Java 文件还不够吗?
最佳答案
我能够让它发挥作用。这是我所做的:
- 按照上面的操作创建原型(prototype)。
- 将 pom.xml 编辑到以下文件
- mv 从 src/main/groovy 到 src/main/java 的所有内容,测试也是如此。
- 删除空的常规目录
- 将所有 *.java 文件重命名为 *.groovy
- mvn 干净编译
- 由于 Java 与 groovy 语法的差异,我收到了一些预期的编译错误,但所有 groovy 文件都能被识别。
这对你有用吗?如果不是有什么不同?
这是我正在使用的 pom 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1</version>
<name>bar Groovy Eclipse Maven Java App</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.1.1-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<小时/>
编辑
由于我误解了实际问题,并且您确实希望将文件分开,因此有 2 个选项:
- 像您一样使用 build-helper-plugin
- 使用 groovy-eclipse-compiler 魔力:
<plugin> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.7.0-01</version> <extensions>true</extensions> </plugin> </build>
这在 groovy-eclipse-compiler 上都有描述。页。
关于eclipse - Maven 的 Groovy Eclipse 编译器插件不编译任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16426102/