我正面临以下难题,为此我花了大量时间试图解决,但迄今未成功。我有一个自定义的Gradle插件,其作用是启动一个进程并在后台运行它。
我的代码插件代码如下:
public class TaskSpawnerPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('spawnTask', type: TaskSpawner)
}
}
这是有问题的任务
public class TaskSpawner extends DefaultTask {
@Input
String command
@Input
String ready
@Input
String workDir = '.'
TaskSpawner() {
description = 'Given a Unix like cmd, this will start it and let it run on the background'
}
@TaskAction
public void spawn() {
getLogger().quiet "Attempting to run provided command $command"
if (!(command && ready)) {
throw new GradleException("Please make sure that both the command and ready check are provided!")
}
waitFor(createProcess(workDir, command))
}
private def waitFor(Process process) {
new BufferedReader(new InputStreamReader(process.getInputStream())).withCloseable {
reader ->
def line
while ((line = reader.readLine()) != null) {
getLogger().quiet line
if (line.contains(ready)) {
getLogger().quiet "$command is ready"
break
}
}
}
}
private def static createProcess(String directory, String command) {
new ProcessBuilder(command.split(' '))
.redirectErrorStream(true)
.directory(Paths.get(directory).toFile())
.redirectError(ProcessBuilder.Redirect.INHERIT)
.redirectInput(ProcessBuilder.Redirect.INHERIT)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.start()
}
}
代码位于具有以下程序包名称的程序包结构中
fts.gradle
我的构建脚本如下:
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'maven-publish'
}
group = 'fts.gradle'
version = '0.3'
repositories {
jcenter()
}
dependencies {
compile gradleApi()
compile localGroovy()
}
gradlePlugin {
plugins {
greetingsPlugin {
id = 'fts.gradle.taskspawn'
implementationClass = 'fts.gradle.TaskSpawnerPlugin'
}
}
}
我通常会构建我的插件,然后将其部署在本地托管的工件上。我的问题围绕着如何导入它并在项目中使用它。
我暂时要执行以下操作:
buildscript {
repositories {
maven { url "<maven_url>" }
}
dependencies {
classpath group: 'fts.gradle', name: 'task-spawner', version: '0.3'
}
}
plugins {
id 'java'
id 'application'
id 'eclipse'
}
apply plugin: 'fts.gradle'
然后我尝试使用以下方法应用它:
但是,当尝试刷新项目时,此操作将失败:
* What went wrong:
A problem occurred evaluating project ':integration-tests'.
> Could not get unknown property 'TaskSpawner' for project ':integration-tests' of type org.gradle.api.Project.
我已经阅读了文档,并且尝试了各种方法来创建和导入作为独立jar的插件,但是到目前为止,我还没有成功。
有人可以在这里阐明一下吗?在过去的几天里,这一直是疯子。
注意,我使用的Gradle版本为5.6.2,以供参考