Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gradle. Java, and Groovy together?

I am trying to use a Gradle project in IntelliJ 13 but I keep running into issues such as:

  • Java files can't see Groovy files
  • IntelliJ seems to forget about Groovy and prompts me to configure a GDK for it

I read that the groovy plugin allows Groovy and Java in mixed own source path, but Java wants its own. So I have the following directory structure:

  • src\main\groovy
  • src\main\java
  • src\test\groovy

I have a mix of Java and Groovy classes

Here is my build.gradle:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'


buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
    }
}

jar {
    baseName = 'my-app'
    version = '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.RC4")
    compile("org.springframework:spring-orm:4.0.0.RC1")
    compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
    compile("com.h2database:h2:1.3.172")
    compile("joda-time:joda-time:2.3")
    compile("org.thymeleaf:thymeleaf-spring4")
    compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
    compile ('org.codehaus.groovy:groovy-all:2.2.1')

    testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
    testCompile("junit:junit")
}

jacocoTestReport {
  <!-- not sure this is right  -->
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

And here is a build error I get when I run "gradle clean build":

...src/main/java/com/product/service/FileDownloadService.java:24:  cannot find symbol 
symbol  : class FileDownload 
location: class com.product.service.FileDownloadService

private FileDownload fileDownload;

If I make everything Java, then I don't get any compile or execution errors.

like image 829
sonoerin Avatar asked Mar 13 '14 04:03

sonoerin


People also ask

Can you mix Java and Groovy?

Joint compilation is a process designed to compile both Java and Groovy files in the same project, in a single Maven command. With joint compilation, the Groovy compiler will: parse the source files. depending on the implementation, create stubs that are compatible with the Java compiler.

How do I run Groovy in Gradle?

From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 2: Groovy as implementation language. Next you can choose the DSL for writing buildscripts - 1 : Groovy or 2: Kotlin .

Does Gradle use Groovy?

Dependency management Because Gradle's build language is based on Groovy, and parts of Gradle are implemented in Groovy, Gradle already ships with a Groovy library. Nevertheless, Groovy projects need to explicitly declare a Groovy dependency. This dependency will then be used on compile and runtime class paths.

Can I use Java classes in Groovy?

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

tries append to file "build.gradle" the next lines

sourceSets {
      main {
        java { srcDirs = [] }    // no source dirs for the java compiler
        groovy { srcDir "src" }  // compile everything in src/ with groovy
       }
    }

excuse me for my bad english. I hope that this can help your solution.

like image 137
arturoeanton Avatar answered Oct 23 '22 05:10

arturoeanton


Like mentioned above, compiling with the groovy plugin will also compile the java classes. We just have to make sure that the java compile task is not fired on the source as well as the groovy task...

To to this, and retain the source folders (for e.g.: in eclipse), you can use the following refined snippet in the build.gradle:

apply plugin: 'groovy'
//...
sourceSets {
  main {
    java { srcDirs = [] }    // no source dirs for the java compiler
    groovy { srcDirs = ["src/main/java", "src/main/groovy"] }  // compile   everything in src/ with groovy
  }
}

If you just specify the groovy { srcDir "src" }, your main/groovy and main/java folders are identified as packages in eclipse...

like image 22
leroyse Avatar answered Oct 23 '22 03:10

leroyse


My solution would be to just have both Java and Groovy classes located in src/main/groovy and src/test/groovy (for test classes). From the compiler point of view it would result in something quite similar to the changes to the sourceSets suggested in the other answers.

From a user perspective I do not see a real benefit to keep the source files in separate folder hierarchies as it makes finding things harder. Also you are likely to migrate your classes between Java and Groovy to select the optimal implementation.

The main benefit is it works out of the box without any configuration in Gradle's build script. So it helps keeping things simple.

like image 2
Tobias Kremer Avatar answered Oct 23 '22 03:10

Tobias Kremer