|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.jib.gradle; |
| 18 | + |
| 19 | +import com.google.cloud.tools.jib.builder.BuildConfiguration; |
| 20 | +import com.google.cloud.tools.jib.docker.DockerContextGenerator; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import com.google.common.io.InsecureRecursiveDeleteException; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.List; |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import org.gradle.api.DefaultTask; |
| 28 | +import org.gradle.api.GradleException; |
| 29 | +import org.gradle.api.tasks.Input; |
| 30 | +import org.gradle.api.tasks.OutputDirectory; |
| 31 | +import org.gradle.api.tasks.TaskAction; |
| 32 | +import org.gradle.api.tasks.options.Option; |
| 33 | + |
| 34 | +public class DockerContextTask extends DefaultTask { |
| 35 | + |
| 36 | + @Nullable private String targetDir; |
| 37 | + @Nullable private String baseImage; |
| 38 | + @Nullable private List<String> jvmFlags; |
| 39 | + @Nullable private String mainClass; |
| 40 | + |
| 41 | + @Input |
| 42 | + @Nullable |
| 43 | + public String getBaseImage() { |
| 44 | + return baseImage; |
| 45 | + } |
| 46 | + |
| 47 | + @Input |
| 48 | + @Nullable |
| 49 | + public List<String> getJvmFlags() { |
| 50 | + return jvmFlags; |
| 51 | + } |
| 52 | + |
| 53 | + @Input |
| 54 | + @Nullable |
| 55 | + public String getMainClass() { |
| 56 | + return mainClass; |
| 57 | + } |
| 58 | + |
| 59 | + /** The output directory for the Docker context is by default {@code build/jib-dockercontext}. */ |
| 60 | + @OutputDirectory |
| 61 | + public String getTargetDir() { |
| 62 | + if (targetDir == null) { |
| 63 | + return getProject().getBuildDir().toPath().resolve("jib-dockercontext").toString(); |
| 64 | + } |
| 65 | + return targetDir; |
| 66 | + } |
| 67 | + |
| 68 | + /** The output directory can be overriden with the {@code --jib.dockerDir} command line option. */ |
| 69 | + @Option(option = "jib.dockerDir", description = "Directory to output the Docker context to") |
| 70 | + public void setTargetDir(String targetDir) { |
| 71 | + this.targetDir = targetDir; |
| 72 | + } |
| 73 | + |
| 74 | + @TaskAction |
| 75 | + public void generateDockerContext() { |
| 76 | + // Asserts required parameters are not null. |
| 77 | + Preconditions.checkNotNull(baseImage); |
| 78 | + Preconditions.checkNotNull(jvmFlags); |
| 79 | + Preconditions.checkNotNull(mainClass); |
| 80 | + |
| 81 | + // TODO: Refactor with BuildImageTask. |
| 82 | + ProjectProperties projectProperties = new ProjectProperties(getProject(), getLogger()); |
| 83 | + |
| 84 | + String mainClass = this.mainClass; |
| 85 | + if (mainClass == null) { |
| 86 | + mainClass = projectProperties.getMainClassFromJarTask(); |
| 87 | + if (mainClass == null) { |
| 88 | + throw new GradleException("Could not find main class specified in a 'jar' task"); |
| 89 | + } |
| 90 | + } |
| 91 | + if (!BuildConfiguration.isValidJavaClass(mainClass)) { |
| 92 | + getLogger().warn("'mainClass' is not a valid Java class : " + mainClass); |
| 93 | + } |
| 94 | + |
| 95 | + String targetDir = getTargetDir(); |
| 96 | + |
| 97 | + try { |
| 98 | + new DockerContextGenerator(projectProperties.getSourceFilesConfiguration()) |
| 99 | + .setBaseImage(baseImage) |
| 100 | + .setJvmFlags(jvmFlags) |
| 101 | + .setMainClass(mainClass) |
| 102 | + .generate(Paths.get(targetDir)); |
| 103 | + |
| 104 | + getLogger().info("Created Docker context at " + targetDir); |
| 105 | + |
| 106 | + } catch (InsecureRecursiveDeleteException ex) { |
| 107 | + throwMojoExecutionExceptionWithHelpMessage( |
| 108 | + ex, |
| 109 | + "cannot clear directory '" |
| 110 | + + targetDir |
| 111 | + + "' safely - clear it manually before creating the Docker context"); |
| 112 | + |
| 113 | + } catch (IOException ex) { |
| 114 | + throwMojoExecutionExceptionWithHelpMessage( |
| 115 | + ex, "check if the command-line option `--jib.dockerDir` is set correctly"); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Applies the configuration from {@code jibExtension}. This must be called before {@link |
| 121 | + * #generateDockerContext}. |
| 122 | + */ |
| 123 | + void applyExtension(JibExtension jibExtension) { |
| 124 | + baseImage = jibExtension.getFrom().getImage(); |
| 125 | + jvmFlags = jibExtension.getJvmFlags(); |
| 126 | + mainClass = jibExtension.getMainClass(); |
| 127 | + } |
| 128 | + |
| 129 | + private <T extends Throwable> void throwMojoExecutionExceptionWithHelpMessage( |
| 130 | + T ex, String suggestion) { |
| 131 | + StringBuilder message = new StringBuilder("Export Docker context failed"); |
| 132 | + if (suggestion != null) { |
| 133 | + message.append(", perhaps you should "); |
| 134 | + message.append(suggestion); |
| 135 | + } |
| 136 | + throw new GradleException(message.toString(), ex); |
| 137 | + } |
| 138 | +} |
0 commit comments