首页 > 代码库 > Gradle源码一

Gradle源码一

Gradle执行的关键代码 org.gradle.initialization.DefaultGradleLauncher.java

   private void doBuildStages(Stage upTo) {
        // Evaluate init scripts
        initScriptHandler.executeScripts(gradle);

        // Evaluate settings script
        SettingsInternal settings = settingsHandler.findAndLoadSettings(gradle);
        buildListener.settingsEvaluated(settings);

        // Load build
        buildLoader.load(settings.getRootProject(), settings.getDefaultProject(), gradle, settings.getRootClassLoaderScope());
        buildListener.projectsLoaded(gradle);

        // Configure build
        buildConfigurer.configure(gradle);

        if (!gradle.getStartParameter().isConfigureOnDemand()) {
            buildListener.projectsEvaluated(gradle);
        }

        modelConfigurationListener.onConfigure(gradle);

        if (upTo == Stage.Configure) {
            return;
        }

        // Populate task graph
        buildExecuter.select(gradle);

        if (gradle.getStartParameter().isConfigureOnDemand()) {
            buildListener.projectsEvaluated(gradle);
        }

        // Execute build
        buildExecuter.execute();
        tasksCompletionListener.onTasksFinished(gradle);

        assert upTo == Stage.Build;
    }

从注释上我们可以看出gradle的几个关键步骤

A. Evaluate init scripts

1.InitScriptHandler 通过

    protected InitScriptHandler createInitScriptHandler() {
        return new InitScriptHandler(
                new DefaultInitScriptProcessor(
                        get(ScriptPluginFactory.class),
                        get(ScriptHandlerFactory.class)
                )
        );
    }
指定了
DefaultInitScriptProcessor <span style="font-family: Arial, Helvetica, sans-serif;">对象做它的 processor</span>


2.指定Script finder  (in org.gradle.startParameter.java)

从 C:\Users\xxx\.gradle\init.gradle     <-USER_HOME

C:\Users\xxx\.gradle\init.d  文件夹中endswith  ".gradle"   <- USER_HOME

C:\gradle-2.2.1\init.d 文件夹中  endswith  ".gradle"    <-  GRADLE_HOME



3.调用DefaultInitScriptProcessor.processs 函数处理


Gradle源码一