扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
上一篇博客
创新互联服务项目包括鹤壁网站建设、鹤壁网站制作、鹤壁网页制作以及鹤壁网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,鹤壁网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到鹤壁省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!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; ListsearchClassPath; 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 daemonArgs = new ArrayList (); daemonArgs.add(daemonParameters.getEffectiveJvm().getJavaExecutable().getAbsolutePath()); List 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(argCount); for (int i = 0; i < argCount; i++) { startupOpts.add(decoder.readString()); } int additionalClassPathLength = decoder.readSmallInt(); additionalClassPath = new ArrayList (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"
最后附加自己编译的gradle3.1源代码和编译生成文件
gradle3.1源代码
gradle3.1源代码编译后文件
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流