需要:要为同一项目中的多个程序分别创建一个运行任务
基于此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/