20. Gradle编译其他应用代码流程(七) - 守护进程编译
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20. Gradle编译其他应用代码流程(七) - 守护进程编译相关的知识,希望对你有一定的参考价值。
上一篇博客
18. Gradle编译其他应用代码流程(六) - 执行Task过程
一. 守护进程的作用
守护进程就是一个用来构建的其他进程。
从前几篇文章我们知道gradle编译的时候会加载各种所需要的Jar,加载这些Jar是需要时间的。
如果我们之前有守护进程编译过其他程序,而这个进程没有被kill掉,那么是可以重用这个守护进程的。
二. 选择守护进程编译
选择守护进程编译的策略是
如果守护进程存在,那么就使用守护进程编译。
否则,如果可以在当前进程编译,那么就在当前进程编译。
否则,启动新的守护进程来编译。
代码如下:
class BuildActionsFactory implements CommandLineAction { public Runnable createAction(CommandLineParser parser, ParsedCommandLine commandLine) { ... if (parameters.getDaemonParameters().isEnabled()) { return runBuildWithDaemon(parameters.getStartParameter(), parameters.getDaemonParameters(), loggingServices); } if (canUseCurrentProcess(parameters.getDaemonParameters())) { return runBuildInProcess(parameters.getStartParameter(), parameters.getDaemonParameters(), loggingServices); } return runBuildInSingleUseDaemon(parameters.getStartParameter(), parameters.getDaemonParameters(), loggingServices); } }
三. 启动守护进程的入口
public class DefaultDaemonStarter implements DaemonStarter { .... public DaemonStartupInfo startDaemon() { String daemonUid = UUID.randomUUID().toString(); GradleInstallation gradleInstallation = CurrentGradleInstallation.get(); ModuleRegistry registry = new DefaultModuleRegistry(gradleInstallation); ClassPath classpath; List<File> searchClassPath; if (gradleInstallation == null) { // When not running from a Gradle distro, need runtime impl for launcher plus the search path to look for other modules classpath = new DefaultClassPath(); for (Module module : registry.getModule("gradle-launcher").getAllRequiredModules()) { classpath = classpath.plus(module.getClasspath()); } searchClassPath = registry.getAdditionalClassPath().getAsFiles(); } else { // When running from a Gradle distro, only need launcher jar. The daemon can find everything from there. classpath = registry.getModule("gradle-launcher").getImplementationClasspath(); searchClassPath = Collections.emptyList(); } if (classpath.isEmpty()) { throw new IllegalStateException("Unable to construct a bootstrap classpath when starting the daemon"); } versionValidator.validate(daemonParameters); List<String> daemonArgs = new ArrayList<String>(); daemonArgs.add(daemonParameters.getEffectiveJvm().getJavaExecutable().getAbsolutePath()); List<String> daemonOpts = daemonParameters.getEffectiveJvmArgs(); daemonArgs.addAll(daemonOpts); daemonArgs.add("-cp"); daemonArgs.add(CollectionUtils.join(File.pathSeparator, classpath.getAsFiles())); if (Boolean.getBoolean("org.gradle.daemon.debug")) { daemonArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"); } daemonArgs.add(GradleDaemon.class.getName()); // Version isn‘t used, except by a human looking at the output of jps. daemonArgs.add(GradleVersion.current().getVersion()); // Serialize configuration to daemon via the process‘ stdin ByteArrayOutputStream serializedConfig = new ByteArrayOutputStream(); FlushableEncoder encoder = new KryoBackedEncoder(new EncodedStream.EncodedOutput(serializedConfig)); try { encoder.writeString(daemonParameters.getGradleUserHomeDir().getAbsolutePath()); encoder.writeString(daemonDir.getBaseDir().getAbsolutePath()); encoder.writeSmallInt(daemonParameters.getIdleTimeout()); encoder.writeSmallInt(daemonParameters.getPeriodicCheckInterval()); encoder.writeString(daemonUid); encoder.writeSmallInt(daemonOpts.size()); for (String daemonOpt : daemonOpts) { encoder.writeString(daemonOpt); } encoder.writeSmallInt(searchClassPath.size()); for (File file : searchClassPath) { encoder.writeString(file.getAbsolutePath()); } encoder.flush(); } catch (IOException e) { throw new UncheckedIOException(e); } ByteArrayInputStream stdInput = new ByteArrayInputStream(serializedConfig.toByteArray()); return startProcess(daemonArgs, daemonDir.getVersionedDir(), stdInput); }
四. 守护进程执行入口
public class GradleDaemon { public static void main(String[] args) { new ProcessBootstrap().run("org.gradle.launcher.daemon.bootstrap.DaemonMain", args); } } public class DaemonMain extends EntryPoint { private static final Logger LOGGER = Logging.getLogger(DaemonMain.class); private PrintStream originalOut; private PrintStream originalErr; @Override protected void doAction(String[] args, ExecutionListener listener) { ... KryoBackedDecoder decoder = new KryoBackedDecoder(new EncodedStream.EncodedInput(System.in)); try { gradleHomeDir = new File(decoder.readString()); daemonBaseDir = new File(decoder.readString()); idleTimeoutMs = decoder.readSmallInt(); periodicCheckIntervalMs = decoder.readSmallInt(); daemonUid = decoder.readString(); int argCount = decoder.readSmallInt(); startupOpts = new ArrayList<String>(argCount); for (int i = 0; i < argCount; i++) { startupOpts.add(decoder.readString()); } int additionalClassPathLength = decoder.readSmallInt(); additionalClassPath = new ArrayList<File>(additionalClassPathLength); for (int i = 0; i < additionalClassPathLength; i++) { additionalClassPath.add(new File(decoder.readString())); } } catch (EOFException e) { System.out.println("DaemonMain doAction 4"); throw new UncheckedIOException(e); } NativeServices.initialize(gradleHomeDir); DaemonServerConfiguration parameters = new DefaultDaemonServerConfiguration(daemonUid, daemonBaseDir, idleTimeoutMs, periodicCheckIntervalMs, startupOpts); LoggingServiceRegistry loggingRegistry = LoggingServiceRegistry.newCommandLineProcessLogging(); LoggingManagerInternal loggingManager = loggingRegistry.newInstance(LoggingManagerInternal.class); TrueTimeProvider timeProvider = new TrueTimeProvider(); DaemonServices daemonServices = new DaemonServices(parameters, loggingRegistry, loggingManager, new DefaultClassPath(additionalClassPath), timeProvider.getCurrentTime()); File daemonLog = daemonServices.getDaemonLogFile(); // Any logging prior to this point will not end up in the daemon log file. initialiseLogging(loggingManager, daemonLog); LOGGER.debug("Assuming the daemon was started with following jvm opts: {}", startupOpts); Daemon daemon = daemonServices.get(Daemon.class); daemon.start(); try { DaemonContext daemonContext = daemonServices.get(DaemonContext.class); Long pid = daemonContext.getPid(); daemonStarted(pid, daemon.getUid(), daemon.getAddress(), daemonLog); DaemonExpirationStrategy expirationStrategy = daemonServices.get(MasterExpirationStrategy.class); daemon.stopOnExpiration(expirationStrategy, parameters.getPeriodicCheckIntervalMs()); } finally { daemon.stop(); } } }
五. 守护进程的日志
守护进程的日志没有打印在控制台,而是打印在文件中,通过下面这行代码,进行了日志输出流转向。
// Any logging prior to this point will not end up in the daemon log file. initialiseLogging(loggingManager, daemonLog);
日志保存的位置在 GRADLE_USER_HOME/daemon/版本号/daemon-进程号.out.log
如果没有配置GRADLE_USER_HOME,那就在根目录。windows的话在‘c:\Users\rongwei.huang\.gradle\‘
我自己的日志目录是:"d:\gradle_jar_cache\daemon\3.1-snapshot-1\daemon-10972.out.log"
本文出自 “www.bogo.com” 博客,请务必保留此出处http://483181.blog.51cto.com/473181/1931190
以上是关于20. Gradle编译其他应用代码流程(七) - 守护进程编译的主要内容,如果未能解决你的问题,请参考以下文章
Android Gradle安卓应用构建流程 ( 资源文件编译 )
Android Gradle安卓应用构建流程 ( Java 源码编译 和 AIDL 文件编译 )