上一篇博客
創(chuàng)新互聯(lián)服務項目包括鶴壁網(wǎng)站建設、鶴壁網(wǎng)站制作、鶴壁網(wǎng)頁制作以及鶴壁網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,鶴壁網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到鶴壁省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!18. Gradle編譯其他應用代碼流程(六) - 執(zhí)行Task過程
一. 守護進程的作用
守護進程就是一個用來構(gòu)建的其他進程。
從前幾篇文章我們知道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); }
四. 守護進程執(zhí)行入口
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(); } } }
五. 守護進程的日志
守護進程的日志沒有打印在控制臺,而是打印在文件中,通過下面這行代碼,進行了日志輸出流轉(zhuǎn)向。
// 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源代碼編譯后文件
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。
新聞標題:20.Gradle編譯其他應用代碼流程(七)-守護進程編譯-創(chuàng)新互聯(lián)
文章來源:http://www.rwnh.cn/article34/ceihse.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、Google、網(wǎng)站導航、品牌網(wǎng)站制作、網(wǎng)站排名、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容