Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiling Java and groovy with java 11

Tags:

java

groovy

Does anyone know which compiler/plugin compiles java and groovy code with the java 11 version?

I've tried GMavenPlus, Groovy eclipse compiler, and Maven’s Ant plugin. So far without success.

like image 527
Petronella Avatar asked Dec 17 '18 14:12

Petronella


People also ask

Does Java 11 use Groovy?

It provides the syntax equivalent of Java 10's var reserved type (but you can use it with Groovy 3 from JDK 8). It can be used for fields, local variables and parameters. It can also be used for lambda parameters (a Java 11 feature).

What version of Java does Groovy support?

Available from Codehaus, Groovy 2.3 is the first version of Groovy that is officially compatible with Java Development Kit 8, which Oracle released in March. However, Java 8 lambdas are not yet supported; the roadmap calls for supporting that in Groovy 3.0, which is due at the end of the year.

How do I integrate Groovy with Java?

parse the source files. depending on the implementation, create stubs that are compatible with the Java compiler. invoke the Java compiler to compile the stubs along with Java sources – this way Java classes can find Groovy dependencies. compile the Groovy sources – now our Groovy sources can find their Java ...

Can we use Groovy in Java?

Groovy scripts can use any Java classes. They can be compiled to Java bytecode (in . class files) that can be invoked from normal Java classes. The Groovy compiler, groovyc, compiles both Groovy scripts and Java source files, however some Java syntax (such as nested classes) is not supported yet.


3 Answers

In the end this worked:

<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

like image 119
Petronella Avatar answered Oct 03 '22 23:10

Petronella


Here is a typical POM for the groovy-eclipse-compiler adapter:

<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>

It assumes you have src/main/groovy, src/main/java, src/test/groovy and src/test/java. It is possible to have other source folder configurations with some additional XML. More details here: https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin

like image 20
emilles Avatar answered Oct 02 '22 23:10

emilles


There is a sample gradle project in GitHub that compiles Java and Groovy and uses Java from Groovy and viceversa. With a little changes you can make it work in Java 11. Basically use this 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'
}

This must be the file structure:

enter image description here

The Groovy class:

package de.jonashavers.groovierjavacompilation

class PlainGroovy {
    String language = 'groovy'

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

The Java Class:

package de.jonashavers.groovierjavacompilation;

class JavaExtendingGroovy extends PlainGroovy {
}

And the tests:

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());
    }
}

Groovy:

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
    }
}

I forked the project and followed the directions in this link to make it work. Here is the fork that works in Java11, the java11 branch. Hope this helps:

like image 30
Victor M Herasme Perez Avatar answered Oct 02 '22 23:10

Victor M Herasme Perez