1
1
/*
2
- * Copyright 2012-2021 the original author or authors.
2
+ * Copyright 2012-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .boot .buildpack .platform .build ;
18
18
19
19
import java .io .File ;
20
+ import java .time .Instant ;
21
+ import java .time .format .DateTimeParseException ;
20
22
import java .util .Arrays ;
21
23
import java .util .Collections ;
22
24
import java .util .LinkedHashMap ;
@@ -79,6 +81,8 @@ public class BuildRequest {
79
81
80
82
private final Cache launchCache ;
81
83
84
+ private final Instant createdDate ;
85
+
82
86
BuildRequest (ImageReference name , Function <Owner , TarArchive > applicationContent ) {
83
87
Assert .notNull (name , "Name must not be null" );
84
88
Assert .notNull (applicationContent , "ApplicationContent must not be null" );
@@ -98,12 +102,14 @@ public class BuildRequest {
98
102
this .tags = Collections .emptyList ();
99
103
this .buildCache = null ;
100
104
this .launchCache = null ;
105
+ this .createdDate = null ;
101
106
}
102
107
103
108
BuildRequest (ImageReference name , Function <Owner , TarArchive > applicationContent , ImageReference builder ,
104
109
ImageReference runImage , Creator creator , Map <String , String > env , boolean cleanCache ,
105
110
boolean verboseLogging , PullPolicy pullPolicy , boolean publish , List <BuildpackReference > buildpacks ,
106
- List <Binding > bindings , String network , List <ImageReference > tags , Cache buildCache , Cache launchCache ) {
111
+ List <Binding > bindings , String network , List <ImageReference > tags , Cache buildCache , Cache launchCache ,
112
+ Instant createdDate ) {
107
113
this .name = name ;
108
114
this .applicationContent = applicationContent ;
109
115
this .builder = builder ;
@@ -120,6 +126,7 @@ public class BuildRequest {
120
126
this .tags = tags ;
121
127
this .buildCache = buildCache ;
122
128
this .launchCache = launchCache ;
129
+ this .createdDate = createdDate ;
123
130
}
124
131
125
132
/**
@@ -131,7 +138,8 @@ public BuildRequest withBuilder(ImageReference builder) {
131
138
Assert .notNull (builder , "Builder must not be null" );
132
139
return new BuildRequest (this .name , this .applicationContent , builder .inTaggedOrDigestForm (), this .runImage ,
133
140
this .creator , this .env , this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish ,
134
- this .buildpacks , this .bindings , this .network , this .tags , this .buildCache , this .launchCache );
141
+ this .buildpacks , this .bindings , this .network , this .tags , this .buildCache , this .launchCache ,
142
+ this .createdDate );
135
143
}
136
144
137
145
/**
@@ -142,7 +150,8 @@ public BuildRequest withBuilder(ImageReference builder) {
142
150
public BuildRequest withRunImage (ImageReference runImageName ) {
143
151
return new BuildRequest (this .name , this .applicationContent , this .builder , runImageName .inTaggedOrDigestForm (),
144
152
this .creator , this .env , this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish ,
145
- this .buildpacks , this .bindings , this .network , this .tags , this .buildCache , this .launchCache );
153
+ this .buildpacks , this .bindings , this .network , this .tags , this .buildCache , this .launchCache ,
154
+ this .createdDate );
146
155
}
147
156
148
157
/**
@@ -154,7 +163,7 @@ public BuildRequest withCreator(Creator creator) {
154
163
Assert .notNull (creator , "Creator must not be null" );
155
164
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , creator , this .env ,
156
165
this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
157
- this .network , this .tags , this .buildCache , this .launchCache );
166
+ this .network , this .tags , this .buildCache , this .launchCache , this . createdDate );
158
167
}
159
168
160
169
/**
@@ -170,7 +179,8 @@ public BuildRequest withEnv(String name, String value) {
170
179
env .put (name , value );
171
180
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator ,
172
181
Collections .unmodifiableMap (env ), this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish ,
173
- this .buildpacks , this .bindings , this .network , this .tags , this .buildCache , this .launchCache );
182
+ this .buildpacks , this .bindings , this .network , this .tags , this .buildCache , this .launchCache ,
183
+ this .createdDate );
174
184
}
175
185
176
186
/**
@@ -185,7 +195,7 @@ public BuildRequest withEnv(Map<String, String> env) {
185
195
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator ,
186
196
Collections .unmodifiableMap (updatedEnv ), this .cleanCache , this .verboseLogging , this .pullPolicy ,
187
197
this .publish , this .buildpacks , this .bindings , this .network , this .tags , this .buildCache ,
188
- this .launchCache );
198
+ this .launchCache , this . createdDate );
189
199
}
190
200
191
201
/**
@@ -196,7 +206,7 @@ public BuildRequest withEnv(Map<String, String> env) {
196
206
public BuildRequest withCleanCache (boolean cleanCache ) {
197
207
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
198
208
cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
199
- this .network , this .tags , this .buildCache , this .launchCache );
209
+ this .network , this .tags , this .buildCache , this .launchCache , this . createdDate );
200
210
}
201
211
202
212
/**
@@ -207,7 +217,7 @@ public BuildRequest withCleanCache(boolean cleanCache) {
207
217
public BuildRequest withVerboseLogging (boolean verboseLogging ) {
208
218
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
209
219
this .cleanCache , verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
210
- this .network , this .tags , this .buildCache , this .launchCache );
220
+ this .network , this .tags , this .buildCache , this .launchCache , this . createdDate );
211
221
}
212
222
213
223
/**
@@ -218,7 +228,7 @@ public BuildRequest withVerboseLogging(boolean verboseLogging) {
218
228
public BuildRequest withPullPolicy (PullPolicy pullPolicy ) {
219
229
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
220
230
this .cleanCache , this .verboseLogging , pullPolicy , this .publish , this .buildpacks , this .bindings ,
221
- this .network , this .tags , this .buildCache , this .launchCache );
231
+ this .network , this .tags , this .buildCache , this .launchCache , this . createdDate );
222
232
}
223
233
224
234
/**
@@ -229,7 +239,7 @@ public BuildRequest withPullPolicy(PullPolicy pullPolicy) {
229
239
public BuildRequest withPublish (boolean publish ) {
230
240
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
231
241
this .cleanCache , this .verboseLogging , this .pullPolicy , publish , this .buildpacks , this .bindings ,
232
- this .network , this .tags , this .buildCache , this .launchCache );
242
+ this .network , this .tags , this .buildCache , this .launchCache , this . createdDate );
233
243
}
234
244
235
245
/**
@@ -253,7 +263,7 @@ public BuildRequest withBuildpacks(List<BuildpackReference> buildpacks) {
253
263
Assert .notNull (buildpacks , "Buildpacks must not be null" );
254
264
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
255
265
this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , buildpacks , this .bindings ,
256
- this .network , this .tags , this .buildCache , this .launchCache );
266
+ this .network , this .tags , this .buildCache , this .launchCache , this . createdDate );
257
267
}
258
268
259
269
/**
@@ -277,7 +287,7 @@ public BuildRequest withBindings(List<Binding> bindings) {
277
287
Assert .notNull (bindings , "Bindings must not be null" );
278
288
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
279
289
this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , bindings ,
280
- this .network , this .tags , this .buildCache , this .launchCache );
290
+ this .network , this .tags , this .buildCache , this .launchCache , this . createdDate );
281
291
}
282
292
283
293
/**
@@ -289,7 +299,7 @@ public BuildRequest withBindings(List<Binding> bindings) {
289
299
public BuildRequest withNetwork (String network ) {
290
300
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
291
301
this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
292
- network , this .tags , this .buildCache , this .launchCache );
302
+ network , this .tags , this .buildCache , this .launchCache , this . createdDate );
293
303
}
294
304
295
305
/**
@@ -311,7 +321,7 @@ public BuildRequest withTags(List<ImageReference> tags) {
311
321
Assert .notNull (tags , "Tags must not be null" );
312
322
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
313
323
this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
314
- this .network , tags , this .buildCache , this .launchCache );
324
+ this .network , tags , this .buildCache , this .launchCache , this . createdDate );
315
325
}
316
326
317
327
/**
@@ -323,7 +333,7 @@ public BuildRequest withBuildCache(Cache buildCache) {
323
333
Assert .notNull (buildCache , "BuildCache must not be null" );
324
334
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
325
335
this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
326
- this .network , this .tags , buildCache , this .launchCache );
336
+ this .network , this .tags , buildCache , this .launchCache , this . createdDate );
327
337
}
328
338
329
339
/**
@@ -335,7 +345,31 @@ public BuildRequest withLaunchCache(Cache launchCache) {
335
345
Assert .notNull (launchCache , "LaunchCache must not be null" );
336
346
return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
337
347
this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
338
- this .network , this .tags , this .buildCache , launchCache );
348
+ this .network , this .tags , this .buildCache , launchCache , this .createdDate );
349
+ }
350
+
351
+ /**
352
+ * Return a new {@link BuildRequest} with an updated created date.
353
+ * @param createdDate the created date
354
+ * @return an updated build request
355
+ */
356
+ public BuildRequest withCreatedDate (String createdDate ) {
357
+ Assert .notNull (createdDate , "CreatedDate must not be null" );
358
+ return new BuildRequest (this .name , this .applicationContent , this .builder , this .runImage , this .creator , this .env ,
359
+ this .cleanCache , this .verboseLogging , this .pullPolicy , this .publish , this .buildpacks , this .bindings ,
360
+ this .network , this .tags , this .buildCache , this .launchCache , parseCreatedDate (createdDate ));
361
+ }
362
+
363
+ private Instant parseCreatedDate (String createdDate ) {
364
+ if ("now" .equalsIgnoreCase (createdDate )) {
365
+ return Instant .now ();
366
+ }
367
+ try {
368
+ return Instant .parse (createdDate );
369
+ }
370
+ catch (DateTimeParseException ex ) {
371
+ throw new IllegalArgumentException ("Error parsing '" + createdDate + "' as an image created date" , ex );
372
+ }
339
373
}
340
374
341
375
/**
@@ -471,6 +505,14 @@ public Cache getLaunchCache() {
471
505
return this .launchCache ;
472
506
}
473
507
508
+ /**
509
+ * Return the custom created date that should be used by the lifecycle.
510
+ * @return the created date
511
+ */
512
+ public Instant getCreatedDate () {
513
+ return this .createdDate ;
514
+ }
515
+
474
516
/**
475
517
* Factory method to create a new {@link BuildRequest} from a JAR file.
476
518
* @param jarFile the source jar file
0 commit comments