9

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?

1
  • Tasks without a group set don't show up in ./gradlew tasks, you can use ./gradlew tasks --all to list them all. Commented Aug 16, 2018 at 10:28

1 Answer 1

18

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

With Gradle 8.12 I had to use mainClass= instead of main=.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.