使用gradle kotlin dsl执行JavaExec任务 [英] execute JavaExec task using gradle kotlin dsl

查看:160
本文介绍了使用gradle kotlin dsl执行JavaExec任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了简单的build.gradle.kts文件

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
}

src/main/java/com.lapots.breed中,我使用主方法创建了Application

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!");
    }
}

但是当我尝试执行execute任务时,它会失败,并显示错误的提示:该任务不存在.同样,当我使用gradlew tasks列出所有可用任务时,它根本不显示execute任务.

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.

出什么问题了?

推荐答案

以下构建脚本应该起作用(Gradle 4.10.2,Kotlin DSL 1.0-rc-6):

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
}

根据未列出的任务-从某些版本开始,Gradle不会显示未分配

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
}

这篇关于使用gradle kotlin dsl执行JavaExec任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆