|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.gradle.tasks.bundling; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Supplier; |
| 24 | + |
| 25 | +import org.gradle.api.DefaultTask; |
| 26 | +import org.gradle.api.Project; |
| 27 | +import org.gradle.api.Task; |
| 28 | +import org.gradle.api.tasks.Input; |
| 29 | +import org.gradle.api.tasks.Optional; |
| 30 | +import org.gradle.api.tasks.TaskAction; |
| 31 | + |
| 32 | +import org.springframework.boot.buildpack.platform.build.BuildRequest; |
| 33 | +import org.springframework.boot.buildpack.platform.build.Builder; |
| 34 | +import org.springframework.boot.buildpack.platform.docker.DockerException; |
| 35 | +import org.springframework.boot.buildpack.platform.docker.type.ImageName; |
| 36 | +import org.springframework.boot.buildpack.platform.docker.type.ImageReference; |
| 37 | +import org.springframework.boot.buildpack.platform.io.ZipFileTarArchive; |
| 38 | +import org.springframework.util.StringUtils; |
| 39 | + |
| 40 | +/** |
| 41 | + * A {@link Task} for bundling an application into an OCI image using a |
| 42 | + * <a href="https://buildpacks.io">buildpack</a>. |
| 43 | + * |
| 44 | + * @author Andy Wilkinson |
| 45 | + * @since 2.3.0 |
| 46 | + */ |
| 47 | +public class BootBuildImage extends DefaultTask { |
| 48 | + |
| 49 | + private Supplier<File> jar; |
| 50 | + |
| 51 | + private String imageName; |
| 52 | + |
| 53 | + private String builder; |
| 54 | + |
| 55 | + private Map<String, String> environment = new HashMap<String, String>(); |
| 56 | + |
| 57 | + private boolean cleanCache; |
| 58 | + |
| 59 | + private boolean verboseLogging; |
| 60 | + |
| 61 | + /** |
| 62 | + * Configures this task to create an image from the given {@code bootJar} task. This |
| 63 | + * task is also configured to depend upon the given task. |
| 64 | + * @param bootJar the fat jar from which the image should be created. |
| 65 | + */ |
| 66 | + public void from(BootJar bootJar) { |
| 67 | + dependsOn(bootJar); |
| 68 | + this.jar = () -> bootJar.getArchiveFile().get().getAsFile(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Configures this task to create an image from the given jar file. |
| 73 | + * @param jar the jar from which the image should be created. |
| 74 | + */ |
| 75 | + public void from(File jar) { |
| 76 | + this.jar = () -> jar; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the name of the image that will be built. When {@code null}, the name will |
| 81 | + * be derived from the {@link Project Project's} {@link Project#getName() name} and |
| 82 | + * {@link Project#getVersion version}. |
| 83 | + * @return name of the image |
| 84 | + */ |
| 85 | + @Input |
| 86 | + @Optional |
| 87 | + public String getImageName() { |
| 88 | + return this.imageName; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sets the name of the image that will be built. |
| 93 | + * @param imageName name of the image |
| 94 | + */ |
| 95 | + public void setImageName(String imageName) { |
| 96 | + this.imageName = imageName; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the builder that will be used to build the image. When {@code null}, the |
| 101 | + * default builder will be used. |
| 102 | + * @return the builder |
| 103 | + */ |
| 104 | + @Input |
| 105 | + @Optional |
| 106 | + public String getBuilder() { |
| 107 | + return this.builder; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets the builder that will be used to build the image. |
| 112 | + * @param builder the builder |
| 113 | + */ |
| 114 | + public void setBuilder(String builder) { |
| 115 | + this.builder = builder; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns the environment that will be used when building the image. |
| 120 | + * @return the environment |
| 121 | + */ |
| 122 | + @Input |
| 123 | + public Map<String, String> getEnvironment() { |
| 124 | + return this.environment; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets the environment that will be used when building the image. |
| 129 | + * @param environment the environment |
| 130 | + */ |
| 131 | + public void setEnvironment(Map<String, String> environment) { |
| 132 | + this.environment = environment; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Add an entry to the environment that will be used when building the image. |
| 137 | + * @param name the name of the entry |
| 138 | + * @param value the value of the entry |
| 139 | + */ |
| 140 | + public void environment(String name, String value) { |
| 141 | + this.environment.put(name, value); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Adds entries to the environment that will be used when building the image. |
| 146 | + * @param entries the entries to add to the environment |
| 147 | + */ |
| 148 | + public void environment(Map<String, String> entries) { |
| 149 | + this.environment.putAll(entries); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns whether caches should be cleaned before packaging. |
| 154 | + * @return whether caches should be cleaned |
| 155 | + */ |
| 156 | + @Input |
| 157 | + public boolean isCleanCache() { |
| 158 | + return this.cleanCache; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Sets whether caches should be cleaned before packaging. |
| 163 | + * @param cleanCache {@code true} to clean the cache, otherwise {@code false}. |
| 164 | + */ |
| 165 | + public void setCleanCache(boolean cleanCache) { |
| 166 | + this.cleanCache = cleanCache; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Whether verbose logging should be enabled while building the image. |
| 171 | + * @return whether verbose logging should be enabled |
| 172 | + */ |
| 173 | + @Input |
| 174 | + public boolean isVerboseLogging() { |
| 175 | + return this.verboseLogging; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Sets whether verbose logging should be enabled while building the image. |
| 180 | + * @param verboseLogging {@code true} to enable verbose logging, otherwise |
| 181 | + * {@code false}. |
| 182 | + */ |
| 183 | + public void setVerboseLogging(boolean verboseLogging) { |
| 184 | + this.verboseLogging = verboseLogging; |
| 185 | + } |
| 186 | + |
| 187 | + @TaskAction |
| 188 | + void buildImage() throws DockerException, IOException { |
| 189 | + Builder builder = new Builder(); |
| 190 | + BuildRequest request = createRequest(); |
| 191 | + builder.build(request); |
| 192 | + } |
| 193 | + |
| 194 | + BuildRequest createRequest() { |
| 195 | + BuildRequest request = customize( |
| 196 | + BuildRequest.of(determineImageReference(), (owner) -> new ZipFileTarArchive(this.jar.get(), owner))); |
| 197 | + return request; |
| 198 | + } |
| 199 | + |
| 200 | + private ImageReference determineImageReference() { |
| 201 | + if (StringUtils.hasText(this.imageName)) { |
| 202 | + return ImageReference.of(this.imageName); |
| 203 | + } |
| 204 | + ImageName imageName = ImageName.of(getProject().getName()); |
| 205 | + String version = getProject().getVersion().toString(); |
| 206 | + if ("unspecified".equals(version)) { |
| 207 | + return ImageReference.of(imageName); |
| 208 | + } |
| 209 | + return ImageReference.of(imageName, version); |
| 210 | + } |
| 211 | + |
| 212 | + private BuildRequest customize(BuildRequest request) { |
| 213 | + if (StringUtils.hasText(this.builder)) { |
| 214 | + request = request.withBuilder(ImageReference.of(this.builder)); |
| 215 | + } |
| 216 | + if (this.environment != null && !this.environment.isEmpty()) { |
| 217 | + request = request.withEnv(this.environment); |
| 218 | + } |
| 219 | + request = request.withCleanCache(this.cleanCache); |
| 220 | + request = request.withVerboseLogging(this.verboseLogging); |
| 221 | + return request; |
| 222 | + } |
| 223 | + |
| 224 | +} |
0 commit comments