Previous blog post
Gradle compiling other application code flows (vi)-Executing a task procedure
I. The role of the Guardian process
The daemon is a process that is used to build.
In previous articles we knew that Gradle would load all the necessary jars when compiling, and it would take time to load the jars.
This daemon can be reused if we have a daemon that has compiled other programs and this process has not been killed.
Two. Select the daemon compilation
The policy to select the daemon compilation is
If the daemon exists, it is compiled with a daemon process.
Otherwise, if it can be compiled in the current process, it is compiled in the current process.
Otherwise, start a new daemon to compile.
The code is as follows:
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); }}
Three. Start the daemon's portal
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&nbsP;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> d aemonargs = 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); }
Four. Daemon Execution portal
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 (), &NBSp;daemon.getaddress (), daemonlog); daemonexpirationstrategy expirationstrategy = daemonservices.get ( Masterexpirationstrategy.class); Daemon.stoponexpiration (Expirationstrategy, parameters.getperiodiccheckintervalms ()); } finally { daemon.stop (); } }}
Five. Daemon Log
The daemon log is not printed in the console, but is printed in the file, and the log output stream is turned through the following line of code.
Any logging prior to this point won't end up in the daemon log file.initialiselogging (Loggingmanager, Daemonlog);
The log is saved in the gradle_user_home/daemon/version number/daemon-the process number. out.log
If Gradle_user_home is not configured, it is in the root directory. Windows words in ' c:\Users\rongwei.huang\.gradle\ '
My own log directory is: "D:\gradle_jar_cache\daemon\3.1-snapshot-1\daemon-10972.out.log"
This article is from the "www.bogo.com" blog, make sure to keep this source http://483181.blog.51cto.com/473181/1931190
Gradle compiling other application code processes (vii)-daemon compilation