首页 > 代码库 > Gradle源码入门四

Gradle源码入门四

接三

如果我们运行gradle hello  (hello 是一个task)

 代码会走到org.gradle.launcher.cli.RunBuildAction,  这个类的run()函数

    public void run() {
        executer.execute(
                new ExecuteBuildAction(startParameter),
                new DefaultBuildCancellationToken(),
                new DefaultBuildActionParameters(clientMetaData, startTime, systemProperties, envVariables, currentDir, startParameter.getLogLevel()));
    }

executer is an instance of   InProcessBuildActionExecuter

public class ExecuteBuildAction implements BuildAction<Void>

改Action类有自己的run实现

    public Void run(BuildController buildController) {
        buildController.setStartParameter(startParameter);
        buildController.run();
        return null;
    }

run最走到org.gradle.initialization.DefaultGradleLauncher.java

    private BuildResult doBuild(Stage upTo) {
        loggingManager.start();
        buildListener.buildStarted(gradle);
               Throwable failure = null;
        try {
            doBuildStages(upTo);
        } catch (Throwable t) {
            failure = exceptionAnalyser.transform(t);
        }
        BuildResult buildResult = new BuildResult(gradle, failure);
        buildListener.buildFinished(buildResult);

        return buildResult;
    }

这里就接上了第一篇所讲的doBuildStages

        // Evaluate init scripts
        initScriptHandler.executeScripts(gradle);
中,会生成很多文件夹和文件

技术分享÷

InitScript/classes 下面是一个emptyScript.txt

no_initscript 下面是一个 .class 文件, 是对一些init settings文件调用Groovy的函数编译出来 的 (in org.gradle.groovy.scripts.internal ->DefaultScriptCompilationHandler.java

        String scriptText = source.getResource().getText();
        String scriptName = source.getClassName();
        GroovyCodeSource codeSource = new GroovyCodeSource(scriptText == null ? "" : scriptText, scriptName, "/groovy/script");
        try {
            groovyClassLoader.parseClass(codeSource, false);
        } catch (MultipleCompilationErrorsException e) {
            wrapCompilationFailure(source, e);
        } catch (CompilationFailedException e) {
            throw new GradleException(String.format("Could not compile %s.", source.getDisplayName()), e);
        }

堆栈如下图


技术分享

Gradle源码入门四