|
| 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.image.json.BuildableManifestTemplate; |
| 20 | +import com.google.cloud.tools.jib.image.json.OCIManifestTemplate; |
| 21 | +import com.google.cloud.tools.jib.image.json.V22ManifestTemplate; |
| 22 | +import com.google.common.annotations.VisibleForTesting; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import javax.annotation.Nullable; |
| 26 | +import javax.inject.Inject; |
| 27 | +import org.gradle.api.Action; |
| 28 | +import org.gradle.api.Project; |
| 29 | +import org.gradle.api.model.ObjectFactory; |
| 30 | +import org.gradle.api.provider.ListProperty; |
| 31 | +import org.gradle.api.provider.Property; |
| 32 | + |
| 33 | +/** |
| 34 | + * Plugin extension for {@link JibPlugin}. |
| 35 | + * |
| 36 | + * <p>Example configuration: |
| 37 | + * |
| 38 | + * <pre>{@code |
| 39 | + * jib { |
| 40 | + * from { |
| 41 | + * image = ‘gcr.io/my-gcp-project/my-base-image’ |
| 42 | + * credHelper = ‘gcr’ |
| 43 | + * } |
| 44 | + * to { |
| 45 | + * image = ‘gcr.io/gcp-project/my-app:built-with-jib’ |
| 46 | + * credHelper = ‘ecr-login’ |
| 47 | + * } |
| 48 | + * jvmFlags = [‘-Xms512m’, ‘-Xdebug’] |
| 49 | + * mainClass = ‘com.mycompany.myproject.Main’ |
| 50 | + * reproducible = true |
| 51 | + * format = OCI |
| 52 | + * } |
| 53 | + * }</pre> |
| 54 | + */ |
| 55 | +public class JibExtension { |
| 56 | + |
| 57 | + // TODO: Consolidate with BuildImageMojo#ImageFormat. |
| 58 | + /** Enumeration of {@link BuildableManifestTemplate}s. */ |
| 59 | + @VisibleForTesting |
| 60 | + enum ImageFormat { |
| 61 | + Docker(V22ManifestTemplate.class), |
| 62 | + OCI(OCIManifestTemplate.class); |
| 63 | + |
| 64 | + private final Class<? extends BuildableManifestTemplate> manifestTemplateClass; |
| 65 | + |
| 66 | + ImageFormat(Class<? extends BuildableManifestTemplate> manifestTemplateClass) { |
| 67 | + this.manifestTemplateClass = manifestTemplateClass; |
| 68 | + } |
| 69 | + |
| 70 | + private Class<? extends BuildableManifestTemplate> getManifestTemplateClass() { |
| 71 | + return manifestTemplateClass; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Defines default configuration values. |
| 76 | + private static final String DEFAULT_FROM_IMAGE = "gcr.io/distroless/java"; |
| 77 | + private static final List<String> DEFAULT_JVM_FLAGS = Collections.emptyList(); |
| 78 | + private static final ImageFormat DEFAULT_FORMAT = ImageFormat.Docker; |
| 79 | + private static final boolean DEFAULT_REPRODUCIBLE = true; |
| 80 | + |
| 81 | + private final ImageConfiguration from; |
| 82 | + private final ImageConfiguration to; |
| 83 | + private final ListProperty<String> jvmFlags; |
| 84 | + private final Property<String> mainClass; |
| 85 | + private final Property<Boolean> reproducible; |
| 86 | + private final Property<ImageFormat> format; |
| 87 | + |
| 88 | + @Inject |
| 89 | + public JibExtension(Project project) { |
| 90 | + ObjectFactory objectFactory = project.getObjects(); |
| 91 | + |
| 92 | + from = objectFactory.newInstance(ImageConfiguration.class); |
| 93 | + to = objectFactory.newInstance(ImageConfiguration.class); |
| 94 | + |
| 95 | + jvmFlags = objectFactory.listProperty(String.class); |
| 96 | + mainClass = objectFactory.property(String.class); |
| 97 | + reproducible = objectFactory.property(Boolean.class); |
| 98 | + format = objectFactory.property(ImageFormat.class); |
| 99 | + |
| 100 | + // Sets defaults. |
| 101 | + from.setImage(DEFAULT_FROM_IMAGE); |
| 102 | + jvmFlags.set(DEFAULT_JVM_FLAGS); |
| 103 | + reproducible.set(DEFAULT_REPRODUCIBLE); |
| 104 | + format.set(DEFAULT_FORMAT); |
| 105 | + } |
| 106 | + |
| 107 | + public void from(Action<? super ImageConfiguration> action) { |
| 108 | + action.execute(from); |
| 109 | + } |
| 110 | + |
| 111 | + public void to(Action<? super ImageConfiguration> action) { |
| 112 | + action.execute(to); |
| 113 | + } |
| 114 | + |
| 115 | + public void setJvmFlags(List<String> jvmFlags) { |
| 116 | + this.jvmFlags.set(jvmFlags); |
| 117 | + } |
| 118 | + |
| 119 | + public void setMainClass(String mainClass) { |
| 120 | + this.mainClass.set(mainClass); |
| 121 | + } |
| 122 | + |
| 123 | + public void setReproducible(boolean isEnabled) { |
| 124 | + reproducible.set(isEnabled); |
| 125 | + } |
| 126 | + |
| 127 | + public void setFormat(ImageFormat format) { |
| 128 | + this.format.set(format); |
| 129 | + } |
| 130 | + |
| 131 | + ImageConfiguration getFrom() { |
| 132 | + return from; |
| 133 | + } |
| 134 | + |
| 135 | + ImageConfiguration getTo() { |
| 136 | + return to; |
| 137 | + } |
| 138 | + |
| 139 | + List<String> getJvmFlags() { |
| 140 | + return jvmFlags.get(); |
| 141 | + } |
| 142 | + |
| 143 | + @Nullable |
| 144 | + String getMainClass() { |
| 145 | + return mainClass.getOrNull(); |
| 146 | + } |
| 147 | + |
| 148 | + boolean getReproducible() { |
| 149 | + return reproducible.get(); |
| 150 | + } |
| 151 | + |
| 152 | + Class<? extends BuildableManifestTemplate> getFormat() { |
| 153 | + return format.get().getManifestTemplateClass(); |
| 154 | + } |
| 155 | +} |
0 commit comments