How to set up a vanilla groovy project with main class executable via jar?

djhaskin987 :

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:

  • how to set up a groovy project (EDIT: Including where to put source files, in what directories, directory structure, etc.)
  • assuming I haven't build groovy before
  • or java before (I have, but work with me)
  • in a way that I can specify a dependency (this is my dependency --> https://github.com/bpsm/edn-java) so probably using maven or gradle
  • one that has a main class and a main method (again, work with me)
  • at the end of the build, I need a jar file that I can execute via java -jar <groovy-project>.jar which prints out "hello world"

As simply as possible.

Will :

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.

1. Creating the project structure

  1. Download Gradle and put it in your $PATH

  2. Create your directory project (I created /tmp/gr8ex)

  3. Switch to it and run gradle init [1]

  4. 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):

  1. This is to create the source code dir:

    mkdir -p src/main/groovy
    
  2. And the test source folder:

    mkdir -p src/test/groovy
    
  3. 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
    

2. Adding source code

  1. Let's add a test package:

    mkdir -p src/test/groovy/org/gr8ex
    
  2. 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!"
        }
    }
    
  3. Let's execute the test and check it fails:

    gradle test
    
  4. 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
    
  5. Let's add source folder

    mkdir -p src/main/groovy/org/gr8ex
    
  6. 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
        }
    }
    
  7. 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.

3. Creating the jar executable

There is a couple of ways to achieve that (like the shadow plugin). I'm going to stick with a "fatjar" approach.

  1. 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
    }
    
  2. Packaging it:

    gradle fatjar
    
  3. The resulting jar will be in builds/libs/gr8ex.jar. Let's execute it:

    $ java -jar build/libs/gr8ex.jar 
    Hello, World!
    
  4. 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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=472495&siteId=1