execute JavaExec task using gradle kotlin dsl

跟風遠走 提交于 2019-12-11 12:45:05

问题


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:


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
}


来源:https://stackoverflow.com/questions/51810254/execute-javaexec-task-using-gradle-kotlin-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!