【问题标题】:Correct way to set up a Maven POM to compile mixed Java/Groovy code in Intellij IDEA在 Intellij IDEA 中设置 Maven POM 以编译混合 Java/Groovy 代码的正确方法
【发布时间】:2018-01-04 19:21:49
【问题描述】:

我在 Intellij IDEA 中有一个非常简单的测试项目,我在其中尝试混合 Java 9 和 Groovy 代码。此外,这个项目有Maven Support,即它是根据 Maven 原型组织的,并且有一个 POM。

现在我在默认包中有两个类互相调用(虽然不是循环的,因为编译失败)并且一切正常,即 Build > Build ProjectRun > Run 'Main' 正在工作。

但是,这种编译似乎独立于 Maven 配置。 POM 中没有 Groovy 支持。如果我只是从命令行使用mvn compile 编译,编译会失败,因为链接器找不到任何由 Groovy 编译产生的对象。

POM 只包含 maven-compiler-plugin 的配置,我手动添加了 sourcetarget 节点下的 configuration 设置为 1.9 让 IDEA 从/编译到 Java 9 而不是从/到 Java 5。所以当我选择 Rebuild Project 时,POM 中的内容与 IDEA 所做的事情之间存在一些交互。

配置 IDEA 和/或配置 POM 的正确方法是什么,以便在 IDEA 和命令行中都能成功编译。如果有人知道,POM 和 IDEA 之间的交互是什么?

我必须在 POM 中配置Groovy Eclipse Maven plugin 吗? (我会努力做到的)

【问题讨论】:

  • IDEA 使用 pom.xml 作为项目配置的初始源。因此,IDEA 构建和 Maven 构建应该以相同的方式工作。 IDEA 可能会检测到 groovy 文件并建议将 Groovy SDK 添加到类路径中,但是此更改(与项目配置中的任何其他手动更改一样)将在 Maven 工具窗口的“重新导入”时删除。因此,您可以尝试运行“重新导入”并检查项目依赖项中是否仍然提到了 groovy。如果您需要使用 maven 构建 grovy,则应将 groovy 库的依赖项添加到您的 pom.xml 中。

标签: java maven intellij-idea groovy


【解决方案1】:

这就是我为 Java 8 和 Eclipse 所做的工作,对于一个同时包含 Java 和 Groovy 代码的项目。我知道我曾经通过谷歌找到了这个基础,但没有保存 URL,唉。不确定它是否适用于 Java 9 和 IntelliJ,但值得一试吗?

<properties>
  <groovy.eclipse.compiler.plugin.version>2.9.1-01</groovy.eclipse.compiler.plugin.version>
</properties>
....
<plugin>
  <artifactId>maven-compiler-plugin</artifactId>

  <!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher -->
  <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>${groovy.eclipse.compiler.plugin.version}</version>
    </dependency>
    <!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch -->
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-eclipse-batch</artifactId>
      <version>2.3.7-01</version>
    </dependency>
  </dependencies>
</plugin>
<plugin>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-eclipse-compiler</artifactId>
  <version>${groovy.eclipse.compiler.plugin.version}</version>
  <extensions>true</extensions>  <!-- required to get plugin to compile tests when no src/main/java dir exists -->
</plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2018-03-07
    相关资源
    最近更新 更多