Back Home

Getting Started

For windows

1) download and unzip Gradle

2) Add to path (advanced settings)

For existing projects:

3) Navigate to development project folder, at promp type gradle init

In IntelliJ

5) Edit build.gradle file to contain the basic template

apply plugin: 'java'
repositories {
    mavenCentral()
}
dependencies{
 
    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api
    // compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'
 
}

6) Add any relevant libraries in the dependencies section - format can be found here: https://mvnrepository.com/

Native Exec - command call

task nativeImage(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = project.mainClassName
    executable = 'native-image'
    jvmArgs '-H:+ReportUnsupportedElementsAtRuntime'
}

FatJar - ShadowJar

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
 
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
    }
}
apply plugin: 'com.github.johnrengelman.shadow'
 
 
 
 
 
// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    maven {url = "http://degtluv0836.global.umusic.ext:8081/artifactory/libs-release/" }
}
 
// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'io.vertx:vertx-core:3.3.2'
    compile 'io.vertx:vertx-web:3.3.2'
    compile 'io.vertx:vertx-web-templ-handlebars:3.3.0'
    compile 'org.slf4j:slf4j-api:1.7.21'
    compile 'org.slf4j:slf4j-log4j12:1.7.5'
    compile "org.codehaus.groovy:groovy-all:2.3.11"
    compile(group: 'com.umgi.est.technicalservices', name: 'enterprise-technicalservices-commons', version: '4.0.0')
    compile 'io.searchbox:jest:1.0.1'
    compile 'com.mashape.unirest:unirest-java:1.4.9'
    testCompile 'junit:junit:4.12'
}
 
shadowJar {
    manifest {
        attributes 'Implementation-Title': 'Xpath checker',
                'Implementation-Version': '1.0',
                'Main-Class': 'Main'
    }
}

Note this needs a definition to work as well as the manifest mainClassName = 'Main'

Get Interactive Input

import org.gradle.api.internal.tasks.userinput.UserInputHandler
def projectName
def handler = services.get(UserInputHandler)
 
task ask {
   projectName = handler.askQuestion("prjoject name?", "")
}
 
task print () {
    println "Creating project ${projectName}"
}

Executing Commands

There are number of functions and bash extensions, the default Exec Tasks allows you to execute 1 command The following script will not work as expected:

  • the second “commandLine” will overwrite the first and the first one will not execute
task createProject (type: Exec) {
    try {
        dependsOn ask
        println "Creating project ${projectName} and profile ${profileName}"
        commandLine "bash","-c /usr/local/bin/oc login https://console.si-c2-ocp.swift.com:8443 --token=fv6-PixKWgTMFqprGoe8Z774XshOe7tmZRt-fBvDjbo"
        executable "bash","-c /usr/local/bin/oc new-project ${projectName} --display-name ${projectName}"
 
        ignoreExitValue true
        doLast {
            if (execResult.getExitValue() == 0) {
                println "oc new-project success"
            } else {
                println "oc new-project failed "
            }
        }
    } catch(Exception e ){
        e.printSackTrace()
    }

Executing multiple commands in one task:

 exec{
        workingDir '/usr/local/bin/'
        commandLine "oc","status"
    }
    exec {
        workingDir '/usr/local/bin/'
        commandLine  "oc", "login", "https://console.si-c2-ocp.com:8443", "--token=fv6-PixKWgTMFqprGoe8Z77e7tmZRt-fBvDjbo"
    }

Note: Gradle will complain if there is not valid return value. So the last commandLine should not be wrapped in an exec

Working Offline

To use gradle plugins from local repos:

pluginManagement {
    repositories {
        mavenLocal()
    }
}

and now it will only try to resolve plugins from ~/.m2/repository/

Gradle Plugins

 
gradle.txt · Last modified: 2020/01/24 01:30 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd