-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
BuildRequest.java
247 lines (214 loc) · 7.99 KB
/
BuildRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.buildpack.platform.build;
import java.io.File;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
import org.springframework.boot.buildpack.platform.io.Owner;
import org.springframework.boot.buildpack.platform.io.TarArchive;
import org.springframework.util.Assert;
/**
* A build request to be handled by the {@link Builder}.
*
* @author Phillip Webb
* @author Scott Frederick
* @since 2.3.0
*/
public class BuildRequest {
static final String DEFAULT_BUILDER_IMAGE_NAME = "gcr.io/paketo-buildpacks/builder:base-platform-api-0.3";
private static final ImageReference DEFAULT_BUILDER = ImageReference.of(DEFAULT_BUILDER_IMAGE_NAME);
private final ImageReference name;
private final Function<Owner, TarArchive> applicationContent;
private final ImageReference builder;
private final Creator creator;
private final Map<String, String> env;
private final boolean cleanCache;
private final boolean verboseLogging;
BuildRequest(ImageReference name, Function<Owner, TarArchive> applicationContent) {
Assert.notNull(name, "Name must not be null");
Assert.notNull(applicationContent, "ApplicationContent must not be null");
this.name = name.inTaggedForm();
this.applicationContent = applicationContent;
this.builder = DEFAULT_BUILDER;
this.env = Collections.emptyMap();
this.cleanCache = false;
this.verboseLogging = false;
this.creator = Creator.withVersion("");
}
BuildRequest(ImageReference name, Function<Owner, TarArchive> applicationContent, ImageReference builder,
Creator creator, Map<String, String> env, boolean cleanCache, boolean verboseLogging) {
this.name = name;
this.applicationContent = applicationContent;
this.builder = builder;
this.creator = creator;
this.env = env;
this.cleanCache = cleanCache;
this.verboseLogging = verboseLogging;
}
/**
* Return a new {@link BuildRequest} with an updated builder.
* @param builder the new builder to use
* @return an updated build request
*/
public BuildRequest withBuilder(ImageReference builder) {
Assert.notNull(builder, "Builder must not be null");
builder = (builder.getDigest() != null) ? builder : builder.inTaggedForm();
return new BuildRequest(this.name, this.applicationContent, builder, this.creator, this.env, this.cleanCache,
this.verboseLogging);
}
/**
* Return a new {@link BuildRequest} with an updated builder.
* @param creator the new {@code Creator} to use
* @return an updated build request
*/
public BuildRequest withCreator(Creator creator) {
Assert.notNull(creator, "Creator must not be null");
return new BuildRequest(this.name, this.applicationContent, this.builder, creator, this.env, this.cleanCache,
this.verboseLogging);
}
/**
* Return a new {@link BuildRequest} with an additional env variable.
* @param name the variable name
* @param value the variable value
* @return an updated build request
*/
public BuildRequest withEnv(String name, String value) {
Assert.hasText(name, "Name must not be empty");
Assert.hasText(value, "Value must not be empty");
Map<String, String> env = new LinkedHashMap<>(this.env);
env.put(name, value);
return new BuildRequest(this.name, this.applicationContent, this.builder, this.creator,
Collections.unmodifiableMap(env), this.cleanCache, this.verboseLogging);
}
/**
* Return a new {@link BuildRequest} with an additional env variables.
* @param env the additional variables
* @return an updated build request
*/
public BuildRequest withEnv(Map<String, String> env) {
Assert.notNull(env, "Env must not be null");
Map<String, String> updatedEnv = new LinkedHashMap<>(this.env);
updatedEnv.putAll(env);
return new BuildRequest(this.name, this.applicationContent, this.builder, this.creator,
Collections.unmodifiableMap(updatedEnv), this.cleanCache, this.verboseLogging);
}
/**
* Return a new {@link BuildRequest} with an specific clean cache settings.
* @param cleanCache if the cache should be cleaned
* @return an updated build request
*/
public BuildRequest withCleanCache(boolean cleanCache) {
return new BuildRequest(this.name, this.applicationContent, this.builder, this.creator, this.env, cleanCache,
this.verboseLogging);
}
/**
* Return a new {@link BuildRequest} with an specific verbose logging settings.
* @param verboseLogging if verbose logging should be used
* @return an updated build request
*/
public BuildRequest withVerboseLogging(boolean verboseLogging) {
return new BuildRequest(this.name, this.applicationContent, this.builder, this.creator, this.env,
this.cleanCache, verboseLogging);
}
/**
* Return the name of the image that should be created.
* @return the name of the image
*/
public ImageReference getName() {
return this.name;
}
/**
* Return a {@link TarArchive} containing the application content that the buildpack
* should package. This is typically the contents of the Jar.
* @param owner the owner of the tar entries
* @return the application content
* @see TarArchive#fromZip(File, Owner)
*/
public TarArchive getApplicationContent(Owner owner) {
return this.applicationContent.apply(owner);
}
/**
* Return the builder that should be used.
* @return the builder to use
*/
public ImageReference getBuilder() {
return this.builder;
}
/**
* Return the {@link Creator} the builder should use.
* @return the {@code Creator}
*/
public Creator getCreator() {
return this.creator;
}
/**
* Return any env variable that should be passed to the builder.
* @return the builder env
*/
public Map<String, String> getEnv() {
return this.env;
}
/**
* Return if caches should be cleaned before packaging.
* @return if caches should be cleaned
*/
public boolean isCleanCache() {
return this.cleanCache;
}
/**
* Return if verbose logging output should be used.
* @return if verbose logging should be used
*/
public boolean isVerboseLogging() {
return this.verboseLogging;
}
/**
* Factory method to create a new {@link BuildRequest} from a JAR file.
* @param jarFile the source jar file
* @return a new build request instance
*/
public static BuildRequest forJarFile(File jarFile) {
assertJarFile(jarFile);
return forJarFile(ImageReference.forJarFile(jarFile).inTaggedForm(), jarFile);
}
/**
* Factory method to create a new {@link BuildRequest} from a JAR file.
* @param name the name of the image that should be created
* @param jarFile the source jar file
* @return a new build request instance
*/
public static BuildRequest forJarFile(ImageReference name, File jarFile) {
assertJarFile(jarFile);
return new BuildRequest(name, (owner) -> TarArchive.fromZip(jarFile, owner));
}
/**
* Factory method to create a new {@link BuildRequest} with specific content.
* @param name the name of the image that should be created
* @param applicationContent function to provide the application content
* @return a new build request instance
*/
public static BuildRequest of(ImageReference name, Function<Owner, TarArchive> applicationContent) {
return new BuildRequest(name, applicationContent);
}
private static void assertJarFile(File jarFile) {
Assert.notNull(jarFile, "JarFile must not be null");
Assert.isTrue(jarFile.exists(), "JarFile must exist");
Assert.isTrue(jarFile.isFile(), "JarFile must be a file");
}
}