Gradle-在一个项目中为多个java程序运行任务

Gradle-在一个项目中为多个java程序运行任务,java,gradle,Java,Gradle,需要:为同一项目中的多个程序分别创建一个运行任务 基于本文建议的解决方案。我尝试了如下所示 工作代码: task runCustom1(type: JavaExec) { group = 'Z_Custom_Run' description = 'Testing for Gradle Run' classpath sourceSets.main.runtimeClasspath main = "pkg01.TestGradleRun" }

需要:为同一项目中的多个程序分别创建一个运行任务

基于本文建议的解决方案。我尝试了如下所示

工作代码:

task runCustom1(type: JavaExec) {
    group = 'Z_Custom_Run'
    description = 'Testing for Gradle Run'

    classpath sourceSets.main.runtimeClasspath
    main = "pkg01.TestGradleRun"
}

task runCustom2(type: JavaExec) {
    group = 'Z_Custom_Run'
    description = 'Testing for Gradle Run'

    classpath sourceSets.main.runtimeClasspath
    main = "pkg01.TestGradleRun2"
}
def customRunTask(String className, String packagePath){
    return tasks.create("run${className}", JavaExec){
            group = 'zCustomRun'
            description = 'Testing for Gradle Run'

            classpath sourceSets.main.runtimeClasspath
            main = packagePath
    }
}

artifacts {
    archives customRunTask("Test1","pkg01.TestGradleRun"),
             customRunTask("Test2","pkg01.TestGradleRun2")
}
A problem occurred evaluating root project 'testJavaFeatures'.
> Cannot convert the provided notation to an object of type ConfigurablePublishArtifact: task ':runTest1'.
  The following types/formats are supported:
    - Instances of ConfigurablePublishArtifact.
    - Instances of PublishArtifact.
    - Instances of AbstractArchiveTask, for example jar.
    - Instances of Provider<RegularFile>.
    - Instances of Provider<Directory>.
    - Instances of Provider<File>.
    - Instances of RegularFile.
    - Instances of Directory.
    - Instances of File.
    - Maps with 'file' key
但是上面的方法很麻烦,因为我必须为许多程序生成代码&因此尝试了下面的方法,看看是否可以保持代码紧凑。但它给出了如下所示的错误

试用代码:

task runCustom1(type: JavaExec) {
    group = 'Z_Custom_Run'
    description = 'Testing for Gradle Run'

    classpath sourceSets.main.runtimeClasspath
    main = "pkg01.TestGradleRun"
}

task runCustom2(type: JavaExec) {
    group = 'Z_Custom_Run'
    description = 'Testing for Gradle Run'

    classpath sourceSets.main.runtimeClasspath
    main = "pkg01.TestGradleRun2"
}
def customRunTask(String className, String packagePath){
    return tasks.create("run${className}", JavaExec){
            group = 'zCustomRun'
            description = 'Testing for Gradle Run'

            classpath sourceSets.main.runtimeClasspath
            main = packagePath
    }
}

artifacts {
    archives customRunTask("Test1","pkg01.TestGradleRun"),
             customRunTask("Test2","pkg01.TestGradleRun2")
}
A problem occurred evaluating root project 'testJavaFeatures'.
> Cannot convert the provided notation to an object of type ConfigurablePublishArtifact: task ':runTest1'.
  The following types/formats are supported:
    - Instances of ConfigurablePublishArtifact.
    - Instances of PublishArtifact.
    - Instances of AbstractArchiveTask, for example jar.
    - Instances of Provider<RegularFile>.
    - Instances of Provider<Directory>.
    - Instances of Provider<File>.
    - Instances of RegularFile.
    - Instances of Directory.
    - Instances of File.
    - Maps with 'file' key
错误:

task runCustom1(type: JavaExec) {
    group = 'Z_Custom_Run'
    description = 'Testing for Gradle Run'

    classpath sourceSets.main.runtimeClasspath
    main = "pkg01.TestGradleRun"
}

task runCustom2(type: JavaExec) {
    group = 'Z_Custom_Run'
    description = 'Testing for Gradle Run'

    classpath sourceSets.main.runtimeClasspath
    main = "pkg01.TestGradleRun2"
}
def customRunTask(String className, String packagePath){
    return tasks.create("run${className}", JavaExec){
            group = 'zCustomRun'
            description = 'Testing for Gradle Run'

            classpath sourceSets.main.runtimeClasspath
            main = packagePath
    }
}

artifacts {
    archives customRunTask("Test1","pkg01.TestGradleRun"),
             customRunTask("Test2","pkg01.TestGradleRun2")
}
A problem occurred evaluating root project 'testJavaFeatures'.
> Cannot convert the provided notation to an object of type ConfigurablePublishArtifact: task ':runTest1'.
  The following types/formats are supported:
    - Instances of ConfigurablePublishArtifact.
    - Instances of PublishArtifact.
    - Instances of AbstractArchiveTask, for example jar.
    - Instances of Provider<RegularFile>.
    - Instances of Provider<Directory>.
    - Instances of Provider<File>.
    - Instances of RegularFile.
    - Instances of Directory.
    - Instances of File.
    - Maps with 'file' key
评估根项目“testJavaFeatures”时出现问题。 >无法将提供的表示法转换为ConfigurablePublishArtifact:task':runTest1'类型的对象。 支持以下类型/格式: -ConfigurablePublishArtifact的实例。 -PublishArtifact的实例。 -AbstractArchiveTask的实例,例如jar。 -提供程序的实例。 -提供程序的实例。 -提供程序的实例。 -RegularFile的实例。 -目录的实例。 -文件的实例。 -使用“文件”键映射
由于我对Gradle不太熟悉,请向专家咨询如何修复错误并使其正常工作

您就快到了。。。下面的方法应该有效

def customRunTask(String className, String packagePath){
    return tasks.create("run${className}", JavaExec){
        group = 'zCustomRun'
        description = 'run ${packagePath}.${className}'

        classpath sourceSets.main.runtimeClasspath
        main = packagePath + '.' + className
    }
}

customRunTask('ClassA', 'com.pkg1')
customRunTask('ClassB', 'com.pkg2')

(并从您的文件中删除“工件”部分)

不确定您试图做什么,但您可以非常轻松地生成类似的任务:

List mainClassNames = [ 'pkg01.TestGradleRun', 'pkg01.TestGradleRun2' ]

mainClassNames.each{ name ->

  task "runCustom-$name"(type: JavaExec) {
    group = 'Z_Custom_Run'
    description = "Testing for Gradle Run for $name"

    classpath sourceSets.main.runtimeClasspath
    main = name
  }
}

artifacts {
    archives mainClassNames.collect{ ":runCustom-$it" }
}

您不能将
JavaExec
类型的任务定义为
工件的
存档
。你的意思是使用类型为
Jar
的任务吗?@LukasKörfer我试图通过在tasks.create的花括号中指定Jar&包含整个工作代码(如qns所示)。它为一个程序生成,我不知道如何使它为多个程序工作。