Gradle源码入门三

本文以在command窗口执行最简单的 gradle -v 为例子说明
从上文提到的EntryPoint.java开始,调用该类的doAction</p><p>实际调用到Main.java  的doAction
<pre name="code" class="java">    protected void doAction(String[] args, ExecutionListener listener) {
        createActionFactory().convert(Arrays.asList(args)).execute(listener);
    }

CommandLineActionFactory createActionFactory() {

return new CommandLineActionFactory();

}

convert是一个很重要的函数,

* <p>Converts the given command-line arguments to an {@link Action} which performs the action requested by the

* command-line args.

    public Action<ExecutionListener> convert(List<String> args) {
        ServiceRegistry loggingServices = createLoggingServices();

        LoggingConfiguration loggingConfiguration = new LoggingConfiguration();

        return new ExceptionReportingAction(
                new WithLogging(loggingServices, args, loggingConfiguration,
                        new JavaRuntimeValidationAction(
                            new ParseAndBuildAction(loggingServices, args))),
                new BuildExceptionReporter(loggingServices.get(StyledTextOutputFactory.class), loggingConfiguration, clientMetaData()));
    }

在convert函数中, 把

ExceptionReportingAction, WithLogging, ParseAndBuildAction 和 <pre name="code" class="java">JavaRuntimeValidationAction 用构造函数参数的方式‘串联’起来,实现逐步调用的效果

代码走到

ParseAndBuildAction.createAction

    private static class BuiltInActions implements CommandLineAction {
        public void configureCommandLineParser(CommandLineParser parser) {
            parser.option(HELP, "?", "help").hasDescription("Shows this help message.");
            parser.option(VERSION, "version").hasDescription("Print version info.");
        }

        public Runnable createAction(CommandLineParser parser, ParsedCommandLine commandLine) {
            if (commandLine.hasOption(HELP)) {
                return new ShowUsageAction(parser);
            }
            if (commandLine.hasOption(VERSION)) {
                return new ShowVersionAction();
            }
            return null;
        }
    }

本例子中有参数v, 返回new ShowVersionAction();

进入

    private static class ShowVersionAction implements Runnable {
        public void run() {
            GradleVersion currentVersion = GradleVersion.current();

            final StringBuilder sb = new StringBuilder();
            sb.append("\n------------------------------------------------------------\nGradle ");
            sb.append(currentVersion.getVersion());
            sb.append("\n------------------------------------------------------------\n\nBuild time:   ");
            sb.append(currentVersion.getBuildTime());
            sb.append("\nBuild number: ");
            sb.append(currentVersion.getBuildNumber());
            sb.append("\nRevision:     ");
            sb.append(currentVersion.getRevision());
            sb.append("\n\nGroovy:       ");
            sb.append(GroovySystem.getVersion());
            sb.append("\nAnt:          ");
            sb.append(Main.getAntVersion());
            sb.append("\nJVM:          ");
            sb.append(Jvm.current());
            sb.append("\nOS:           ");
            sb.append(OperatingSystem.current());
            sb.append("\n");

            System.out.println(sb.toString());
        }
    }

这样就打出了gradle的版本信息

时间: 2024-10-12 16:06:48

Gradle源码入门三的相关文章

Gradle源码入门四

接三 如果我们运行gradle hello  (hello 是一个task) 代码会走到org.gradle.launcher.cli.RunBuildAction,  这个类的run()函数 public void run() { executer.execute( new ExecuteBuildAction(startParameter), new DefaultBuildCancellationToken(), new DefaultBuildActionParameters(clien

Gradle源码入门学习之十-- Listener &amp; Dispatch

new ListenerBroadcast<OutputEventListener>(OutputEventListener.class); -> public ListenerBroadcast(Class<T> type) { this.type = type; broadcast = new BroadcastDispatch<T>(type); source = new ProxyDispatchAdapter<T>(broadcast, ty

Gradle源码入门九-settings的include是如何工作的

先上call stack script.run()  in org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory 本代码中,script=settings_ani2pwefx3gv..... ->org.gradle.groovy.scripts.BasicScript.methodMissing(String name, Object params)  //name = 'include' params='sub_proje

Gradle源码入门八

启动的时候,调用了  launcher.cli.BuildActionsFactory.java private Runnable runBuildInProcess(StartParameter startParameter, DaemonParameters daemonParameters, ServiceRegistry loggingServices) { ServiceRegistry globalServices = ServiceRegistryBuilder.builder

gradle源码入门学习六

DefaultServiceRegistry之findProviderMethods private void findProviderMethods(Object target) { Set<String> methods = new HashSet<String>(); for (Class<?> type = target.getClass(); type != Object.class; type = type.getSuperclass()) { findDe

Gradle源码入门五

分析DefaultGradleLauncher.java private void doBuildStages(Stage upTo) 函数的第二步 // Evaluate settings script SettingsInternal settings = settingsHandler.findAndLoadSettings(gradle); buildListener.settingsEvaluated(settings); 从 startParameter 开始 StartParame

如何用Android Studio查看build.gradle源码

上一篇博客里讲过 build.gradle 里的每一行代码基本都是在调用一个方法,既然是这样,我们就可以用 android studio(下面简称as) 去查看它源码的方法注释说明,这样就可以理解每个方法是做什么的了,就算是在大神的 build.gradle 遇见一些没看懂的代码,也可以点进去看方法介绍来理解.就像我们在查看 SDK 里的各种方法一样. 但是有个问题,应该也有小伙伴跟我一样吧,每次在大神的 build.gradle 看到一堆不懂的代码时,按住 Ctrl 键后,鼠标移到代码上时有个

Nouveau源码分析(三):NVIDIA设备初始化之nouveau_drm_probe

Nouveau源码分析(三) 向DRM注册了Nouveau驱动之后,内核中的PCI模块就会扫描所有没有对应驱动的设备,然后和nouveau_drm_pci_table对照. 对于匹配的设备,PCI模块就调用对应的probe函数,也就是nouveau_drm_probe. // /drivers/gpu/drm/nouveau/nouveau_drm.c 281 static int nouveau_drm_probe(struct pci_dev *pdev, 282 const struct

[Android]Fragment源码分析(三) 事务

Fragment管理中,不得不谈到的就是它的事务管理,它的事务管理写的非常的出彩.我们先引入一个简单常用的Fragment事务管理代码片段: FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction(); ft.add(R.id.fragmentContainer, fragment, "tag"); ft.addToBackStack("<span style="fo