java - Gradle-项目中多个Java程序的运行任务

标签 java gradle

需要:要为同一项目中的多个程序分别创建一个运行任务
基于此LINK中建议的解决方案。我尝试如下所示。
工作代码:

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
由于我不太熟悉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')
(并从文件中删除 Artifact 部分)

关于java - Gradle-项目中多个Java程序的运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62755823/

相关文章:

android - 在多项目中处理(仅)库中的资源(无依赖项)

java - Intershop studio 4.20.0 - 编译Java - ClassNotFoundException : com. sun.xml.bind.v2.ContextFactory

java - 安卓 : Displaying information in two columns instead of one for BaseAdapter

java - 检查二维数组中的相邻元素并替换它们

java - Entry语句的作用是什么?

android - 找到路径为 'META-INF/LGPL2.11' 的 2 个文件

bash - 如何在 shell 脚本中捕获 Gradle 退出代码?

java - 线程 "main"java.lang.NoClassDefFoundError : org/apache/commons/csv/CSVFormat 中出现异常

java - 字符串的数字操作

Android Studio - 使用 facebook sdk 和 jsoup 1.7.2 sources.jar 导入项目后构建失败