Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute JavaExec task using gradle kotlin dsl

I've created simple build.gradle.kts file

group  = "com.lapots.breed"
version = "1.0-SNAPSHOT"

plugins { java }

java { sourceCompatibility = JavaVersion.VERSION_1_8 }

repositories { mavenCentral() }

dependencies {}

task<JavaExec>("execute") {
    main = "com.lapots.breed.Application"
    classpath = java.sourceSets["main"].runtimeClasspath
}

In src/main/java/com.lapots.breed I created Application class with main method

package com.lapots.breed;

public class Application {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

But when I try to execute execute tasks it fails with the error that task doesn't exist. Also when I list all the available tasks using gradlew tasks it doesn't show execute task at all.

What is the problem?

like image 954
lapots Avatar asked Aug 12 '18 15:08

lapots


1 Answers

The following build script should work (Gradle 4.10.2, Kotlin DSL 1.0-rc-6):

group = "com.lapots.breed"
version = "1.0-SNAPSHOT"

plugins {
    java
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
}

repositories {
    mavenCentral()
}

task("execute", JavaExec::class) {
    main = "com.lapots.breed.Application"
    classpath = sourceSets["main"].runtimeClasspath
}

According the not-listed task - from certain version, Gradle doesn't show custom tasks which don't have assigned AbstractTask.group. You can either list them via gradle tasks --all, or set the group property on the given task(s), e.g.:

task("execute", JavaExec::class) {
    group = "myCustomTasks"
    main = "com.lapots.breed.Application"
    classpath = sourceSets["main"].runtimeClasspath
}
like image 119
Vít Kotačka Avatar answered Sep 28 '22 09:09

Vít Kotačka