Gradle 源码分析(二)

1. 写在前面

在 Gradle源码分析(一)一文中,我们分析到了 DefaultGradleLauncherexecuteTasks(),接下来将从这里继续分析。Gradle的构建流程分为几个阶段,这里先分析 LoadSettings 阶段(gradle 源码版本为 5.6.4)。

2. LoadSettings

2.1 整体实现

这里我整理了 LoadSettings 阶段的一些主要操作,并绘制了调用链的时序图。如果对源码不感兴趣的同学只需要看这一部分的内容即可。

2.1.1 时序图

LoadSettings时序图.png

2.1.2 主要操作

LoadSettings 阶段 Gradle 主要做了下面这些事情。

  1. 调用 BuildListenerbuildStarted() 方法;
  2. 查找并执行 init 脚本;
  3. 查找 settings.gradle 文件;
  4. 编译 buildSrc;
  5. 解析 gradle.properties 以及一些系统配置,环境变量等;
  6. 解析 settings.gradle 文件,生成 DefaultSettings 对象;
  7. 调用 BuildListenersettingsEvaluated() 方法。

2.2 源码分析

2.2.1 调用 BuildListener 的 buildStarted

LoadSettings 的过程是发生在 DefaultGradleLauncherprepareSettings() 中,先来看看它的源码。

// DefaultGradleLauncher.java
private void prepareSettings() {
    if (stage == null) {
        // 调用 BuildListener 的 buildStarted();
        buildListener.buildStarted(gradle);
        // ... 省略部分代码
    }
}

这里首先调用了 BuildListenerbuildStarted() 。有些同学可能发现,注册 BuildListener 后,buildStarted() 并不会执行到,这其实是我们注册的时机比较晚,这个方法主要是给 Gradle 内部使用的。

2.2.2 执行 init 脚本

继续向下看 prepareSettings()

// DefaultGradleLauncher.java
private void prepareSettings() {
    if (stage == null) {
        // 调用 BuildListener 的 buildStarted();
        buildListener.buildStarted(gradle);
        settingsPreparer.prepareSettings(gradle);
        // ... 省略部分代码
    }
}

这里的 settingsPreparer 是在 DefaultGradleLauncherFactorynewInstance() 中创建 DefaultGradleLauncher 的时候从 BuildScopeServices 里面获取的。

// DefaultGradleLauncherFactory.java
private DefaultGradleLauncher doNewInstance(BuildDefinition buildDefinition,
                                            BuildState build,
                                            @Nullable GradleLauncher parent,
                                            BuildTreeScopeServices buildTreeScopeServices,
                                            List servicesToStop) {
    BuildScopeServices serviceRegistry = new BuildScopeServices(buildTreeScopeServices);

    // ... 省略部分代码

    // 通过反射的形式调用 BuildScopeServices 的createSettingsPreparer()方法
    SettingsPreparer settingsPreparer = serviceRegistry.get(SettingsPreparer.class);
    // ... 省略部分代码
    DefaultGradleLauncher gradleLauncher = new DefaultGradleLauncher(
        gradle,
        serviceRegistry.get(ProjectsPreparer.class),
        serviceRegistry.get(ExceptionAnalyser.class),
        gradle.getBuildListenerBroadcaster(),
        listenerManager.getBroadcaster(BuildCompletionListener.class),
        gradle.getServices().get(BuildWorkExecutor.class),
        serviceRegistry,
        servicesToStop,
        includedBuildControllers,
        settingsPreparer,
        taskExecutionPreparer,
        gradle.getServices().get(InstantExecution.class)
    );
    // ... 省略部分代码
}

它是通过反射调用 BuildScopeServicescreateSettingsPreparer()

// BuildScopeServices.java
protected SettingsPreparer createSettingsPreparer(InitScriptHandler initScriptHandler, SettingsLoaderFactory settingsLoaderFactory, BuildOperationExecutor buildOperationExecutor, BuildDefinition buildDefinition) {
    return new BuildOperatingFiringSettingsPreparer(
        new DefaultSettingsPreparer(
            initScriptHandler,
            settingsLoaderFactory),
        buildOperationExecutor,
        buildDefinition.getFromBuild());
}

所以 settingsPreparer 实际上是 BuildOperatingFiringSettingsPreparer,我们来看它的 prepareSettings()

// BuildOperatingFiringSettingsPreparer.java
@Override
public void prepareSettings(GradleInternal gradle) {
    buildOperationExecutor.run(new LoadBuild(gradle));
}

private class LoadBuild implements RunnableBuildOperation {
    @Override
    public void run(BuildOperationContext context) {
        doLoadBuild();
        context.setResult(RESULT);
    }

    void doLoadBuild() {
        delegate.prepareSettings(gradle);
    }
}

这里的 buildOperationExecutor 是通过反射调用CrossBuildSessionScopeServicescreateBuildOperationExecutor()

// CrossBuildSessionScopeServices.java
BuildOperationExecutor createBuildOperationExecutor() {
    return new DelegatingBuildOperationExecutor(services.get(BuildOperationExecutor.class));
}

它最终会调用到 LoadBuildrun(),继而调用到 LoadBuilddoLoadBuild(),这里的 delegate 即传入 BuildOperatingFiringSettingsPreparerDefaultSettingsPreparer对象,继续看 DefaultSettingsPreparerprepareSettings()

// DefaultSettingsPreparer.java
@Override
public void prepareSettings(GradleInternal gradle) {
    // 1. 查找并执行 init 脚本
    initScriptHandler.executeScripts(gradle);
    // ... 省略部分代码
}

这里的 initScriptHandler 也是通过反射调用 BuildScopeServicescreateInitScriptHandler()

// BuildScopeServices.java
protected InitScriptHandler createInitScriptHandler(ScriptPluginFactory scriptPluginFactory, ScriptHandlerFactory scriptHandlerFactory, BuildOperationExecutor buildOperationExecutor) {
    return new InitScriptHandler(
        new DefaultInitScriptProcessor(
            scriptPluginFactory,
            scriptHandlerFactory
        ),
        buildOperationExecutor
    );
}

也就是会继续调用到 InitScriptHandlerexecuteScripts()

public void executeScripts(final GradleInternal gradle) {
    // 1. 查找 init 文件
    final List initScripts = gradle.getStartParameter().getAllInitScripts();
    // 2. 如果没有需要执行的 init 文件,则直接返回
    if (initScripts.isEmpty()) {
        return;
    }
    // 3.执行 init 文件
    buildOperationExecutor.run(new RunnableBuildOperation() {
        @Override
        public void run(BuildOperationContext context) {
            BasicTextResourceLoader resourceLoader = new BasicTextResourceLoader();
            for (File script : initScripts) {
                TextResource resource = resourceLoader.loadFile("initialization script", script);
                processor.process(new TextResourceScriptSource(resource), gradle);
            }
        }

        @Override
        public BuildOperationDescriptor.Builder description() {
            return BuildOperationDescriptor.displayName("Run init scripts").progressDisplayName("Running init scripts");
        }
    });
}

这里就是查找 init 文件了,如果有 init 文件则执行;如果没有直接返回。这里主要关心一下查找 init 文件的过程,继续看 StartParametergetAllInitScripts()

public List getAllInitScripts() {
    // 会从 gradle user home dir 和 gradle home dir 下查找 init 文件
    CompositeInitScriptFinder initScriptFinder = new CompositeInitScriptFinder(
        new UserHomeInitScriptFinder(getGradleUserHomeDir()), new DistributionInitScriptFinder(gradleHomeDir)
    );

    List scripts = new ArrayList(getInitScripts());
    initScriptFinder.findScripts(scripts);
    return Collections.unmodifiableList(scripts);
}

getGradleUserHomeDir() 默认情况下是在 ~/.gradle 下,gradleHomeDir 默认情况下是在 ~/.gradle/wrapper/dists/gradle-5.6.4-all/xxx 下;这里会通过 UserHomeInitScriptFinder 查找 gradle user home 下的 init 文件,通过 DistributionInitScriptFinder 查找 gradle home 下的 init 文件。

// UserHomeInitScriptFinder.java
@Override
public void findScripts(Collection scripts) {
    // 查找 gradle user home dir 下的 init.gradle 或 init.gradle.kts 文件
    File userInitScript = resolveScriptFile(userHomeDir, "init");
    if (userInitScript != null) {
        scripts.add(userInitScript);
    }
    // 在 gradle user home dir 下的 init.d 目录找 .gradle 文件
    findScriptsInDir(new File(userHomeDir, "init.d"), scripts);
}

// DistributionInitScriptFinder.java
@Override
public void findScripts(Collection scripts) {
    if (gradleHome == null) {
        return;
    }
    // 在 gradle home dir 下的 init.d 目录 .gradle 文件
    findScriptsInDir(new File(gradleHome, "init.d"), scripts);
}

从源码可以看出

  1. 在 gradle user home dir 下找 init.gradle,init.gradle.kts 文件;
  2. 在 gradle user home dir 下的 init.d 目录找 .gradle 文件;
  3. 在 gradle home dir 下的 init.d 目录找 .gradle 文件。

2.2.3 查找 settings.gradle

继续看 DefaultSettingsPreparerprepareSettings()

// DefaultSettingsPreparer.java
@Override
public void prepareSettings(GradleInternal gradle) {
    // 查找并执行init文件
    initScriptHandler.executeScripts(gradle);
    // 获取 SettingsLoader
    SettingsLoader settingsLoader = gradle.getParent() != null ? settingsLoaderFactory.forNestedBuild() : settingsLoaderFactory.forTopLevelBuild();
    settingsLoader.findAndLoadSettings(gradle);
}

这里的 settingsLoaderFactory 是通过反射调用 BuildScopeServicescreateSettingsLoaderFactory()

// BuildScopeServices.java
protected SettingsLoaderFactory createSettingsLoaderFactory(SettingsProcessor settingsProcessor, BuildLayoutFactory buildLayoutFactory, BuildState currentBuild, ClassLoaderScopeRegistry classLoaderScopeRegistry, FileLockManager fileLockManager, BuildOperationExecutor buildOperationExecutor, CachedClasspathTransformer cachedClasspathTransformer, CachingServiceLocator cachingServiceLocator, BuildStateRegistry buildRegistry, ProjectStateRegistry projectRegistry, PublicBuildPath publicBuildPath) {
    return new DefaultSettingsLoaderFactory(
        new DefaultSettingsFinder(buildLayoutFactory),
        settingsProcessor,
        new BuildSourceBuilder(
            currentBuild,
            classLoaderScopeRegistry.getCoreAndPluginsScope(),
            fileLockManager,
            buildOperationExecutor,
            cachedClasspathTransformer,
            new BuildSrcBuildListenerFactory(
                PluginsProjectConfigureActions.of(
                    BuildSrcProjectConfigurationAction.class,
                    cachingServiceLocator)),
            buildRegistry,
            publicBuildPath),
        buildRegistry,
        projectRegistry,
        publicBuildPath
    );
}

这里返回的是 DefaultSettingsLoaderFactory 的对象,而 DefaultSettingsLoaderFactoryforTopLevelBuild()forNestedBuild() 最终都会调用到 defaultSettingsLoader()

// DefaultSettingsLoaderFactory.java
@Override
public SettingsLoader forTopLevelBuild() {
    return new CompositeBuildSettingsLoader(
        new ChildBuildRegisteringSettingsLoader(
            new CommandLineIncludedBuildSettingsLoader(
                defaultSettingsLoader()
            ),
            buildRegistry,
            publicBuildPath),
        buildRegistry);
}

@Override
public SettingsLoader forNestedBuild() {
    return new ChildBuildRegisteringSettingsLoader(
        defaultSettingsLoader(),
        buildRegistry,
        publicBuildPath
    );
}

private SettingsLoader defaultSettingsLoader() {
    return new SettingsAttachingSettingsLoader(
        new DefaultSettingsLoader(
            settingsFinder,
            settingsProcessor,
            buildSourceBuilder
        ),
        projectRegistry);
}

所以直接看 SettingsAttachingSettingsLoaderfindAndLoadSettings()

// SettingsAttachingSettingsLoader.java
public SettingsInternal findAndLoadSettings(GradleInternal gradle) {
    SettingsInternal settings = delegate.findAndLoadSettings(gradle);
    // ... 省略部分代码
}

这里调用的是 delegateDefaultSettingsLoaderfindAndLoadSettings()

// DefaultSettingsLoader.java
public SettingsInternal findAndLoadSettings(GradleInternal gradle) {
    StartParameter startParameter = gradle.getStartParameter();
    // 查找 settings.gradle 文件
    SettingsInternal settings = findSettingsAndLoadIfAppropriate(gradle, startParameter);

    ProjectSpec spec = ProjectSpecs.forStartParameter(startParameter, settings);
    if (useEmptySettings(spec, settings, startParameter)) {
        settings = createEmptySettings(gradle, startParameter);
    }

    setDefaultProject(spec, settings);
    return settings;
}

private SettingsInternal findSettingsAndLoadIfAppropriate(GradleInternal gradle,
                                                          StartParameter startParameter) {
    // 查找settings.gradle文件
    SettingsLocation settingsLocation = findSettings(startParameter);
    // ... 省略部分代码
}

private SettingsLocation findSettings(StartParameter startParameter) {
    return settingsFinder.find(startParameter);
}

可以看到最终调用了 settingsFinderfind(),而 settingsFinder 是在创建 DefaultSettingsLoaderFactory 时候传入的 DefaultSettingsFinder 对象,所以接着看 DefaultSettingsFinderfind()

// DefaultSettingsFinder.java
public BuildLayout find(StartParameter startParameter) {
    return layoutFactory.getLayoutFor(new BuildLayoutConfiguration(startParameter));
}

这里的 layoutFactory 是通过反射调用 GlobalScopeServicescreateBuildLayoutFactory()

// GlobalScopeServices.java
BuildLayoutFactory createBuildLayoutFactory() {
    return new BuildLayoutFactory();
}

接着看 BuildLayoutFactorygetLayoutFor()

// BuildLayoutFactory.java
public BuildLayout getLayoutFor(BuildLayoutConfiguration configuration) {
    // 空的settings.gradle
    if (configuration.isUseEmptySettings()) {
        return buildLayoutFrom(configuration, null);
    }
    // 通过参数-c xxx.gradle 指定了settings.gradle
    File explicitSettingsFile = configuration.getSettingsFile();
    if (explicitSettingsFile != null) {
        if (!explicitSettingsFile.isFile()) {
            throw new MissingResourceException(explicitSettingsFile.toURI(), String.format("Could not read settings file '%s' as it does not exist.", explicitSettingsFile.getAbsolutePath()));
        }
        return buildLayoutFrom(configuration, explicitSettingsFile);
    }

    // 当前目录及上层目录递归查找,找不到还是认为在当前目录下
    File currentDir = configuration.getCurrentDir();
    boolean searchUpwards = configuration.isSearchUpwards();
    return getLayoutFor(currentDir, searchUpwards ? null : currentDir.getParentFile());
}

BuildLayout getLayoutFor(File currentDir, File stopAt) {
    File settingsFile = findExistingSettingsFileIn(currentDir);
    if (settingsFile != null) {
        return layout(currentDir, settingsFile);
    }
    for (File candidate = currentDir.getParentFile(); candidate != null && !candidate.equals(stopAt); candidate = candidate.getParentFile()) {
        settingsFile = findExistingSettingsFileIn(candidate);
        if (settingsFile == null) {
            settingsFile = findExistingSettingsFileIn(new File(candidate, "master"));
        }
        if (settingsFile != null) {
            return layout(candidate, settingsFile);
        }
    }
    return layout(currentDir, new File(currentDir, Settings.DEFAULT_SETTINGS_FILE));
}

这个方法就是查找 settings.gradle 的过程,这里分为三步:

  1. 如果 isUseEmptySettings() 为true,则不设置 settings.gradle;
  2. 如果通过 -c xxx.gradle 指定了 settings.gradle,则使用指定的文件;
  3. 在当前目录下查找 settings.gradle,如果找不到则递归向上层寻找,以及同层级的 master 目录下寻找,如果还是找不到,默认认为在当前目录下。

2.2.4 编译 buildSrc

分析完查找 settings.gradle,继续看 DefaultSettingsLoaderfindSettingsAndLoadIfAppropriate()

// DefaultSettingsLoader.java
private SettingsInternal findSettingsAndLoadIfAppropriate(GradleInternal gradle,
                                                          StartParameter startParameter) {
    // 查找 settings.gradle
    SettingsLocation settingsLocation = findSettings(startParameter);
    // 编译buildSrc
    ClassLoaderScope buildSourceClassLoaderScope = buildSourceBuilder.buildAndCreateClassLoader(settingsLocation.getSettingsDir(), startParameter);
    // ... 省略部分代码
}

这里的 buildSourceBuilder 是在创建 DefaultSettingsLoaderFactory 传入的 BuildSourceBuilder 对象,接着看 BuildSourceBuilderbuildAndCreateClassLoader()

// BuildSourceBuilder.java
public ClassLoaderScope buildAndCreateClassLoader(File rootDir, StartParameter containingBuildParameters) {
    // 根目录下的 buildSrc 文件夹
    File buildSrcDir = new File(rootDir, DefaultSettings.DEFAULT_BUILD_SRC_DIR);
    // 编译 buildSrc 
    ClassPath classpath = createBuildSourceClasspath(buildSrcDir, containingBuildParameters);
    return classLoaderScope.createChild(buildSrcDir.getAbsolutePath())
        .export(classpath)
        .lock();
}

这里就是编译 buildSrc 文件夹了,会发现它是在解析 settings.gradle 之前的,这方便在解析 settings.gradle 的时候直接使用 buildSrc 里面的内容。

2.2.5 解析 gradle.properties

分析完编译 buildSrc,继续看 DefaultSettingsLoaderfindSettingsAndLoadIfAppropriate()

// DefaultSettingsLoader.java
private SettingsInternal findSettingsAndLoadIfAppropriate(GradleInternal gradle,
                                                          StartParameter startParameter) {
    // 查找 settings.gradle
    SettingsLocation settingsLocation = findSettings(startParameter);
    // 编译buildSrc
    ClassLoaderScope buildSourceClassLoaderScope = buildSourceBuilder.buildAndCreateClassLoader(settingsLocation.getSettingsDir(), startParameter);
    return settingsProcessor.process(gradle, settingsLocation, buildSourceClassLoaderScope, startParameter);
}

这里的 settingsProcessor 是通过反射调用 BuildScopeServicescreateSettingsProcessor()

// BuildScopeServices.java
protected SettingsProcessor createSettingsProcessor(ScriptPluginFactory scriptPluginFactory, ScriptHandlerFactory scriptHandlerFactory, Instantiator instantiator,
                                                    ServiceRegistryFactory serviceRegistryFactory, IGradlePropertiesLoader propertiesLoader, BuildOperationExecutor buildOperationExecutor, TextResourceLoader textResourceLoader) {
    return new BuildOperationSettingsProcessor(
        new RootBuildCacheControllerSettingsProcessor(
            new SettingsEvaluatedCallbackFiringSettingsProcessor(
                new PropertiesLoadingSettingsProcessor(
                    new ScriptEvaluatingSettingsProcessor(
                        scriptPluginFactory,
                        new SettingsFactory(
                            instantiator,
                            serviceRegistryFactory,
                            scriptHandlerFactory
                        ),
                        propertiesLoader,
                        textResourceLoader),
                    propertiesLoader
                )
            )
        ),
        buildOperationExecutor);
}

BuildOperationSettingsProcessor 对象,这里会一直委托调用到 PropertiesLoadingSettingsProcessorprocess(),直接看 PropertiesLoadingSettingsProcessorprocess()

// PropertiesLoadingSettingsProcessor.java
public SettingsInternal process(GradleInternal gradle,
                                SettingsLocation settingsLocation,
                                ClassLoaderScope buildRootClassLoaderScope,
                                StartParameter startParameter) {
    // 解析 gradle.properties
    propertiesLoader.loadProperties(settingsLocation.getSettingsDir());
    return processor.process(gradle, settingsLocation, buildRootClassLoaderScope, startParameter);
}

这里的 propertiesLoader 是通过反射调用 BuildScopeServicescreateGradlePropertiesLoader()

// BuildScopeServices.java
protected IGradlePropertiesLoader createGradlePropertiesLoader() {
    return new DefaultGradlePropertiesLoader((StartParameterInternal) get(StartParameter.class));
}

接着看 DefaultGradlePropertiesLoaderloadProperties()

// DefaultGradlePropertiesLoader.java
public void loadProperties(File settingsDir) {
    loadProperties(settingsDir, startParameter, getAllSystemProperties(), getAllEnvProperties());
}

void loadProperties(File settingsDir, StartParameterInternal startParameter, Map systemProperties, Map envProperties) {
    defaultProperties.clear();
    overrideProperties.clear();
    // 1. 解析 gradle home dir 的gradle.properties
    addGradleProperties(defaultProperties, new File(startParameter.getGradleHomeDir(), Project.GRADLE_PROPERTIES));
    // 2. 解析 settings.gradle 同层级的 gradle.properties
    addGradleProperties(defaultProperties, new File(settingsDir, Project.GRADLE_PROPERTIES));
    // 3. 解析 gradle user home dir 的 gradle.properties
    addGradleProperties(overrideProperties, new File(startParameter.getGradleUserHomeDir(), Project.GRADLE_PROPERTIES));
    setSystemProperties(startParameter.getSystemPropertiesArgs());
    // 4. 解析环境变量
    overrideProperties.putAll(getEnvProjectProperties(envProperties));
    // 5. 解析系统配置
    overrideProperties.putAll(getSystemProjectProperties(systemProperties));
    // 6. 解析命令行配置
    overrideProperties.putAll(startParameter.getProjectProperties());
}

这里就是解析 gradle.properties 了,可以看到主要获取了这些配置:

  1. 解析 ~/.gradle/wrapper/dists/gradle-5.6.4-all/xxx/gradle.properties 文件;
  2. 解析与 settings.gradle 同层级的 gradle.properties 文件;
  3. 解析 ~/.gradle/gradle.properties 文件;
  4. 解析环境变量;
  5. 解析系统配置;
  6. 解析命令配置;

2.2.6 解析 settings.gradle

解析完配置后 PropertiesLoadingSettingsProcessor 会委托给 ScriptEvaluatingSettingsProcessor 执行 process()

// ScriptEvaluatingSettingsProcessor.java
public SettingsInternal process(GradleInternal gradle,
                                SettingsLocation settingsLocation,
                                ClassLoaderScope buildRootClassLoaderScope,
                                StartParameter startParameter) {
    // 合并属性
    Map properties = propertiesLoader.mergeProperties(Collections.emptyMap());
    // 加载settings.gradle文件
    TextResourceScriptSource settingsScript = new TextResourceScriptSource(textResourceLoader.loadFile("settings file", settingsLocation.getSettingsFile()));
    // 生成 DefaultSettings 对象
    SettingsInternal settings = settingsFactory.createSettings(gradle, settingsLocation.getSettingsDir(), settingsScript, properties, startParameter, buildRootClassLoaderScope);
    // 解析 settings.gradle
    applySettingsScript(settingsScript, settings);
    return settings;
}

这里会加载 settings.gradle 文件,并构建 DefaultSettings 对象,然后再解析 settings.gradle。

2.2.7 调用 BuildListener 的 settingsEvaluated

解析完 settings.gradle 后逐层返回到 ScriptEvaluatingSettingsProcessorprocess()

// ScriptEvaluatingSettingsProcessor.java
public SettingsInternal process(GradleInternal gradle, SettingsLocation settingsLocation, ClassLoaderScope buildRootClassLoaderScope, StartParameter startParameter) {
    SettingsInternal settings = delegate.process(gradle, settingsLocation, buildRootClassLoaderScope, startParameter);
    // 调用 BuildListener 的 settingsEvaluated()
    gradle.getBuildListenerBroadcaster().settingsEvaluated(settings);
    return settings;
}

这里调用了 BuildListenersettingsEvaluated()。最后回到 DefaultGradleLauncherprepareSettings()

private void prepareSettings() {
    if (stage == null) {
        buildListener.buildStarted(gradle);
        settingsPreparer.prepareSettings(gradle);
        // 状态标记为 LoadSettings 
        stage = Stage.LoadSettings;
    }
}

做完上述这些事情后,会将状态标记为 LoadSettings。至此 Gradle 构建流程的 LoadSettings 就分析完了。

你可能感兴趣的:(Gradle 源码分析(二))