I am getting very frustrated. I can't find a single, cohesive answer for my problem anywhere on the internet. Lots of documentation, nothing that brings it all together, though (that I can find).
All I need is someone to tell me:
java -jar <groovy-project>.jar which prints out "hello world"As simply as possible.
I strongly recommend Gradle; it is very simple to setup (though I too struggled when learning it). I added the resulting project in my github.
Let's create a project structure from zero without an IDE. I presume you already have $JAVA_HOME set up.
Download Gradle and put it in your $PATH
Create your directory project (I created /tmp/gr8ex)
Switch to it and run gradle init [1]
Edit the build.gradle created file and add these line:
plugins { // [2]
    id 'groovy'
}
repositories { mavenCentral() } // [3]
dependencies { // [4]
    testCompile 'org.codehaus.groovy:groovy-all:2.4.8'
    compile 'org.codehaus.groovy:groovy-all:2.4.8'
    testCompile 'junit:junit:4.12'
}
Now the source files; we need to create the default directory structure that gradle uses (we can change it, but let's go with the defaults):
This is to create the source code dir:
mkdir -p src/main/groovy
And the test source folder:
mkdir -p src/test/groovy
The end result should look like this:
gr8ex
    ├── build.gradle
    ├── gradle
    │   └── wrapper
    │       ├── gradle-wrapper.jar
    │       └── gradle-wrapper.properties
    ├── gradlew
    ├── gradlew.bat
    ├── settings.gradle
    └── src
        ├── main
        │   └── groovy
        └── test
            └── groovy
Let's add a test package:
mkdir -p src/test/groovy/org/gr8ex
And a test. I'm using gedit src/test/groovy/org/gr8ex/HelloTest.groovy:
package org.gr8ex
class HelloTest extends GroovyTestCase {
    void 'test Hello should return "Hello, World!"' () {
        assert new Hello().world == "Hello, World!"
    }
}
Let's execute the test and check it fails:
gradle test
Yep, it failed:
/tmp/gr8ex/src/test/groovy/org/gr8ex/HelloTest.groovy: 5: unable to resolve class Hello 
 @ line 5, column 12.
          assert new Hello().world == "Hello, World!"
          ^
1 error
:compileTestGroovy FAILED
Let's add source folder
mkdir -p src/main/groovy/org/gr8ex
And a source file (I used gedit src/main/groovy/org/gr8ex/Hello.groovy). Note it already have our static main method:
package org.gr8ex
class Hello {
    def getWorld() {
        "Hello, World!"
    }
    static main(args) {
        println new Hello().world
    }
}
Test again (with gradle test) and assert we get the message BUILD SUCCESSFUL:
$ gradle test
:compileJava UP-TO-DATE
:compileGroovy
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
BUILD SUCCESSFUL
Total time: 5.52 secs
Done. Time to create our application jar.
jar executableThere is a couple of ways to achieve that (like the shadow plugin). I'm going to stick with a "fatjar" approach.
Let's add a fatjar instruction in our build.gradle [5]:
task fatjar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'org.gr8ex.Hello'
    }
    from { 
        configurations
            .runtime
            .collect { it.isDirectory() ? it : zipTree(it) }
    }
    with jar
}
Packaging it:
gradle fatjar
The resulting jar will be in builds/libs/gr8ex.jar. Let's execute it:
$ java -jar build/libs/gr8ex.jar 
Hello, World!
Profit! You can import this project with intellij and (I believe) eclipse.
[1]: Gradle creates some basic structure and add wrapper scripts so it can be executed without Gradle, if needed.
[2]: Here we are telling gradle that this project will use groovy
[3]: We tell gradle to use the mavencentral repository. JCenter is also very popular.
[4]: Here we are telling gradle that this project needs to use the groovy-all lib upon compilation and and testing phases
[5]: If you just stick with a jar {} instruction, like this answer, you will end with a very thin jar which will be missing the groovy libs. This "fatjar" packs your libs into the jar. You might want to tweak it a bit depending on your use case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With