首页 > 代码库 > Gradle源码入门八
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源码入门八