我有一个Gradle插件,可以启动Java进程。代码存储在项目的buildSrc目录下的名为startAppServerPlugin.gradle
的文件中。
插件代码如下:
repositories.jcenter()
dependencies {
localGroovy()
gradleApi()
}
}
public class StartAppServer implements Plugin<Project> {
@Override
void apply(Project project) {
project.task('startServer', type: StartServerTask)
}
}
public class StartServerTask extends DefaultTask {
String command
String ready
String directory = '.'
StartServerTask(){
description = "Spawn a new server process in the background."
}
@TaskAction
void spawn(){
if(!(command && ready)) {
throw new GradleException("Ensure that mandatory fields command and ready are set.")
}
Process process = buildProcess(directory, command)
waitFor(process)
}
private waitFor(Process process) {
def line
def reader = new BufferedReader(new InputStreamReader(process.getInputStream()))
while ((line = reader.readLine()) != null) {
logger.quiet line
if (line.contains(ready)) {
logger.quiet "$command is ready."
break
}
}
}
private static Process buildProcess(String directory, String command) {
def builder = new ProcessBuilder(command.split(' '))
builder.redirectErrorStream(true)
builder.directory(new File(directory))
def process = builder.start()
process
}
}
我正在尝试找到一种将此内容导入我的主要
build.gradle
文件的方法,因为我迄今为止尝试过的所有方法都没有成功。到目前为止,我已经尝试过这个:
apply from: 'startAppServerPlugin.gradle'
apply plugin: 'fts.gradle.plugins'
但它一直失败。我尝试在网上搜索例子来做我需要做的事情,但到目前为止都没有成功。请问有人能给个提示,我应该如何做呢?