2121import java .nio .charset .Charset ;
2222import java .nio .charset .StandardCharsets ;
2323import java .util .Date ;
24+ import java .util .HashMap ;
2425import java .util .List ;
2526import java .util .Map ;
2627import java .util .Properties ;
3233import org .gradle .api .file .RegularFileProperty ;
3334import org .gradle .api .plugins .ExtensionAware ;
3435import org .gradle .api .tasks .CacheableTask ;
36+ import org .gradle .api .tasks .Input ;
3537import org .gradle .api .tasks .InputDirectory ;
38+ import org .gradle .api .tasks .Internal ;
3639import org .gradle .api .tasks .OutputFile ;
3740import org .gradle .api .tasks .PathSensitive ;
3841import org .gradle .api .tasks .PathSensitivity ;
4144import pl .project13 .core .CommitIdPropertiesOutputFormat ;
4245import pl .project13 .core .GitCommitIdExecutionException ;
4346import pl .project13 .core .GitCommitIdPlugin ;
47+ import pl .project13 .core .GitCommitPropertyConstant ;
4448import pl .project13 .core .git .GitDescribeConfig ;
4549import pl .project13 .core .log .LogInterface ;
4650import pl .project13 .core .util .BuildFileChangeListener ;
@@ -58,32 +62,40 @@ public class GitCommitIdPluginGenerationTask extends DefaultTask {
5862 * task execution graph.
5963 */
6064 public static final String NAME = "gitCommitIdGenerationTask" ;
65+ public static final String PLUGIN_EXECUTION_MESSAGE =
66+ "Executing GitCommitIdPlugin to gather relevant properties" ;
67+ private HashMap <String , String > lastGeneratedOutput ;
6168
6269 /**
6370 * The {@link GitCommitIdPluginExtension} that serves as configuration of the plugin / task.
6471 *
6572 * @return The {@link GitCommitIdPluginExtension}
6673 */
67- private GitCommitIdPluginExtension getGitCommitIdPluginExtension () {
74+ @ Internal
75+ public GitCommitIdPluginExtension getGitCommitIdPluginExtension () {
6876 return getProject ().getExtensions ().findByType (GitCommitIdPluginExtension .class );
6977 }
7078
71- private GitCommitIdPluginOutputSettingsExtension getGitCommitIdPluginOutputSettingsExtension () {
79+ @ Internal
80+ public GitCommitIdPluginOutputSettingsExtension getGitCommitIdPluginOutputSettingsExtension () {
7281 return ((ExtensionAware ) getGitCommitIdPluginExtension ()).getExtensions ()
7382 .findByType (GitCommitIdPluginOutputSettingsExtension .class );
7483 }
7584
76- private GitCommitIdPluginGitSettingsExtension getGitCommitIdPluginGitSettingsExtension () {
85+ @ Internal
86+ public GitCommitIdPluginGitSettingsExtension getGitCommitIdPluginGitSettingsExtension () {
7787 return ((ExtensionAware ) getGitCommitIdPluginExtension ()).getExtensions ()
7888 .findByType (GitCommitIdPluginGitSettingsExtension .class );
7989 }
8090
81- private GitCommitIdPluginFormatSettingsExtension getGitCommitIdPluginFormatSettingsExtension () {
91+ @ Internal
92+ public GitCommitIdPluginFormatSettingsExtension getGitCommitIdPluginFormatSettingsExtension () {
8293 return ((ExtensionAware ) getGitCommitIdPluginExtension ()).getExtensions ()
8394 .findByType (GitCommitIdPluginFormatSettingsExtension .class );
8495 }
8596
86- private GitCommitIdPluginFilterSettingsExtension getGitCommitIdPluginFilterSettingsExtension () {
97+ @ Internal
98+ public GitCommitIdPluginFilterSettingsExtension getGitCommitIdPluginFilterSettingsExtension () {
8799 return ((ExtensionAware ) getGitCommitIdPluginExtension ()).getExtensions ()
88100 .findByType (GitCommitIdPluginFilterSettingsExtension .class );
89101 }
@@ -115,12 +127,37 @@ public RegularFileProperty getOutput() {
115127 return getGitCommitIdPluginOutputSettingsExtension ().getOutputFile ();
116128 }
117129
130+ /**
131+ * Attempt to support up-to-date checks with the generated properties.
132+ * A similar concept is used in {@link org.gradle.api.tasks.WriteProperties}.
133+ *
134+ * @return
135+ * The properties that may have been generated by previous runs, or if not up-to-date
136+ * recalculates the properties by execution the plugin again.
137+ */
138+ @ Input
139+ public HashMap <String , String > getGeneratedProperties () {
140+ if (lastGeneratedOutput != null ) {
141+ return lastGeneratedOutput ;
142+ }
143+ lastGeneratedOutput = runThePlugin ();
144+ return lastGeneratedOutput ;
145+ }
146+
118147 /**
119148 * The task action that ties it all together and runs the underlying logic of gathering the data
120149 * and exporting it to the relevant locations.
121150 */
122151 @ TaskAction
123152 public void runTheTask () {
153+ getProject ()
154+ .getExtensions ()
155+ .getExtraProperties ()
156+ .set ("gitProperties" , getGeneratedProperties ());
157+ }
158+
159+ private HashMap <String , String > runThePlugin () {
160+ getLogger ().debug (PLUGIN_EXECUTION_MESSAGE );
124161 GitCommitIdPluginExtension extension = getGitCommitIdPluginExtension ();
125162 boolean verbose = extension .getVerbose ().get ();
126163 final LogInterface log = new LogInterface () {
@@ -208,7 +245,7 @@ public List<String> getExcludeProperties() {
208245 @ Override
209246 public List <String > getIncludeOnlyProperties () {
210247 return getGitCommitIdPluginFilterSettingsExtension ()
211- .getIncludeOnlyProperties ().get ();
248+ .getIncludeOnlyProperties ().get ();
212249 }
213250
214251 @ Nullable
@@ -246,7 +283,7 @@ public CommitIdGenerationMode getCommitIdGenerationMode() {
246283 @ Override
247284 public boolean getUseBranchNameFromBuildEnvironment () {
248285 return getGitCommitIdPluginGitSettingsExtension ()
249- .getShouldUseBranchNameFromBuildEnvironment ().get ();
286+ .getShouldUseBranchNameFromBuildEnvironment ().get ();
250287 }
251288
252289 @ Override
@@ -262,13 +299,13 @@ public String getEvaluateOnCommit() {
262299 @ Override
263300 public File getDotGitDirectory () {
264301 return getGitCommitIdPluginGitSettingsExtension ()
265- .getDotGitDirectory ().get ().getAsFile ();
302+ .getDotGitDirectory ().get ().getAsFile ();
266303 }
267304
268305 @ Override
269306 public boolean shouldGenerateGitPropertiesFile () {
270307 return getGitCommitIdPluginOutputSettingsExtension ()
271- .getShouldGenerateOutputFile ().get ();
308+ .getShouldGenerateOutputFile ().get ();
272309 }
273310
274311 @ Override
@@ -284,8 +321,8 @@ public void performPropertiesReplacement(Properties properties) {
284321 @ Override
285322 public CommitIdPropertiesOutputFormat getPropertiesOutputFormat () {
286323 return getGitCommitIdPluginOutputSettingsExtension ()
287- .getOutputFormat ()
288- .get ();
324+ .getOutputFormat ()
325+ .get ();
289326 }
290327
291328 @ Override
@@ -308,8 +345,8 @@ public File getProjectBaseDir() {
308345 @ Override
309346 public File getGenerateGitPropertiesFile () {
310347 return getGitCommitIdPluginOutputSettingsExtension ()
311- .getOutputFile ()
312- .get ().getAsFile ();
348+ .getOutputFile ()
349+ .get ().getAsFile ();
313350 }
314351
315352 @ Override
@@ -328,15 +365,27 @@ public Charset getPropertiesSourceCharset() {
328365 @ Override
329366 public boolean shouldPropertiesEscapeUnicode () {
330367 return getGitCommitIdPluginOutputSettingsExtension ()
331- .getShouldEscapedUnicodeForPropertiesOutput ().get ();
368+ .getShouldEscapedUnicodeForPropertiesOutput ().get ();
332369 }
333370 };
334371
335372 try {
336- Properties properties = null ;
373+ Properties properties = new Properties () ;
337374 pl .project13 .core .GitCommitIdPlugin .runPlugin (cb , properties );
338375
339- // getLogger().info("GitCommitIdPlugin says: '%s'", properties);
376+ // Convert it to a hashmap, excluding build_time
377+ var prefix = getGitCommitIdPluginFormatSettingsExtension ().getPropertyPrefix ().get ();
378+ HashMap <String , String > hm = new HashMap <>();
379+ for (Map .Entry <Object , Object > e : properties .entrySet ()) {
380+ String key = (String ) e .getKey ();
381+ // DO NOT EXPOSE BUILD_TIME, otherwise up-to-date checks will not work
382+ if (key .startsWith (prefix ) && key .endsWith (GitCommitPropertyConstant .BUILD_TIME )) {
383+ hm .put (key , "" );
384+ } else {
385+ hm .put (key , (String ) e .getValue ());
386+ }
387+ }
388+ return hm ;
340389 } catch (GitCommitIdExecutionException e ) {
341390 throw new RuntimeException (e );
342391 }
0 commit comments