用Java 11编译Java和groovy

时间:2018-12-17 14:20:51

标签: java groovy

有人知道哪个编译器/插件使用Java 11版本编译Java和groovy代码吗?

我尝试了GMavenPlus,Groovy eclipse编译器和Maven的Ant插件。到目前为止没有成功。

3 个答案:

答案 0 :(得分:3)

GitHub中有一个sample gradle project,可编译Java和Groovy,并使用Groovy的Java,反之亦然。稍作更改,就可以使其在Java 11中运行。基本上使用以下build.gradle

group 'de.jonashavers'
version '1.0.0'

apply plugin: 'groovy'

sourceSets {
    main {
        java { srcDirs = [] }
        groovy { srcDirs << ['src/main/java'] }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.5.7'
    compile 'javax.xml.bind:jaxb-api:2.3.0'
    compile 'com.sun.xml.bind:jaxb-core:2.3.0.1'
    compile 'com.sun.xml.bind:jaxb-impl:2.3.0.1'
    compile 'javax.activation:activation:1.1.1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

这必须是文件结构:

enter image description here

Groovy类:

package de.jonashavers.groovierjavacompilation

class PlainGroovy {
    String language = 'groovy'

    static void main(String[] args) {
        String lang = "Groovy"
        println(lang)
    }
}

Java类:

package de.jonashavers.groovierjavacompilation;

class JavaExtendingGroovy extends PlainGroovy {
}

测试:

Java:

package de.jonashavers.groovierjavacompilation;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JavaTest {

    @Test
    public void testAccessToGroovy() {
        PlainGroovy plainGroovy = new PlainGroovy();
        assertEquals("groovy", plainGroovy.getLanguage());
    }

    @Test
    public void testAccessToGroovyFromJava() {
        JavaExtendingGroovy groovyFromJava = new JavaExtendingGroovy();
        assertEquals("groovy", groovyFromJava.getLanguage());
    }
}

时髦:

package de.jonashavers.groovierjavacompilation

class GroovyTest extends GroovyTestCase {

    void testAccessToGroovy() {
        PlainGroovy plainGroovy = new PlainGroovy()
        assertEquals 'groovy', plainGroovy.language
    }

    void testAccessToGroovyFromJava() {
        JavaExtendingGroovy groovyFromJava = new JavaExtendingGroovy()
        assertEquals 'groovy', groovyFromJava.language
    }
}

我分叉了项目,并按照this link中的说明进行了工作。这是the fork that works in Java11,它是 java11分支。希望这会有所帮助:

答案 1 :(得分:2)

最后成功了:

  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
           <source>11</source>
           <target>11</target>
           <release>11</release>
        </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.6.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compileTests</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

groovy.version:2.4.12

答案 2 :(得分:0)

这是groovy-eclipse-compiler适配器的典型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>

  <artifactId>proj-name</artifactId>
  <groupId>org.whatever</groupId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.fork>true</maven.compiler.fork>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding>
  </properties>

  <pluginRepositories>
    <pluginRepository>
      <id>bintray</id>
      <name>Groovy Bintray</name>
      <url>https://dl.bintray.com/groovy/maven</url>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy</artifactId>
      <version>2.5.5</version>
      <classifier>indy</classifier>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <compilerId>groovy-eclipse-compiler</compilerId>
          <compilerArguments>
            <indy/>
          </compilerArguments>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>3.0.0-01</version>
          </dependency>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>2.5.5-01</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

</project>

假定您具有src / main / groovy,src / main / java,src / test / groovy和src / test / java。可能具有其他源文件夹配置以及一些其他XML。此处有更多详细信息:https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin