Ryan S
Ryan S

Reputation: 155

How can I create Gradle tasks correctly?

I'm trying to build tasks to allow me to specify my profile for my spring app using Gradle.

buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
    }

    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

plugins {
    id "com.diffplug.gradle.spotless" version "3.10.0"
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'

spotless {
    java {
        googleJavaFormat()
        licenseHeaderFile 'habicus.license.java'
    }
}

group = 'com.remindful'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-jersey')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-web-services')
    compile('org.springframework.boot:spring-boot-starter-websocket')
    compile('org.springframework.session:spring-session-core')
    compile("com.h2database:h2")
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile group: 'com.google.guava', name: 'guava', version: '11.0.2'
    testCompile 'junit:junit:4.12'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.1.RELEASE'
}


// TODO: Add prod profile in application-properties
task prod {
    run { systemProperty "spring.profiles.active", "prod" }
}

task dev {
    run { systemProperty "spring.profiles.active", "dev" }
}
// To force debug on application boot, switch suspend to y
bootRun {
    systemProperties System.properties
    jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"]
}

tasks.bootRun.dependsOn build
bootRun.mustRunAfter dev

I have two questions:

1) Intellij highlight the tasks with yellow squiggly underline, so I'm wondering if the syntax here is wrong?

2) Do I use gradle-wrapper for something like this or just gradle? I'm trying to better understand the difference.

Upvotes: 1

Views: 784

Answers (1)

std4453
std4453

Reputation: 858

1) Intellij highlight the tasks with yellow squiggly underline, so I'm wondering if the syntax here is wrong?

Unfortunately, IntelliJ does not support gradle completely, for example, the ext block in the build script is not recognized correctly, so when accessing members defined throught the ext block, IntelliJ fails to resolve its definition & type.

So there's no need for panic when IntelliJ displays a yellow underline, you only need to take notice of the errors reported by the gradle command. If gradle build says OK, then everything is fine.

Meanwhile, IntelliJ is unable to resolve third-party plugins statically, therefore it is also unable to recognize tasks and Task classes added by these plugins. In this case, it will also show this yellowish underline complaining about something like cannot infer argument type.

The solution is to refresh all Gradle projects, by clicking the refresh button on the gradle panel, and if your build script is written correctly, these underlines will most probably vanish. This is because IntelliJ embedds with gradle using the Gradle Tooling API, and during the gradle sync process (fired by refresh all Gradle projects), third-party plugins got resolved and the whole project object model is built, so that IntelliJ would know that nothing is wrong with your tasks.

If gradle build fails, then the problem is with your own build script. On how to write gradle build scripts & tasks correctly, see Authoring Tasks - Gradle User Manual.


2) Do I use gradle-wrapper for something like this or just gradle? I'm trying to better understand the difference.

The Gradle wrapper is explained in the official userguide. To be brief, I would quote:

The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary.

So if you don't have a wrapper script in your project, you should execute gradle wrapper to generate one, and then commit it to your VCS. After that, wherever you previously execute gradle <task> in the command line, you can/should replace it with ./gradlew <task> (in *nix environment) or gradlew.bat <task> (in Windows).

The main difference between using the gradle command directly from the command line and the gradle wrapper script is, that the gradle wrapper script will download the gradle binary and uses it to execute gradle builds if no installed gradle binary is found, while using the the gradle command will only result in an error in such case.

Also, when someone else is using your project, he/she can simply clone the repo and run ./gradlew build (in *nix) or gradlew.bat build (in Windows) and your project will build fluently and successfully, regardless of whether he/she has previously installed a gradle distribution.

Upvotes: 1

Related Questions