码迷,mamicode.com
首页 > 其他好文 > 详细

Gradle源码入门八

时间:2015-01-12 19:17:00      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:gradle   源码   

启动的时候,调用了


launcher.cli.BuildActionsFactory.java


    private Runnable runBuildInProcess(StartParameter startParameter, DaemonParameters daemonParameters, ServiceRegistry loggingServices) {
        ServiceRegistry globalServices = ServiceRegistryBuilder.builder()
                .displayName("Global services")
                .parent(loggingServices)
                .parent(NativeServices.getInstance())
                .provider(new GlobalScopeServices(false))
                .build();
        InProcessBuildActionExecuter executer = new InProcessBuildActionExecuter(globalServices.get(GradleLauncherFactory.class));
        return daemonBuildAction(startParameter, daemonParameters, executer);
    }
	build()会调用
	 /**
     * Adds a service provider bean to this registry. This provider may define factory and decorator methods.
     */
    public DefaultServiceRegistry addProvider(Object provider) {
        findProviderMethods(provider);
        return this;
    }


最后会走到
	org\gradle\internal\service\scopes\GlobalScopeServices.java
	
	void configure(ServiceRegistration registration, ClassLoaderRegistry classLoaderRegistry) {
        final List<PluginServiceRegistry> pluginServiceFactories = new ServiceLocator(classLoaderRegistry.getRuntimeClassLoader(), classLoaderRegistry.getPluginsClassLoader()).getAll(PluginServiceRegistry.class);
        for (PluginServiceRegistry pluginServiceRegistry : pluginServiceFactories) {
            registration.add(PluginServiceRegistry.class, pluginServiceRegistry);
            pluginServiceRegistry.registerGlobalServices(registration);
        }
    }


getAll 会根据PluginServiceRegistry.class一系列规则,找到所有factory方法,规则如下:
 
 规则就是ServiceLocator.java 的findFactoriesForServiceType(serviceType);

 resourceName = ‘META-INF/services/org.gradle.internal.service.scopes.PluginServiceRegistry‘

找到
jar:file:/C:/gradle-2.2.1/lib/gradle-launcher-2.2.1.jar!/META-INF/services/org.gradle.internal.service.scopes.PluginServiceRegistry
->[org.gradle.tooling.internal.provider.ToolingServices]

implementations.add(implClass.asSubclass(serviceType));//父类是参数,转为子类
implClass = class org.gradle.tooling.internal.provider.ToolingServices
serviceType = interface org.gradle.internal.service.scopes.PluginServiceRegistry
------------------
可能有多个classLoader去load,所以可能会找到同样的jar包,代码保证重复的就不做了
jar:file:/C:/gradle-2.2.1/lib/gradle-launcher-2.2.1.jar!/META-INF/services/org.gradle.internal.service.scopes.PluginServiceRegistry
continue
-----------------
找到
jar:file:/C:/gradle-2.2.1/lib/plugins/gradle-dependency-management-2.2.1.jar!/META-INF/services/org.gradle.internal.service.scopes.PluginServiceRegistry
->
[class org.gradle.tooling.internal.provider.ToolingServices]

把所有实现的类名加入factory列表。



Gradle源码入门八

标签:gradle   源码   

原文地址:http://blog.csdn.net/cleverwyq/article/details/42646019

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!