Raghuvir Parihar
Raghuvir Parihar

Reputation: 1

Integrating groovy in java in gradle

Im trying to integrate groovy in java with build tools as gradle, There are several examples with maven build, but with gradle its still missing.

I tried of adding plugin of groovy, but for compiler plugin like gmavenplus. I try to add it as dependency but not sure how i can add it in gradle

Upvotes: 0

Views: 194

Answers (1)

Terry Ebdon
Terry Ebdon

Reputation: 527

You can mix Java classes into a Groovy project.

  1. Create a Groovy project using gradle init.
  2. Your Groovy source will be in a source/main/groovy folder.
  3. Add a source/main/java folder (but see the comment, below, from Jeff Scott Brown)
  4. Add your java files in the java folder, in the usual way.
  5. Do the same for your src/test folder

Example src folder structure

src
└───main
    ├───resources
    ├───groovy
    │   └───mixedbuildtest
    │           App.groovy
    │
    ├───java
    │   └───mixedbuildtest
    │           JavaApp.java

Example source code

Groovy Source Code

package mixedbuildtest

class App {
    String getGreeting() {
        return 'Hello from Groovy!'
    }

    static void main(String[] args) {
        println new App().greeting
        println new JavaApp().greeting
    }
}

Java Source Code

package mixedbuildtest;

class JavaApp {
    String getGreeting() {
        return "Hello from Java!";
    }
}

Example output

F:\projects\sx\MixedBuild>gradle run

> Task :app:run
Hello from Groovy!
Hello from Java!

BUILD SUCCESSFUL in 4s
3 actionable tasks: 3 executed
F:\projects\sx\MixedBuild>

Upvotes: 0

Related Questions