2323import java .util .HashMap ;
2424import java .util .List ;
2525import java .util .Map ;
26+ import java .util .concurrent .atomic .AtomicReference ;
2627import java .util .stream .Collectors ;
2728
2829import static java .nio .charset .StandardCharsets .UTF_8 ;
@@ -45,6 +46,8 @@ public void apply(Project project) {
4546 File compilerJavaHome = findCompilerJavaHome ();
4647 File runtimeJavaHome = findRuntimeJavaHome (compilerJavaHome );
4748
49+ Object gitRevisionResolver = createGitRevisionResolver (project );
50+
4851 final List <JavaHome > javaVersions = new ArrayList <>();
4952 for (int version = 8 ; version <= Integer .parseInt (minimumCompilerVersion .getMajorVersion ()); version ++) {
5053 if (System .getenv (getJavaHomeEnvVarName (Integer .toString (version ))) != null ) {
@@ -92,7 +95,7 @@ public void apply(Project project) {
9295 ext .set ("minimumCompilerVersion" , minimumCompilerVersion );
9396 ext .set ("minimumRuntimeVersion" , minimumRuntimeVersion );
9497 ext .set ("gradleJavaVersion" , Jvm .current ().getJavaVersion ());
95- ext .set ("gitRevision" , gitRevision ( project ) );
98+ ext .set ("gitRevision" , gitRevisionResolver );
9699 ext .set ("buildDate" , ZonedDateTime .now (ZoneOffset .UTC ));
97100 });
98101 }
@@ -203,21 +206,35 @@ private static int findDefaultParallel(Project project) {
203206 return _defaultParallel ;
204207 }
205208
206- private String gitRevision (final Project project ) {
207- final ByteArrayOutputStream stdout = new ByteArrayOutputStream ();
208- final ByteArrayOutputStream stderr = new ByteArrayOutputStream ();
209- final ExecResult result = project .exec (spec -> {
210- spec .setExecutable ("git" );
211- spec .setArgs (Arrays .asList ("rev-parse" , "HEAD" ));
212- spec .setStandardOutput (stdout );
213- spec .setErrorOutput (stderr );
214- spec .setIgnoreExitValue (true );
215- });
209+ private Object createGitRevisionResolver (final Project project ) {
210+ return new Object () {
211+ private final AtomicReference <String > gitRevision = new AtomicReference <>();
212+
213+ @ Override
214+ public String toString () {
215+ if (gitRevision .get () == null ) {
216+ final ByteArrayOutputStream stdout = new ByteArrayOutputStream ();
217+ final ByteArrayOutputStream stderr = new ByteArrayOutputStream ();
218+ final ExecResult result = project .exec (spec -> {
219+ spec .setExecutable ("git" );
220+ spec .setArgs (Arrays .asList ("rev-parse" , "HEAD" ));
221+ spec .setStandardOutput (stdout );
222+ spec .setErrorOutput (stderr );
223+ spec .setIgnoreExitValue (true );
224+ });
225+
226+ final String revision ;
227+ if (result .getExitValue () != 0 ) {
228+ revision = "unknown" ;
229+ } else {
230+ revision = stdout .toString (UTF_8 ).trim ();
231+ }
232+ this .gitRevision .compareAndSet (null , revision );
233+ }
234+ return gitRevision .get ();
235+ }
236+ };
216237
217- if (result .getExitValue () != 0 ) {
218- return "unknown" ;
219- }
220- return stdout .toString (UTF_8 ).trim ();
221238 }
222239
223240}
0 commit comments