打开Hybris安装文件夹下的recipes,随便打开一个recipe的build.gradle文件,发现使用了installer-platform-plugin和installer-addon-plugin这两个plugin. Groovy setup任务的逻辑,也就是使用这两个plugin里的实现,根据config实例化platform对象,然后调用其setup方法和executeAntTarget方法。
这两个方法的实现源代码在哪里?这就是本文所要描述的内容。
去docs文件夹里可以找到插件的实现源代码:
到源代码里根据executeAntTarget进行搜索:
在AbstractPlatform.groovy里找到了executeAntTarget的实现逻辑,发现其delegate到了成员属性antExecutor里:
这个antExecutor的类型是HybrisAntExecutor:
找到HybrisAntExcutor,发现其只不过是调用类HybrisPluginUtils的静态方法:
静态方法之一:runProcess
实现核心:
private static void runExternalProcess(String workDir, String[] command, Closure closure) {
def builder = new DefaultExecHandleBuilder()
builder.setWorkingDir((new File(workDir)).absolutePath)
builder.setCommandLine(command)
if (closure) closure.call(builder)
def handle = builder.build()
handle.start()
def result = handle.waitForFinish()
if (result.getExitValue() != 0) {
throw new IllegalStateException("external process returned non-zero exit code, command: ${command}")
}
}
运行外部程序的方法,使用的是Groovy SDK提供的import org.gradle.process.internal.DefaultExecHandleBuilder.
静态方法之二:isWindowsOs
import org.apache.tools.ant.taskdefs.condition.Os
HybrisAntExcutor方法之一:getWindowsAntCmd
private String[] getWindowsAntCmd(String antArgs, String antOpts) {
def antPreCommand = "set "ANT_OPTS=${antOpts}""
antPreCommand += " & set "PLATFORM_HOME=${platformHome}""
antPreCommand += " & set "ANT_HOME=${platformHome}\apache-ant-1.9.1""
antPreCommand += " & set "PATH=${platformHome}\apache-ant-1.9.1\bin;%PATH%""
String antCommand = "${antPreCommand} & ant ${antArgs}"
['cmd', '/c', antCommand]
}
要获取更多Jerry的原创文章,请关注公众号"汪子熙":