Search This Blog

Set Groovy Development Environment

I. Install required SDK and IDE

  1. Install Java SDK 8+
  2. Install Gradle
  3. Install Groovy
  4. Install Intellij Idea



II. Create a new Groovy project using Gradle

  1. Create a new project directory:
    mkdir my-groovy-app
  2. Get into the project directory
    cd my-groovy-app
    and run gradle:
    gradle init
    Select type of project to generate:
      1: basic
      2: application
      3: library
      4: Gradle plugin
    Enter selection (default: basic) [1..4] 1
    
    Select build script DSL:
      1: Groovy
      2: Kotlin
    Enter selection (default: Groovy) [1..2] 1
    
    Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
    Project name (default: my-groovy-app):
    
    > Task :init
    Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.6/samples
    
    BUILD SUCCESSFUL in 22s
    2 actionable tasks: 2 executed
        
  3. Edit build.gradle file and add the content (like below):
    plugins {
        id 'groovy'
        id 'application'
    }
    
    group 'myapps'
    version '1.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
        maven {
            url 'https://my.org/mvn-repo'
        }
    }
    
    dependencies {
        implementation 'org.apache.groovy:groovy:4.0.7'
        implementation 'org.apache.ivy:ivy:2.5.0'
    
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    }
    
    application {
    	mainClass = 'my.App'
    }
    
    test {
        useJUnitPlatform()
    }
        
  4. Create source folder structure:
    mkdir src/main/groovy
    mkdir src/main/java
    mkdir src/main/resources
    mkdir src/test/groovy
    mkdir src/test/java
    mkdir src/test/resources
        
  5. Optional: create gradle.properties file at the project root, and add the options below:
    org.gradle.warning.mode=all
    org.gradle.logging.stacktrace=full
  6. Create Application class: my.App.groovy:
    package my
    
    class App {
        static void main(String[] args) {
        	println 'Hello, Wolrd'
        }
    }
      
  7. Start Intellij Idea, open the project directory. Select src/main/groovy directory, Mark directory as -> Sources root

No comments:

Post a Comment