From b468528f79a253b7b9955214826eb3c9f321b696 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Fri, 6 Jul 2018 17:30:57 -0400 Subject: [PATCH 01/19] Removes SourceFilesConfiguration in favor of list of LayerConfiguration in BuildConfiguration. --- .../builder/BuildStepsIntegrationTest.java | 52 +++++++++--- .../tools/jib/builder/BuildConfiguration.java | 30 ++++++- .../cloud/tools/jib/builder/BuildSteps.java | 56 ++++++------- .../tools/jib/builder/EntrypointBuilder.java | 15 ++-- .../jib/builder/SourceFilesConfiguration.java | 39 +-------- .../BuildAndCacheApplicationLayerStep.java | 39 ++++----- .../tools/jib/builder/steps/StepsRunner.java | 13 +-- .../jib/configuration/LayerConfiguration.java | 23 +++++- .../jib/docker/DockerContextGenerator.java | 34 +++++--- .../tools/jib/frontend/BuildStepsRunner.java | 33 +++++--- .../tools/jib/frontend/MainClassFinder.java | 6 +- .../tools/jib/frontend/ProjectProperties.java | 4 +- .../jib/builder/EntrypointBuilderTest.java | 17 ++-- .../builder/TestSourceFilesConfiguration.java | 81 ------------------- ...BuildAndCacheApplicationLayerStepTest.java | 64 ++++++++++++--- .../docker/DockerContextGeneratorTest.java | 38 +++------ .../jib/frontend/BuildStepsRunnerTest.java | 13 +-- .../jib/frontend/MainClassFinderTest.java | 12 ++- .../tools/jib/gradle/BuildImageTask.java | 6 +- ...on.java => GradleLayerConfigurations.java} | 69 ++++++---------- .../jib/gradle/GradleProjectProperties.java | 15 ++-- 21 files changed, 312 insertions(+), 347 deletions(-) delete mode 100644 jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java rename jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/{GradleSourceFilesConfiguration.java => GradleLayerConfigurations.java} (66%) diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java index 979e7fa32c..4c0d215512 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java @@ -21,36 +21,66 @@ import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; import com.google.cloud.tools.jib.cache.Caches; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.LocalRegistry; +import com.google.common.collect.ImmutableList; +import com.google.common.io.Resources; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.concurrent.ExecutionException; +import java.util.stream.Stream; import org.hamcrest.CoreMatchers; -import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; +import org.junit.*; import org.junit.rules.TemporaryFolder; /** Integration tests for {@link BuildSteps}. */ public class BuildStepsIntegrationTest { + private static final String EXTRACTION_PATH = "/some/extraction/path/"; + + /** Lists the files in the {@code resourcePath} resources directory. */ + private static ImmutableList getFilesList(String resourcePath) + throws URISyntaxException, IOException { + try (Stream fileStream = + Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { + return fileStream.collect(ImmutableList.toImmutableList()); + } + } + @ClassRule public static LocalRegistry localRegistry = new LocalRegistry(5000); private static final TestBuildLogger logger = new TestBuildLogger(); @Rule public TemporaryFolder temporaryCacheDirectory = new TemporaryFolder(); + private ImmutableList fakeLayerConfigurations; + + @Before + public void setUp() throws IOException, URISyntaxException { + fakeLayerConfigurations = + ImmutableList.of( + LayerConfiguration.builder() + .addEntry(getFilesList("application/dependencies"), EXTRACTION_PATH + "libs/") + .build(), + LayerConfiguration.builder() + .addEntry(getFilesList("application/resources"), EXTRACTION_PATH + "resources/") + .build(), + LayerConfiguration.builder() + .addEntry(getFilesList("application/classes"), EXTRACTION_PATH + "classes/") + .build()); + } + @Test public void testSteps_forBuildToDockerRegistry() - throws IOException, URISyntaxException, InterruptedException, CacheMetadataCorruptedException, - ExecutionException, CacheDirectoryNotOwnedException, CacheDirectoryCreationException { - SourceFilesConfiguration sourceFilesConfiguration = new TestSourceFilesConfiguration(); + throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException, + CacheDirectoryNotOwnedException, CacheDirectoryCreationException { BuildConfiguration buildConfiguration = BuildConfiguration.builder(logger) .setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) @@ -61,13 +91,13 @@ public void testSteps_forBuildToDockerRegistry() ExposedPortsParser.parse( Arrays.asList("1000", "2000-2002/tcp", "3000/udp"), logger)) .setAllowHttp(true) + .setLayerConfigurations(fakeLayerConfigurations) .build(); Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath(); BuildSteps buildImageSteps = BuildSteps.forBuildToDockerRegistry( buildConfiguration, - sourceFilesConfiguration, new Caches.Initializer(cacheDirectory, false, cacheDirectory, false)); long lastTime = System.nanoTime(); @@ -94,9 +124,8 @@ public void testSteps_forBuildToDockerRegistry() @Test public void testSteps_forBuildToDockerDaemon() - throws IOException, URISyntaxException, InterruptedException, CacheMetadataCorruptedException, - ExecutionException, CacheDirectoryNotOwnedException, CacheDirectoryCreationException { - SourceFilesConfiguration sourceFilesConfiguration = new TestSourceFilesConfiguration(); + throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException, + CacheDirectoryNotOwnedException, CacheDirectoryCreationException { BuildConfiguration buildConfiguration = BuildConfiguration.builder(logger) .setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) @@ -112,7 +141,6 @@ public void testSteps_forBuildToDockerDaemon() BuildSteps buildDockerSteps = BuildSteps.forBuildToDockerDaemon( buildConfiguration, - sourceFilesConfiguration, new Caches.Initializer(cacheDirectory, false, cacheDirectory, false)); buildDockerSteps.run(); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index 6e279f5e23..fc7f996711 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -17,6 +17,7 @@ package com.google.cloud.tools.jib.builder; import com.google.cloud.tools.jib.configuration.CacheConfiguration; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate; import com.google.cloud.tools.jib.image.json.V22ManifestTemplate; @@ -52,6 +53,7 @@ public static class Builder { @Nullable private CacheConfiguration applicationLayersCacheConfiguration; @Nullable private CacheConfiguration baseImageLayersCacheConfiguration; private boolean allowHttp = false; + private ImmutableList layerConfigurations = ImmutableList.of(); private BuildLogger buildLogger; @@ -169,6 +171,17 @@ public Builder setAllowHttp(boolean allowHttp) { return this; } + /** + * Sets the layers to build. + * + * @param layerConfigurations the configurations for the layers + * @return this + */ + public Builder setLayerConfigurations(List layerConfigurations) { + this.layerConfigurations = ImmutableList.copyOf(layerConfigurations); + return this; + } + /** @return the corresponding build configuration */ public BuildConfiguration build() { // Validates the parameters. @@ -210,7 +223,8 @@ public BuildConfiguration build() { targetFormat, applicationLayersCacheConfiguration, baseImageLayersCacheConfiguration, - allowHttp); + allowHttp, + layerConfigurations); case 1: throw new IllegalStateException(errorMessages.get(0)); @@ -269,6 +283,7 @@ public static Builder builder(BuildLogger buildLogger) { @Nullable private final CacheConfiguration applicationLayersCacheConfiguration; @Nullable private final CacheConfiguration baseImageLayersCacheConfiguration; private final boolean allowHttp; + private final ImmutableList layerConfigurations; /** Instantiate with {@link Builder#build}. */ private BuildConfiguration( @@ -287,7 +302,8 @@ private BuildConfiguration( Class targetFormat, @Nullable CacheConfiguration applicationLayersCacheConfiguration, @Nullable CacheConfiguration baseImageLayersCacheConfiguration, - boolean allowHttp) { + boolean allowHttp, + ImmutableList layerConfigurations) { this.buildLogger = buildLogger; this.baseImageReference = baseImageReference; this.baseImageCredentialHelperName = baseImageCredentialHelperName; @@ -304,6 +320,7 @@ private BuildConfiguration( this.applicationLayersCacheConfiguration = applicationLayersCacheConfiguration; this.baseImageLayersCacheConfiguration = baseImageLayersCacheConfiguration; this.allowHttp = allowHttp; + this.layerConfigurations = layerConfigurations; } public BuildLogger getBuildLogger() { @@ -414,4 +431,13 @@ public CacheConfiguration getBaseImageLayersCacheConfiguration() { public boolean getAllowHttp() { return allowHttp; } + + /** + * Gets the configurations for building the container. + * + * @return the list of layer configurations + */ + public ImmutableList getLayerConfigurations() { + return layerConfigurations; + } } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java index 57f9708eae..b04bd83914 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java @@ -55,18 +55,14 @@ private interface StepsRunnerConsumer { * All the steps to build an image to a Docker registry. * * @param buildConfiguration the configuration parameters for the build - * @param sourceFilesConfiguration the source/destination file configuration for the image * @param cachesInitializer the {@link Caches.Initializer} used to setup the cache * @return a new {@link BuildSteps} for building to a registry */ public static BuildSteps forBuildToDockerRegistry( - BuildConfiguration buildConfiguration, - SourceFilesConfiguration sourceFilesConfiguration, - Caches.Initializer cachesInitializer) { + BuildConfiguration buildConfiguration, Caches.Initializer cachesInitializer) { return new BuildSteps( DESCRIPTION_FOR_DOCKER_REGISTRY, buildConfiguration, - sourceFilesConfiguration, cachesInitializer, String.format( STARTUP_MESSAGE_FORMAT_FOR_DOCKER_REGISTRY, @@ -82,7 +78,7 @@ public static BuildSteps forBuildToDockerRegistry( .runPullAndCacheBaseImageLayersStep() .runPushBaseImageLayersStep() .runBuildAndCacheApplicationLayerSteps() - .runBuildImageStep(getEntrypoint(buildConfiguration, sourceFilesConfiguration)) + .runBuildImageStep(getEntrypoint(buildConfiguration)) .runPushContainerConfigurationStep() .runPushApplicationLayersStep() .runFinalizingPushStep() @@ -94,18 +90,14 @@ public static BuildSteps forBuildToDockerRegistry( * All the steps to build to Docker daemon * * @param buildConfiguration the configuration parameters for the build - * @param sourceFilesConfiguration the source/destination file configuration for the image * @param cachesInitializer the {@link Caches.Initializer} used to setup the cache * @return a new {@link BuildSteps} for building to a Docker daemon */ public static BuildSteps forBuildToDockerDaemon( - BuildConfiguration buildConfiguration, - SourceFilesConfiguration sourceFilesConfiguration, - Caches.Initializer cachesInitializer) { + BuildConfiguration buildConfiguration, Caches.Initializer cachesInitializer) { return new BuildSteps( DESCRIPTION_FOR_DOCKER_DAEMON, buildConfiguration, - sourceFilesConfiguration, cachesInitializer, String.format( STARTUP_MESSAGE_FORMAT_FOR_DOCKER_DAEMON, buildConfiguration.getTargetImageReference()), @@ -116,24 +108,40 @@ public static BuildSteps forBuildToDockerDaemon( .runPullBaseImageStep() .runPullAndCacheBaseImageLayersStep() .runBuildAndCacheApplicationLayerSteps() - .runBuildImageStep(getEntrypoint(buildConfiguration, sourceFilesConfiguration)) + .runBuildImageStep(getEntrypoint(buildConfiguration)) .runFinalizingBuildStep() .runBuildTarballAndLoadDockerStep() .waitOnBuildTarballAndLoadDockerStep()); } /** Creates the container entrypoint for a given configuration. */ - private static ImmutableList getEntrypoint( - BuildConfiguration buildConfiguration, SourceFilesConfiguration sourceFilesConfiguration) { + private static ImmutableList getEntrypoint(BuildConfiguration buildConfiguration) { + // TODO: Don't use indexes. return EntrypointBuilder.makeEntrypoint( - sourceFilesConfiguration, + buildConfiguration + .getLayerConfigurations() + .get(0) + .getLayerEntries() + .get(0) + .getExtractionPath(), + buildConfiguration + .getLayerConfigurations() + .get(1) + .getLayerEntries() + .get(0) + .getExtractionPath(), + buildConfiguration + .getLayerConfigurations() + .get(2) + .getLayerEntries() + .get(0) + .getExtractionPath(), buildConfiguration.getJvmFlags(), buildConfiguration.getMainClass()); } private final String description; private final BuildConfiguration buildConfiguration; - private final SourceFilesConfiguration sourceFilesConfiguration; private final Caches.Initializer cachesInitializer; private final String startupMessage; private final String successMessage; @@ -148,14 +156,12 @@ private static ImmutableList getEntrypoint( private BuildSteps( String description, BuildConfiguration buildConfiguration, - SourceFilesConfiguration sourceFilesConfiguration, Caches.Initializer cachesInitializer, String startupMessage, String successMessage, StepsRunnerConsumer stepsRunnerConsumer) { this.description = description; this.buildConfiguration = buildConfiguration; - this.sourceFilesConfiguration = sourceFilesConfiguration; this.cachesInitializer = cachesInitializer; this.startupMessage = startupMessage; this.successMessage = successMessage; @@ -166,10 +172,6 @@ public BuildConfiguration getBuildConfiguration() { return buildConfiguration; } - public SourceFilesConfiguration getSourceFilesConfiguration() { - return sourceFilesConfiguration; - } - public String getStartupMessage() { return startupMessage; } @@ -189,11 +191,7 @@ public void run() Cache applicationLayersCache = caches.getApplicationCache(); StepsRunner stepsRunner = - new StepsRunner( - buildConfiguration, - sourceFilesConfiguration, - baseImageLayersCache, - applicationLayersCache); + new StepsRunner(buildConfiguration, baseImageLayersCache, applicationLayersCache); stepsRunnerConsumer.accept(stepsRunner); // Writes the cached layers to the cache metadata. @@ -206,8 +204,6 @@ public void run() buildConfiguration.getBuildLogger().lifecycle(""); buildConfiguration .getBuildLogger() - .lifecycle( - "Container entrypoint set to " - + getEntrypoint(buildConfiguration, sourceFilesConfiguration)); + .lifecycle("Container entrypoint set to " + getEntrypoint(buildConfiguration)); } } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java index 2c1c06ffea..c8e00d5304 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java @@ -27,18 +27,23 @@ public class EntrypointBuilder { * *

The entrypoint is {@code java [jvm flags] -cp [classpaths] [main class]}. * - * @param sourceFilesConfiguration configuration defining where files are copied onto the image + * @param dependenciesExtractionPath extraction path of dependencies + * @param resourcesExtractionPath extraction path of resources + * @param classesExtractionPath extraction path of classes * @param jvmFlags the JVM flags to start the container with * @param mainClass the name of the main class to run on startup * @return a list of the entrypoint tokens */ public static ImmutableList makeEntrypoint( - SourceFilesConfiguration sourceFilesConfiguration, List jvmFlags, String mainClass) { + String dependenciesExtractionPath, + String resourcesExtractionPath, + String classesExtractionPath, + List jvmFlags, + String mainClass) { + // TODO: Entrypoint should be built and added to BuildConfiguration rather than built here. ImmutableList classPaths = ImmutableList.of( - sourceFilesConfiguration.getDependenciesPathOnImage() + "*", - sourceFilesConfiguration.getResourcesPathOnImage(), - sourceFilesConfiguration.getClassesPathOnImage()); + dependenciesExtractionPath + "*", resourcesExtractionPath, classesExtractionPath); String classPathsString = String.join(":", classPaths); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java index 15de722be6..77a4d78904 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java @@ -16,51 +16,14 @@ package com.google.cloud.tools.jib.builder; -import com.google.common.collect.ImmutableList; -import java.nio.file.Path; - /** * Immutable configuration that defines where the source files for each of the application layers * are. */ +// TODO: Move these constants to another place. public interface SourceFilesConfiguration { String DEFAULT_DEPENDENCIES_PATH_ON_IMAGE = "/app/libs/"; String DEFAULT_RESOURCES_PATH_ON_IMAGE = "/app/resources/"; String DEFAULT_CLASSES_PATH_ON_IMAGE = "/app/classes/"; - - /** - * @return the source files for the dependencies layer. These files should be in a deterministic - * order. - */ - ImmutableList getDependenciesFiles(); - - /** - * @return the source files for the resources layer. These files should be in a deterministic - * order. - */ - ImmutableList getResourcesFiles(); - - /** - * @return the source files for the classes layer. These files should be in a deterministic order. - */ - ImmutableList getClassesFiles(); - - /** - * @return the Unix-style path where the dependencies source files are placed in the container - * filesystem. Must end with slash. - */ - String getDependenciesPathOnImage(); - - /** - * @return the Unix-style path where the resources source files are placed in the container - * filesystem. Must end with slash. - */ - String getResourcesPathOnImage(); - - /** - * @return the Unix-style path where the classes source files are placed in the container - * filesystem. Must end with slash. - */ - String getClassesPathOnImage(); } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java index ad89a7d66c..da0724fe8e 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java @@ -19,12 +19,12 @@ import com.google.cloud.tools.jib.Timer; import com.google.cloud.tools.jib.async.AsyncStep; import com.google.cloud.tools.jib.builder.BuildConfiguration; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.cache.Cache; import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; import com.google.cloud.tools.jib.cache.CacheReader; import com.google.cloud.tools.jib.cache.CacheWriter; import com.google.cloud.tools.jib.cache.CachedLayerWithMetadata; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.image.ReproducibleLayerBuilder; import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.ListenableFuture; @@ -46,31 +46,22 @@ class BuildAndCacheApplicationLayerStep static ImmutableList makeList( ListeningExecutorService listeningExecutorService, BuildConfiguration buildConfiguration, - SourceFilesConfiguration sourceFilesConfiguration, Cache cache) { try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) { - return ImmutableList.of( - new BuildAndCacheApplicationLayerStep( - "dependencies", - listeningExecutorService, - buildConfiguration, - sourceFilesConfiguration.getDependenciesFiles(), - sourceFilesConfiguration.getDependenciesPathOnImage(), - cache), - new BuildAndCacheApplicationLayerStep( - "resources", - listeningExecutorService, - buildConfiguration, - sourceFilesConfiguration.getResourcesFiles(), - sourceFilesConfiguration.getResourcesPathOnImage(), - cache), - new BuildAndCacheApplicationLayerStep( - "classes", - listeningExecutorService, - buildConfiguration, - sourceFilesConfiguration.getClassesFiles(), - sourceFilesConfiguration.getClassesPathOnImage(), - cache)); + ImmutableList.Builder buildAndCacheApplicationLayerSteps = + ImmutableList.builderWithExpectedSize(buildConfiguration.getLayerConfigurations().size()); + for (LayerConfiguration layerConfiguration : buildConfiguration.getLayerConfigurations()) { + buildAndCacheApplicationLayerSteps.add( + new BuildAndCacheApplicationLayerStep( + layerConfiguration.getLabel(), + listeningExecutorService, + buildConfiguration, + // TODO: Don't use 0 - use all the layer entries. + layerConfiguration.getLayerEntries().get(0).getSourceFiles(), + layerConfiguration.getLayerEntries().get(0).getExtractionPath(), + cache)); + } + return buildAndCacheApplicationLayerSteps.build(); } } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java index d3b93d5115..d400a8825c 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java @@ -19,7 +19,6 @@ import com.google.cloud.tools.jib.async.AsyncSteps; import com.google.cloud.tools.jib.async.NonBlockingSteps; import com.google.cloud.tools.jib.builder.BuildConfiguration; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.cache.Cache; import com.google.cloud.tools.jib.cache.CachedLayer; import com.google.cloud.tools.jib.cache.CachedLayerWithMetadata; @@ -46,7 +45,6 @@ public class StepsRunner { private final ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); private final BuildConfiguration buildConfiguration; - private final SourceFilesConfiguration sourceFilesConfiguration; private final Cache baseLayersCache; private final Cache applicationLayersCache; @@ -66,12 +64,8 @@ public class StepsRunner { @Nullable private BuildTarballAndLoadDockerStep buildTarballAndLoadDockerStep; public StepsRunner( - BuildConfiguration buildConfiguration, - SourceFilesConfiguration sourceFilesConfiguration, - Cache baseLayersCache, - Cache applicationLayersCache) { + BuildConfiguration buildConfiguration, Cache baseLayersCache, Cache applicationLayersCache) { this.buildConfiguration = buildConfiguration; - this.sourceFilesConfiguration = sourceFilesConfiguration; this.baseLayersCache = baseLayersCache; this.applicationLayersCache = applicationLayersCache; } @@ -120,10 +114,7 @@ public StepsRunner runPushBaseImageLayersStep() { public StepsRunner runBuildAndCacheApplicationLayerSteps() { buildAndCacheApplicationLayerSteps = BuildAndCacheApplicationLayerStep.makeList( - listeningExecutorService, - buildConfiguration, - sourceFilesConfiguration, - applicationLayersCache); + listeningExecutorService, buildConfiguration, applicationLayersCache); return this; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java index e420381c39..3a247cc598 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java @@ -29,9 +29,21 @@ public class LayerConfiguration { public static class Builder { private final ImmutableList.Builder layerEntries = ImmutableList.builder(); + private String label = ""; private Builder() {} + /** + * Sets an optional label for this layer. + * + * @param label the label + * @return this + */ + public Builder setLabel(String label) { + this.label = label; + return this; + } + /** * Adds an entry to the layer. * @@ -56,7 +68,7 @@ public Builder addEntry(List sourceFiles, String destinationOnImage) { * @return the built {@link LayerConfiguration} */ public LayerConfiguration build() { - return new LayerConfiguration(layerEntries.build()); + return new LayerConfiguration(label, layerEntries.build()); } } @@ -65,16 +77,23 @@ public static Builder builder() { } private final ImmutableList layerEntries; + private String label; /** * Constructs a new layer configuration. * + * @param label an optional label for the layer * @param layerEntries the list of {@link LayerEntry}s */ - private LayerConfiguration(ImmutableList layerEntries) { + private LayerConfiguration(String label, ImmutableList layerEntries) { + this.label = label; this.layerEntries = layerEntries; } + public String getLabel() { + return label; + } + public ImmutableList getLayerEntries() { return layerEntries; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java index 91bbafd809..08101a9863 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java @@ -19,8 +19,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.cloud.tools.jib.builder.EntrypointBuilder; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.filesystem.FileOperations; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.io.MoreFiles; @@ -46,7 +46,9 @@ */ public class DockerContextGenerator { - private final SourceFilesConfiguration sourceFilesConfiguration; + private final LayerEntry dependenciesLayerEntry; + private final LayerEntry resourcesLayerEntry; + private final LayerEntry classesLayerEntry; @Nullable private String baseImage; private List jvmFlags = Collections.emptyList(); @@ -54,8 +56,13 @@ public class DockerContextGenerator { private List javaArguments = Collections.emptyList(); private List exposedPorts = Collections.emptyList(); - public DockerContextGenerator(SourceFilesConfiguration sourceFilesConfiguration) { - this.sourceFilesConfiguration = sourceFilesConfiguration; + public DockerContextGenerator( + LayerEntry dependenciesLayerEntry, + LayerEntry resourcesLayerEntry, + LayerEntry classesLayerEntry) { + this.dependenciesLayerEntry = dependenciesLayerEntry; + this.resourcesLayerEntry = resourcesLayerEntry; + this.classesLayerEntry = classesLayerEntry; } /** @@ -143,9 +150,9 @@ public void generate(Path targetDirectory) throws IOException { Files.createDirectory(classesDir); // Copies dependencies. - FileOperations.copy(sourceFilesConfiguration.getDependenciesFiles(), dependenciesDir); - FileOperations.copy(sourceFilesConfiguration.getResourcesFiles(), resourcesDIr); - FileOperations.copy(sourceFilesConfiguration.getClassesFiles(), classesDir); + FileOperations.copy(dependenciesLayerEntry.getSourceFiles(), dependenciesDir); + FileOperations.copy(resourcesLayerEntry.getSourceFiles(), resourcesDIr); + FileOperations.copy(classesLayerEntry.getSourceFiles(), classesDir); // Creates the Dockerfile. Files.write( @@ -178,11 +185,11 @@ String makeDockerfile() throws JsonProcessingException { .append("FROM ") .append(Preconditions.checkNotNull(baseImage)) .append("\n\nCOPY libs ") - .append(sourceFilesConfiguration.getDependenciesPathOnImage()) + .append(dependenciesLayerEntry.getExtractionPath()) .append("\nCOPY resources ") - .append(sourceFilesConfiguration.getResourcesPathOnImage()) + .append(resourcesLayerEntry.getExtractionPath()) .append("\nCOPY classes ") - .append(sourceFilesConfiguration.getClassesPathOnImage()) + .append(classesLayerEntry.getExtractionPath()) .append("\n"); for (String port : exposedPorts) { dockerfile.append("\nEXPOSE ").append(port); @@ -191,7 +198,12 @@ String makeDockerfile() throws JsonProcessingException { .append("\nENTRYPOINT ") .append( objectMapper.writeValueAsString( - EntrypointBuilder.makeEntrypoint(sourceFilesConfiguration, jvmFlags, mainClass))) + EntrypointBuilder.makeEntrypoint( + dependenciesLayerEntry.getExtractionPath(), + resourcesLayerEntry.getExtractionPath(), + classesLayerEntry.getExtractionPath(), + jvmFlags, + mainClass))) .append("\nCMD ") .append(objectMapper.writeValueAsString(javaArguments)); return dockerfile.toString(); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java index dc1bb950cd..495b84a702 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java @@ -44,16 +44,14 @@ public class BuildStepsRunner { * Creates a runner to build an image. Creates a directory for the cache, if needed. * * @param buildConfiguration the configuration parameters for the build - * @param sourceFilesConfiguration the source/destination file configuration for the image * @return a {@link BuildStepsRunner} for building to a registry * @throws CacheDirectoryCreationException if the {@code cacheDirectory} could not be created */ - public static BuildStepsRunner forBuildImage( - BuildConfiguration buildConfiguration, SourceFilesConfiguration sourceFilesConfiguration) + public static BuildStepsRunner forBuildImage(BuildConfiguration buildConfiguration) throws CacheDirectoryCreationException { return new BuildStepsRunner( BuildSteps.forBuildToDockerRegistry( - buildConfiguration, sourceFilesConfiguration, getCacheInitializer(buildConfiguration))); + buildConfiguration, getCacheInitializer(buildConfiguration))); } /** @@ -69,7 +67,7 @@ public static BuildStepsRunner forBuildToDockerDaemon( throws CacheDirectoryCreationException { return new BuildStepsRunner( BuildSteps.forBuildToDockerDaemon( - buildConfiguration, sourceFilesConfiguration, getCacheInitializer(buildConfiguration))); + buildConfiguration, getCacheInitializer(buildConfiguration))); } // TODO: Move this up to somewhere where defaults for cache location are provided and ownership is @@ -167,21 +165,34 @@ public void build(HelpfulSuggestions helpfulSuggestions) throws BuildStepsExecut buildLogger.info("Containerizing application with the following files:"); buildLogger.info("\tClasses:"); + // TODO: Don't use the indexes. buildSteps - .getSourceFilesConfiguration() - .getClassesFiles() + .getBuildConfiguration() + .getLayerConfigurations() + .get(2) + .getLayerEntries() + .get(0) + .getSourceFiles() .forEach(classesFile -> buildLogger.info("\t\t" + classesFile)); buildLogger.info("\tResources:"); buildSteps - .getSourceFilesConfiguration() - .getResourcesFiles() + .getBuildConfiguration() + .getLayerConfigurations() + .get(1) + .getLayerEntries() + .get(0) + .getSourceFiles() .forEach(resourceFile -> buildLogger.info("\t\t" + resourceFile)); buildLogger.info("\tDependencies:"); buildSteps - .getSourceFilesConfiguration() - .getDependenciesFiles() + .getBuildConfiguration() + .getLayerConfigurations() + .get(0) + .getLayerEntries() + .get(0) + .getSourceFiles() .forEach(dependencyFile -> buildLogger.info("\t\t" + dependencyFile)); buildSteps.run(); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java index 42babaa9da..64283e736a 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java @@ -21,6 +21,7 @@ import com.google.cloud.tools.jib.filesystem.DirectoryWalker; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Modifier; @@ -74,9 +75,10 @@ public static String resolveMainClass( try { // Adds each file in the classes output directory to the classes files list. - Set visitedRoots = new HashSet<>(); + ImmutableList classesFiles = projectProperties.getClassesLayerSourceFiles(); List mainClasses = new ArrayList<>(); - for (Path classPath : projectProperties.getSourceFilesConfiguration().getClassesFiles()) { + Set visitedRoots = new HashSet<>(); + for (Path classPath : classesFiles) { Path root = classPath.getParent(); if (visitedRoots.contains(root)) { continue; diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java index 1ddfa8460c..d07e0ed94f 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java @@ -17,7 +17,7 @@ package com.google.cloud.tools.jib.frontend; import com.google.cloud.tools.jib.builder.BuildLogger; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; +import com.google.common.collect.ImmutableList; import java.nio.file.Path; import javax.annotation.Nullable; @@ -31,7 +31,7 @@ public interface ProjectProperties { String getPluginName(); - SourceFilesConfiguration getSourceFilesConfiguration(); + ImmutableList getClassesLayerSourceFiles(); Path getCacheDirectory(); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java index f6da756da0..3b5816dfc3 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java @@ -20,7 +20,6 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; -import org.mockito.Mockito; /** Tests for {@link EntrypointBuilder}. */ public class EntrypointBuilderTest { @@ -33,16 +32,6 @@ public void testMakeEntrypoint() { List expectedJvmFlags = Arrays.asList("-flag", "anotherFlag"); String expectedMainClass = "SomeMainClass"; - SourceFilesConfiguration mockSourceFilesConfiguration = - Mockito.mock(SourceFilesConfiguration.class); - - Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage()) - .thenReturn(expectedDependenciesPath); - Mockito.when(mockSourceFilesConfiguration.getResourcesPathOnImage()) - .thenReturn(expectedResourcesPath); - Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage()) - .thenReturn(expectedClassesPath); - Assert.assertEquals( Arrays.asList( "java", @@ -52,6 +41,10 @@ public void testMakeEntrypoint() { "/app/libs/*:/app/resources/:/app/classes/", "SomeMainClass"), EntrypointBuilder.makeEntrypoint( - mockSourceFilesConfiguration, expectedJvmFlags, expectedMainClass)); + expectedDependenciesPath, + expectedResourcesPath, + expectedClassesPath, + expectedJvmFlags, + expectedMainClass)); } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java deleted file mode 100644 index 49d27ce489..0000000000 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2018 Google LLC. All rights reserved. - * - * 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 - * - * http://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 com.google.cloud.tools.jib.builder; - -import com.google.common.collect.ImmutableList; -import com.google.common.io.Resources; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.stream.Stream; - -/** Implementation of {@link SourceFilesConfiguration} that uses test resources. */ -public class TestSourceFilesConfiguration implements SourceFilesConfiguration { - - private static final String EXTRACTION_PATH = "/some/extraction/path/"; - - private final ImmutableList dependenciesSourceFiles; - private final ImmutableList resourcesSourceFiles; - private final ImmutableList classesSourceFiles; - - public TestSourceFilesConfiguration() throws URISyntaxException, IOException { - dependenciesSourceFiles = getFilesList("application/dependencies"); - resourcesSourceFiles = getFilesList("application/resources"); - classesSourceFiles = getFilesList("application/classes"); - } - - @Override - public ImmutableList getDependenciesFiles() { - return dependenciesSourceFiles; - } - - @Override - public ImmutableList getResourcesFiles() { - return resourcesSourceFiles; - } - - @Override - public ImmutableList getClassesFiles() { - return classesSourceFiles; - } - - @Override - public String getDependenciesPathOnImage() { - return EXTRACTION_PATH + "libs/"; - } - - @Override - public String getResourcesPathOnImage() { - return EXTRACTION_PATH + "resources/"; - } - - @Override - public String getClassesPathOnImage() { - return EXTRACTION_PATH + "classes/"; - } - - /** Lists the files in the {@code resourcePath} resources directory. */ - private ImmutableList getFilesList(String resourcePath) - throws URISyntaxException, IOException { - try (Stream fileStream = - Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { - return fileStream.collect(ImmutableList.toImmutableList()); - } - } -} diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java index ea229cbdbd..7bbacd1b5d 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java @@ -19,20 +19,25 @@ import com.google.cloud.tools.jib.async.NonBlockingSteps; import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.builder.TestBuildLogger; -import com.google.cloud.tools.jib.builder.TestSourceFilesConfiguration; import com.google.cloud.tools.jib.cache.Cache; import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; import com.google.cloud.tools.jib.cache.CacheReader; import com.google.cloud.tools.jib.cache.CachedLayerWithMetadata; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.image.ImageLayers; import com.google.cloud.tools.jib.image.LayerPropertyNotFoundException; import com.google.common.collect.ImmutableList; +import com.google.common.io.Resources; import com.google.common.util.concurrent.MoreExecutors; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.concurrent.ExecutionException; +import java.util.stream.Stream; import org.junit.Assert; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -45,16 +50,46 @@ @RunWith(MockitoJUnitRunner.class) public class BuildAndCacheApplicationLayerStepTest { + // TODO: Consolidate with BuildStepsIntegrationTest. + private static final String EXTRACTION_PATH = "/some/extraction/path/"; + + /** Lists the files in the {@code resourcePath} resources directory. */ + private static ImmutableList getFilesList(String resourcePath) + throws URISyntaxException, IOException { + try (Stream fileStream = + Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { + return fileStream.collect(ImmutableList.toImmutableList()); + } + } + @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Mock private BuildConfiguration mockBuildConfiguration; + private ImmutableList fakeLayerConfigurations; + + @Before + public void setUp() throws IOException, URISyntaxException { + fakeLayerConfigurations = + ImmutableList.of( + LayerConfiguration.builder() + .addEntry(getFilesList("application/dependencies"), EXTRACTION_PATH + "libs/") + .build(), + LayerConfiguration.builder() + .addEntry(getFilesList("application/resources"), EXTRACTION_PATH + "resources/") + .build(), + LayerConfiguration.builder() + .addEntry(getFilesList("application/classes"), EXTRACTION_PATH + "classes/") + .build()); + } + @Test public void testRun() throws LayerPropertyNotFoundException, IOException, CacheMetadataCorruptedException, - URISyntaxException, ExecutionException { + ExecutionException { Mockito.when(mockBuildConfiguration.getBuildLogger()).thenReturn(new TestBuildLogger()); - TestSourceFilesConfiguration testSourceFilesConfiguration = new TestSourceFilesConfiguration(); + Mockito.when(mockBuildConfiguration.getLayerConfigurations()) + .thenReturn(fakeLayerConfigurations); Path temporaryCacheDirectory = temporaryFolder.newFolder().toPath(); ImageLayers.Builder applicationLayersBuilder = ImageLayers.builder(); @@ -63,10 +98,7 @@ public void testRun() try (Cache cache = Cache.init(temporaryCacheDirectory)) { ImmutableList buildAndCacheApplicationLayerSteps = BuildAndCacheApplicationLayerStep.makeList( - MoreExecutors.newDirectExecutorService(), - mockBuildConfiguration, - testSourceFilesConfiguration, - cache); + MoreExecutors.newDirectExecutorService(), mockBuildConfiguration, cache); for (BuildAndCacheApplicationLayerStep buildAndCacheApplicationLayerStep : buildAndCacheApplicationLayerSteps) { @@ -86,28 +118,34 @@ public void testRun() Assert.assertEquals( applicationLayers.get(0).getBlobDescriptor(), cacheReader - .getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getDependenciesFiles()) + .getUpToDateLayerBySourceFiles( + fakeLayerConfigurations.get(0).getLayerEntries().get(0).getSourceFiles()) .getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(1).getBlobDescriptor(), cacheReader - .getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getResourcesFiles()) + .getUpToDateLayerBySourceFiles( + fakeLayerConfigurations.get(1).getLayerEntries().get(0).getSourceFiles()) .getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(2).getBlobDescriptor(), cacheReader - .getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getClassesFiles()) + .getUpToDateLayerBySourceFiles( + fakeLayerConfigurations.get(2).getLayerEntries().get(0).getSourceFiles()) .getBlobDescriptor()); // Verifies that the cache reader gets the same layers as the newest application layers. Assert.assertEquals( applicationLayers.get(0).getContentFile(), - cacheReader.getLayerFile(testSourceFilesConfiguration.getDependenciesFiles())); + cacheReader.getLayerFile( + fakeLayerConfigurations.get(0).getLayerEntries().get(0).getSourceFiles())); Assert.assertEquals( applicationLayers.get(1).getContentFile(), - cacheReader.getLayerFile(testSourceFilesConfiguration.getResourcesFiles())); + cacheReader.getLayerFile( + fakeLayerConfigurations.get(1).getLayerEntries().get(0).getSourceFiles())); Assert.assertEquals( applicationLayers.get(2).getContentFile(), - cacheReader.getLayerFile(testSourceFilesConfiguration.getClassesFiles())); + cacheReader.getLayerFile( + fakeLayerConfigurations.get(2).getLayerEntries().get(0).getSourceFiles())); } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java index 89ac1936eb..f5ad173920 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java @@ -16,8 +16,8 @@ package com.google.cloud.tools.jib.docker; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.filesystem.DirectoryWalker; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import com.google.common.io.Resources; import java.io.IOException; @@ -31,13 +31,10 @@ import java.util.Deque; import java.util.List; import org.junit.Assert; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; /** Tests for {@link DockerContextGenerator}. */ @@ -60,21 +57,9 @@ private static void assertSameFiles(Path directory1, Path directory2) throws IOE @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - @Mock private SourceFilesConfiguration mockSourceFilesConfiguration; - - @Before - public void setUpMocks() { - String expectedDependenciesPath = "/app/libs/"; - String expectedResourcesPath = "/app/resources/"; - String expectedClassesPath = "/app/classes/"; - - Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage()) - .thenReturn(expectedDependenciesPath); - Mockito.when(mockSourceFilesConfiguration.getResourcesPathOnImage()) - .thenReturn(expectedResourcesPath); - Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage()) - .thenReturn(expectedClassesPath); - } + private String expectedDependenciesPath = "/app/libs/"; + private String expectedResourcesPath = "/app/resources/"; + private String expectedClassesPath = "/app/classes/"; @Test public void testGenerate() throws IOException, URISyntaxException { @@ -87,11 +72,6 @@ public void testGenerate() throws IOException, URISyntaxException { ImmutableList expectedResourcesFiles = new DirectoryWalker(testResources).filterRoot().walk(); ImmutableList expectedClassesFiles = new DirectoryWalker(testClasses).filterRoot().walk(); - Mockito.when(mockSourceFilesConfiguration.getDependenciesFiles()) - .thenReturn(expectedDependenciesFiles); - Mockito.when(mockSourceFilesConfiguration.getResourcesFiles()) - .thenReturn(expectedResourcesFiles); - Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(expectedClassesFiles); Path targetDirectory = temporaryFolder.newFolder().toPath(); @@ -101,7 +81,10 @@ public void testGenerate() throws IOException, URISyntaxException { */ Files.delete(targetDirectory); - new DockerContextGenerator(mockSourceFilesConfiguration) + new DockerContextGenerator( + new LayerEntry(expectedDependenciesFiles, expectedDependenciesPath), + new LayerEntry(expectedResourcesFiles, expectedResourcesPath), + new LayerEntry(expectedClassesFiles, expectedClassesPath)) .setBaseImage("somebaseimage") .generate(targetDirectory); @@ -120,7 +103,10 @@ public void testMakeDockerfile() throws IOException { List exposedPorts = Arrays.asList("1000/tcp", "2000-2010/udp"); String dockerfile = - new DockerContextGenerator(mockSourceFilesConfiguration) + new DockerContextGenerator( + new LayerEntry(ImmutableList.of(), expectedDependenciesPath), + new LayerEntry(ImmutableList.of(), expectedResourcesPath), + new LayerEntry(ImmutableList.of(), expectedClassesPath)) .setBaseImage(expectedBaseImage) .setJvmFlags(expectedJvmFlags) .setMainClass(expectedMainClass) diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java index cd1800ddef..584173e140 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java @@ -26,6 +26,7 @@ import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; import com.google.cloud.tools.jib.configuration.CacheConfiguration; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.registry.InsecureRegistryException; import com.google.cloud.tools.jib.registry.RegistryUnauthorizedException; import com.google.common.collect.ImmutableList; @@ -76,12 +77,12 @@ public void setUpMocks() { Mockito.when(mockBuildSteps.getBuildConfiguration()).thenReturn(mockBuildConfiguration); Mockito.when(mockBuildConfiguration.getBuildLogger()).thenReturn(mockBuildLogger); - Mockito.when(mockBuildSteps.getSourceFilesConfiguration()) - .thenReturn(mockSourceFilesConfiguration); - Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(ImmutableList.of()); - Mockito.when(mockSourceFilesConfiguration.getResourcesFiles()).thenReturn(ImmutableList.of()); - Mockito.when(mockSourceFilesConfiguration.getDependenciesFiles()) - .thenReturn(ImmutableList.of()); + Mockito.when(mockBuildConfiguration.getLayerConfigurations()) + .thenReturn( + ImmutableList.of( + LayerConfiguration.builder().addEntry(ImmutableList.of(), "ignored").build(), + LayerConfiguration.builder().addEntry(ImmutableList.of(), "ignored").build(), + LayerConfiguration.builder().addEntry(ImmutableList.of(), "ignored").build())); } @Test diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java index 3b2d5cc7b6..152062ef8e 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java @@ -49,8 +49,6 @@ public class MainClassFinderTest { public void setup() { Mockito.when(mockProjectProperties.getLogger()).thenReturn(mockBuildLogger); Mockito.when(mockProjectProperties.getPluginName()).thenReturn("plugin"); - Mockito.when(mockProjectProperties.getSourceFilesConfiguration()) - .thenReturn(mockSourceFilesConfiguration); Mockito.when(mockProjectProperties.getMainClassHelpfulSuggestions(ArgumentMatchers.any())) .thenReturn(mockHelpfulSuggestions); Mockito.when(mockProjectProperties.getJarPluginName()).thenReturn("jar-plugin"); @@ -136,7 +134,7 @@ public void testResolveMainClass() throws MainClassInferenceException { @Test public void testResolveMainClass_notValid() throws MainClassInferenceException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(fakeClassesPath); + Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()).thenReturn(fakeClassesPath); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -146,7 +144,7 @@ public void testResolveMainClass_notValid() throws MainClassInferenceException { public void testResolveMainClass_multipleInferredWithBackup() throws MainClassInferenceException, URISyntaxException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockSourceFilesConfiguration.getClassesFiles()) + Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()) .thenReturn( ImmutableList.of( Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), @@ -162,7 +160,7 @@ public void testResolveMainClass_multipleInferredWithBackup() @Test public void testResolveMainClass_multipleInferredWithoutBackup() throws URISyntaxException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn(null); - Mockito.when(mockSourceFilesConfiguration.getClassesFiles()) + Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()) .thenReturn( ImmutableList.of( Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), @@ -183,7 +181,7 @@ public void testResolveMainClass_multipleInferredWithoutBackup() throws URISynta @Test public void testResolveMainClass_noneInferredWithBackup() throws MainClassInferenceException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(ImmutableList.of()); + Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()).thenReturn(ImmutableList.of()); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -191,7 +189,7 @@ public void testResolveMainClass_noneInferredWithBackup() throws MainClassInfere @Test public void testResolveMainClass_noneInferredWithoutBackup() { - Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(ImmutableList.of()); + Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()).thenReturn(ImmutableList.of()); try { MainClassFinder.resolveMainClass(null, mockProjectProperties); Assert.fail(); diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index 61e92b8177..0ffbb0e6d1 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -114,7 +114,8 @@ public void buildImage() throws InvalidImageReferenceException { .setExposedPorts( ExposedPortsParser.parse(jibExtension.getExposedPorts(), gradleBuildLogger)) .setTargetFormat(jibExtension.getFormat()) - .setAllowHttp(jibExtension.getAllowInsecureRegistries()); + .setAllowHttp(jibExtension.getAllowInsecureRegistries()) + .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( @@ -131,8 +132,7 @@ public void buildImage() throws InvalidImageReferenceException { RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX); try { - BuildStepsRunner.forBuildImage( - buildConfiguration, gradleProjectProperties.getSourceFilesConfiguration()) + BuildStepsRunner.forBuildImage(buildConfiguration) .build(HELPFUL_SUGGESTIONS); } catch (CacheDirectoryCreationException | BuildStepsExecutionException ex) { diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java similarity index 66% rename from jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java rename to jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 1f22997656..22d3282867 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -17,12 +17,14 @@ package com.google.cloud.tools.jib.gradle; import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.common.collect.ImmutableList; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.stream.Stream; import org.gradle.api.Project; @@ -30,27 +32,19 @@ import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; -/** {@link SourceFilesConfiguration} implementation based on inputs from a {@link Project}. */ -class GradleSourceFilesConfiguration implements SourceFilesConfiguration { +import javax.xml.transform.Source; + +/** Builds {@link LayerConfiguration}s based on inputs from a {@link Project}. */ +class GradleLayerConfigurations { /** Name of the `main` {@link SourceSet} to use as source files. */ private static final String MAIN_SOURCE_SET_NAME = "main"; /** Resolves the source files configuration for a Gradle {@link Project}. */ - static GradleSourceFilesConfiguration getForProject( + static GradleLayerConfigurations getForProject( Project project, GradleBuildLogger gradleBuildLogger) throws IOException { - return new GradleSourceFilesConfiguration(project, gradleBuildLogger); - } - - private final ImmutableList dependenciesFiles; - private final ImmutableList resourcesFiles; - private final ImmutableList classesFiles; - - /** Instantiate with {@link #getForProject}. */ - private GradleSourceFilesConfiguration(Project project, GradleBuildLogger gradleBuildLogger) - throws IOException { JavaPluginConvention javaPluginConvention = - project.getConvention().getPlugin(JavaPluginConvention.class); + project.getConvention().getPlugin(JavaPluginConvention.class); SourceSet mainSourceSet = javaPluginConvention.getSourceSets().getByName(MAIN_SOURCE_SET_NAME); @@ -64,7 +58,7 @@ private GradleSourceFilesConfiguration(Project project, GradleBuildLogger gradle if (Files.notExists(classesOutputDirectory.toPath())) { // Warns that output directory was not found. gradleBuildLogger.warn( - "Could not find build output directory '" + classesOutputDirectory + "'"); + "Could not find build output directory '" + classesOutputDirectory + "'"); continue; } try (Stream classFileStream = Files.list(classesOutputDirectory.toPath())) { @@ -96,38 +90,27 @@ private GradleSourceFilesConfiguration(Project project, GradleBuildLogger gradle } // Sorts all files by path for consistent ordering. - this.dependenciesFiles = ImmutableList.sortedCopyOf(dependenciesFiles); - this.resourcesFiles = ImmutableList.sortedCopyOf(resourcesFiles); - this.classesFiles = ImmutableList.sortedCopyOf(classesFiles); - } - - @Override - public ImmutableList getDependenciesFiles() { - return dependenciesFiles; + Collections.sort(dependenciesFiles); + Collections.sort(resourcesFiles); + Collections.sort(classesFiles); + + return new GradleLayerConfigurations( + LayerConfiguration.builder().addEntry(dependenciesFiles, SourceFilesConfiguration.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE).build(), + LayerConfiguration.builder().addEntry(resourcesFiles, SourceFilesConfiguration.DEFAULT_RESOURCES_PATH_ON_IMAGE).build(), + LayerConfiguration.builder().addEntry(classesFiles, SourceFilesConfiguration.DEFAULT_CLASSES_PATH_ON_IMAGE).build()); } - @Override - public ImmutableList getResourcesFiles() { - return resourcesFiles; - } - - @Override - public ImmutableList getClassesFiles() { - return classesFiles; - } - - @Override - public String getDependenciesPathOnImage() { - return DEFAULT_DEPENDENCIES_PATH_ON_IMAGE; - } + private final LayerConfiguration dependenciesLayerConfiguration; + private final LayerConfiguration resourcesLayerConfiguration; + private final LayerConfiguration classesLayerConfiguration; - @Override - public String getResourcesPathOnImage() { - return DEFAULT_RESOURCES_PATH_ON_IMAGE; + private GradleLayerConfigurations(LayerConfiguration dependenciesLayerConfiguration, LayerConfiguration resourcesLayerConfiguration, LayerConfiguration classesLayerConfiguration) { + this.dependenciesLayerConfiguration = dependenciesLayerConfiguration; + this.resourcesLayerConfiguration = resourcesLayerConfiguration; + this.classesLayerConfiguration = classesLayerConfiguration; } - @Override - public String getClassesPathOnImage() { - return DEFAULT_CLASSES_PATH_ON_IMAGE; + LayerConfiguration getClassesLayerConfiguration() { + return classesLayerConfiguration; } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java index 258c15fc7c..235dcfff43 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java @@ -18,6 +18,7 @@ import com.google.cloud.tools.jib.builder.BuildLogger; import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; import com.google.cloud.tools.jib.frontend.MainClassFinder; import com.google.cloud.tools.jib.frontend.MainClassInferenceException; @@ -28,6 +29,8 @@ import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable; + +import com.google.common.collect.ImmutableList; import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.Task; @@ -46,7 +49,7 @@ static GradleProjectProperties getForProject( return new GradleProjectProperties( project, gradleBuildLogger, - GradleSourceFilesConfiguration.getForProject(project, gradleBuildLogger)); + GradleLayerConfigurations.getForProject(project, gradleBuildLogger)); } catch (IOException ex) { throw new GradleException("Obtaining project build output files failed", ex); @@ -55,21 +58,21 @@ static GradleProjectProperties getForProject( private final Project project; private final GradleBuildLogger gradleBuildLogger; - private final SourceFilesConfiguration sourceFilesConfiguration; + private final GradleLayerConfigurations gradleLayerConfigurations; @VisibleForTesting GradleProjectProperties( Project project, GradleBuildLogger gradleBuildLogger, - SourceFilesConfiguration sourceFilesConfiguration) { + GradleLayerConfigurations gradleLayerConfigurations) { this.project = project; this.gradleBuildLogger = gradleBuildLogger; - this.sourceFilesConfiguration = sourceFilesConfiguration; + this.gradleLayerConfigurations = gradleLayerConfigurations; } @Override - public SourceFilesConfiguration getSourceFilesConfiguration() { - return sourceFilesConfiguration; + public ImmutableList getClassesLayerSourceFiles() { + return gradleLayerConfigurations.getClassesLayerConfiguration().getLayerEntries().get(0).getSourceFiles(); } @Override From 69e6b57b642e65ce00ebfba1dc16f30e337bd84f Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Fri, 6 Jul 2018 17:54:25 -0400 Subject: [PATCH 02/19] Applies to jib-gradle-plugin. --- .../tools/jib/frontend/BuildStepsRunner.java | 3 +- .../tools/jib/frontend/MainClassFinder.java | 2 +- .../tools/jib/frontend/ProjectProperties.java | 10 ++++- .../jib/frontend/MainClassFinderTest.java | 17 ++++---- .../tools/jib/gradle/BuildDockerTask.java | 4 +- .../tools/jib/gradle/BuildImageTask.java | 3 +- .../tools/jib/gradle/DockerContextTask.java | 5 ++- .../jib/gradle/GradleLayerConfigurations.java | 42 ++++++++++++++----- .../jib/gradle/GradleProjectProperties.java | 24 ++++++++--- ...ava => GradleLayerConfigurationsTest.java} | 32 ++++++++------ .../gradle/GradleProjectPropertiesTest.java | 5 +-- 11 files changed, 99 insertions(+), 48 deletions(-) rename jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/{GradleSourceFilesConfigurationTest.java => GradleLayerConfigurationsTest.java} (84%) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java index 495b84a702..e48496d0b3 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java @@ -58,12 +58,11 @@ public static BuildStepsRunner forBuildImage(BuildConfiguration buildConfigurati * Creates a runner to build to the Docker daemon. Creates a directory for the cache, if needed. * * @param buildConfiguration the configuration parameters for the build - * @param sourceFilesConfiguration the source/destination file configuration for the image * @return a {@link BuildStepsRunner} for building to a Docker daemon * @throws CacheDirectoryCreationException if the {@code cacheDirectory} could not be created */ public static BuildStepsRunner forBuildToDockerDaemon( - BuildConfiguration buildConfiguration, SourceFilesConfiguration sourceFilesConfiguration) + BuildConfiguration buildConfiguration) throws CacheDirectoryCreationException { return new BuildStepsRunner( BuildSteps.forBuildToDockerDaemon( diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java index 64283e736a..36d0cbf12f 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java @@ -75,7 +75,7 @@ public static String resolveMainClass( try { // Adds each file in the classes output directory to the classes files list. - ImmutableList classesFiles = projectProperties.getClassesLayerSourceFiles(); + ImmutableList classesFiles = projectProperties.getClassesLayerEntry().getSourceFiles(); List mainClasses = new ArrayList<>(); Set visitedRoots = new HashSet<>(); for (Path classPath : classesFiles) { diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java index d07e0ed94f..2d239b8760 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/ProjectProperties.java @@ -17,6 +17,8 @@ package com.google.cloud.tools.jib.frontend; import com.google.cloud.tools.jib.builder.BuildLogger; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import java.nio.file.Path; import javax.annotation.Nullable; @@ -31,7 +33,7 @@ public interface ProjectProperties { String getPluginName(); - ImmutableList getClassesLayerSourceFiles(); + ImmutableList getLayerConfigurations(); Path getCacheDirectory(); @@ -41,6 +43,12 @@ public interface ProjectProperties { @Nullable String getMainClassFromJar(); + LayerEntry getDependenciesLayerEntry(); + + LayerEntry getResourcesLayerEntry(); + + LayerEntry getClassesLayerEntry(); + /** * @param prefix the prefix message for the {@link HelpfulSuggestions}. * @return a {@link HelpfulSuggestions} instance for main class inference failure. diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java index 152062ef8e..35646867f8 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java @@ -18,6 +18,7 @@ import com.google.cloud.tools.jib.builder.BuildLogger; import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import com.google.common.io.Resources; import java.io.IOException; @@ -134,7 +135,7 @@ public void testResolveMainClass() throws MainClassInferenceException { @Test public void testResolveMainClass_notValid() throws MainClassInferenceException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()).thenReturn(fakeClassesPath); + Mockito.when(mockProjectProperties.getClassesLayerEntry()).thenReturn(new LayerEntry(fakeClassesPath, "ignored")); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -144,14 +145,15 @@ public void testResolveMainClass_notValid() throws MainClassInferenceException { public void testResolveMainClass_multipleInferredWithBackup() throws MainClassInferenceException, URISyntaxException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()) + Mockito.when(mockProjectProperties.getClassesLayerEntry()) .thenReturn( + new LayerEntry( ImmutableList.of( Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), Paths.get( Resources.getResource("class-finder-tests/multiple/HelloWorld.class").toURI()), Paths.get( - Resources.getResource("class-finder-tests/multiple/NotMain.class").toURI()))); + Resources.getResource("class-finder-tests/multiple/NotMain.class").toURI())), "ignored")); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -160,14 +162,15 @@ public void testResolveMainClass_multipleInferredWithBackup() @Test public void testResolveMainClass_multipleInferredWithoutBackup() throws URISyntaxException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn(null); - Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()) + Mockito.when(mockProjectProperties.getClassesLayerEntry()) .thenReturn( + new LayerEntry( ImmutableList.of( Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), Paths.get( Resources.getResource("class-finder-tests/multiple/HelloWorld.class").toURI()), Paths.get( - Resources.getResource("class-finder-tests/multiple/NotMain.class").toURI()))); + Resources.getResource("class-finder-tests/multiple/NotMain.class").toURI())), "ignored")); try { MainClassFinder.resolveMainClass(null, mockProjectProperties); Assert.fail(); @@ -181,7 +184,7 @@ public void testResolveMainClass_multipleInferredWithoutBackup() throws URISynta @Test public void testResolveMainClass_noneInferredWithBackup() throws MainClassInferenceException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()).thenReturn(ImmutableList.of()); + Mockito.when(mockProjectProperties.getClassesLayerEntry()).thenReturn(new LayerEntry(ImmutableList.of(), "ignored")); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -189,7 +192,7 @@ public void testResolveMainClass_noneInferredWithBackup() throws MainClassInfere @Test public void testResolveMainClass_noneInferredWithoutBackup() { - Mockito.when(mockProjectProperties.getClassesLayerSourceFiles()).thenReturn(ImmutableList.of()); + Mockito.when(mockProjectProperties.getClassesLayerEntry()).thenReturn(new LayerEntry(ImmutableList.of(), "ignored")); try { MainClassFinder.resolveMainClass(null, mockProjectProperties); Assert.fail(); diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index 2cd592f0c9..af337fe39c 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -125,9 +125,7 @@ public void buildDocker() throws InvalidImageReferenceException { // Uses a directory in the Gradle build cache as the Jib cache. try { - BuildStepsRunner.forBuildToDockerDaemon( - buildConfiguration, gradleProjectProperties.getSourceFilesConfiguration()) - .build(HELPFUL_SUGGESTIONS); + BuildStepsRunner.forBuildToDockerDaemon(buildConfiguration).build(HELPFUL_SUGGESTIONS); } catch (CacheDirectoryCreationException | BuildStepsExecutionException ex) { throw new GradleException(ex.getMessage(), ex.getCause()); diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index 0ffbb0e6d1..7f11283164 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -132,8 +132,7 @@ public void buildImage() throws InvalidImageReferenceException { RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX); try { - BuildStepsRunner.forBuildImage(buildConfiguration) - .build(HELPFUL_SUGGESTIONS); + BuildStepsRunner.forBuildImage(buildConfiguration).build(HELPFUL_SUGGESTIONS); } catch (CacheDirectoryCreationException | BuildStepsExecutionException ex) { throw new GradleException(ex.getMessage(), ex.getCause()); diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java index 3477db3021..80b4b0b20e 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java @@ -123,7 +123,10 @@ public void generateDockerContext() { // here. ExposedPortsParser.parse(jibExtension.getExposedPorts(), gradleBuildLogger); - new DockerContextGenerator(gradleProjectProperties.getSourceFilesConfiguration()) + new DockerContextGenerator( + gradleProjectProperties.getResourcesLayerEntry(), + gradleProjectProperties.getResourcesLayerEntry(), + gradleProjectProperties.getClassesLayerEntry()) .setBaseImage(jibExtension.getBaseImage()) .setJvmFlags(jibExtension.getJvmFlags()) .setMainClass(mainClass) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 22d3282867..df9926a99c 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -18,6 +18,7 @@ import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.configuration.LayerConfiguration; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import java.io.File; import java.io.IOException; @@ -32,8 +33,6 @@ import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; -import javax.xml.transform.Source; - /** Builds {@link LayerConfiguration}s based on inputs from a {@link Project}. */ class GradleLayerConfigurations { @@ -44,7 +43,7 @@ class GradleLayerConfigurations { static GradleLayerConfigurations getForProject( Project project, GradleBuildLogger gradleBuildLogger) throws IOException { JavaPluginConvention javaPluginConvention = - project.getConvention().getPlugin(JavaPluginConvention.class); + project.getConvention().getPlugin(JavaPluginConvention.class); SourceSet mainSourceSet = javaPluginConvention.getSourceSets().getByName(MAIN_SOURCE_SET_NAME); @@ -58,7 +57,7 @@ static GradleLayerConfigurations getForProject( if (Files.notExists(classesOutputDirectory.toPath())) { // Warns that output directory was not found. gradleBuildLogger.warn( - "Could not find build output directory '" + classesOutputDirectory + "'"); + "Could not find build output directory '" + classesOutputDirectory + "'"); continue; } try (Stream classFileStream = Files.list(classesOutputDirectory.toPath())) { @@ -95,22 +94,45 @@ static GradleLayerConfigurations getForProject( Collections.sort(classesFiles); return new GradleLayerConfigurations( - LayerConfiguration.builder().addEntry(dependenciesFiles, SourceFilesConfiguration.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE).build(), - LayerConfiguration.builder().addEntry(resourcesFiles, SourceFilesConfiguration.DEFAULT_RESOURCES_PATH_ON_IMAGE).build(), - LayerConfiguration.builder().addEntry(classesFiles, SourceFilesConfiguration.DEFAULT_CLASSES_PATH_ON_IMAGE).build()); + LayerConfiguration.builder() + .addEntry( + dependenciesFiles, SourceFilesConfiguration.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .build(), + LayerConfiguration.builder() + .addEntry(resourcesFiles, SourceFilesConfiguration.DEFAULT_RESOURCES_PATH_ON_IMAGE) + .build(), + LayerConfiguration.builder() + .addEntry(classesFiles, SourceFilesConfiguration.DEFAULT_CLASSES_PATH_ON_IMAGE) + .build()); } private final LayerConfiguration dependenciesLayerConfiguration; private final LayerConfiguration resourcesLayerConfiguration; private final LayerConfiguration classesLayerConfiguration; - private GradleLayerConfigurations(LayerConfiguration dependenciesLayerConfiguration, LayerConfiguration resourcesLayerConfiguration, LayerConfiguration classesLayerConfiguration) { + private GradleLayerConfigurations( + LayerConfiguration dependenciesLayerConfiguration, + LayerConfiguration resourcesLayerConfiguration, + LayerConfiguration classesLayerConfiguration) { this.dependenciesLayerConfiguration = dependenciesLayerConfiguration; this.resourcesLayerConfiguration = resourcesLayerConfiguration; this.classesLayerConfiguration = classesLayerConfiguration; } - LayerConfiguration getClassesLayerConfiguration() { - return classesLayerConfiguration; + ImmutableList getLayerConfigurations() { + return ImmutableList.of( + dependenciesLayerConfiguration, resourcesLayerConfiguration, classesLayerConfiguration); + } + + LayerEntry getDependenciesLayerEntry() { + return dependenciesLayerConfiguration.getLayerEntries().get(0); + } + + LayerEntry getResourcesLayerEntry() { + return resourcesLayerConfiguration.getLayerEntries().get(0); + } + + LayerEntry getClassesLayerEntry() { + return classesLayerConfiguration.getLayerEntries().get(0); } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java index 235dcfff43..9144381918 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java @@ -17,20 +17,19 @@ package com.google.cloud.tools.jib.gradle; import com.google.cloud.tools.jib.builder.BuildLogger; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; import com.google.cloud.tools.jib.frontend.MainClassFinder; import com.google.cloud.tools.jib.frontend.MainClassInferenceException; import com.google.cloud.tools.jib.frontend.ProjectProperties; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable; - -import com.google.common.collect.ImmutableList; import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.Task; @@ -71,8 +70,23 @@ static GradleProjectProperties getForProject( } @Override - public ImmutableList getClassesLayerSourceFiles() { - return gradleLayerConfigurations.getClassesLayerConfiguration().getLayerEntries().get(0).getSourceFiles(); + public ImmutableList getLayerConfigurations() { + return gradleLayerConfigurations.getLayerConfigurations(); + } + + @Override + public LayerEntry getDependenciesLayerEntry() { + return gradleLayerConfigurations.getDependenciesLayerEntry(); + } + + @Override + public LayerEntry getResourcesLayerEntry() { + return gradleLayerConfigurations.getResourcesLayerEntry(); + } + + @Override + public LayerEntry getClassesLayerEntry() { + return gradleLayerConfigurations.getClassesLayerEntry(); } @Override diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java similarity index 84% rename from jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java rename to jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java index 54376a14c5..02a8d94fcd 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java @@ -42,9 +42,9 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -/** Test for {@link GradleSourceFilesConfiguration}. */ +/** Test for {@link GradleLayerConfigurations}. */ @RunWith(MockitoJUnitRunner.class) -public class GradleSourceFilesConfigurationTest { +public class GradleLayerConfigurationsTest { /** Implementation of {@link FileCollection} that just holds a set of {@link File}s. */ private static class TestFileCollection extends AbstractFileCollection { @@ -74,7 +74,7 @@ public Set getFiles() { @Mock private SourceSetOutput mockMainSourceSetOutput; @Mock private GradleBuildLogger mockGradleBuildLogger; - private GradleSourceFilesConfiguration testGradleSourceFilesConfiguration; + private GradleLayerConfigurations testGradleLayerConfigurations; @Before public void setUp() throws URISyntaxException, IOException { @@ -105,8 +105,8 @@ public void setUp() throws URISyntaxException, IOException { Mockito.when(mockMainSourceSetOutput.getResourcesDir()).thenReturn(resourcesOutputDir); Mockito.when(mockMainSourceSet.getRuntimeClasspath()).thenReturn(runtimeFileCollection); - testGradleSourceFilesConfiguration = - GradleSourceFilesConfiguration.getForProject(mockProject, mockGradleBuildLogger); + testGradleLayerConfigurations = + GradleLayerConfigurations.getForProject(mockProject, mockGradleBuildLogger); } @Test @@ -129,10 +129,14 @@ public void test_correctFiles() throws URISyntaxException { Paths.get(Resources.getResource("application/classes").toURI()).resolve("some.class")); Assert.assertEquals( - expectedDependenciesFiles, testGradleSourceFilesConfiguration.getDependenciesFiles()); + expectedDependenciesFiles, + testGradleLayerConfigurations.getDependenciesLayerEntry().getSourceFiles()); Assert.assertEquals( - expectedResourcesFiles, testGradleSourceFilesConfiguration.getResourcesFiles()); - Assert.assertEquals(expectedClassesFiles, testGradleSourceFilesConfiguration.getClassesFiles()); + expectedResourcesFiles, + testGradleLayerConfigurations.getResourcesLayerEntry().getSourceFiles()); + Assert.assertEquals( + expectedClassesFiles, + testGradleLayerConfigurations.getClassesLayerEntry().getSourceFiles()); } @Test @@ -141,8 +145,8 @@ public void test_noClassesFiles() throws IOException { Mockito.when(mockMainSourceSetOutput.getClassesDirs()) .thenReturn(new TestFileCollection(ImmutableSet.of(nonexistentFile))); - testGradleSourceFilesConfiguration = - GradleSourceFilesConfiguration.getForProject(mockProject, mockGradleBuildLogger); + testGradleLayerConfigurations = + GradleLayerConfigurations.getForProject(mockProject, mockGradleBuildLogger); Mockito.verify(mockGradleBuildLogger) .warn("Could not find build output directory '" + nonexistentFile + "'"); @@ -153,10 +157,12 @@ public void test_noClassesFiles() throws IOException { @Test public void test_correctPathsOnImage() { Assert.assertEquals( - "/app/libs/", testGradleSourceFilesConfiguration.getDependenciesPathOnImage()); + "/app/libs/", + testGradleLayerConfigurations.getDependenciesLayerEntry().getExtractionPath()); Assert.assertEquals( - "/app/resources/", testGradleSourceFilesConfiguration.getResourcesPathOnImage()); + "/app/resources/", + testGradleLayerConfigurations.getResourcesLayerEntry().getExtractionPath()); Assert.assertEquals( - "/app/classes/", testGradleSourceFilesConfiguration.getClassesPathOnImage()); + "/app/classes/", testGradleLayerConfigurations.getClassesLayerEntry().getExtractionPath()); } } diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java index f7c439d556..0e120ed8b8 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java @@ -16,7 +16,6 @@ package com.google.cloud.tools.jib.gradle; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.util.Collections; @@ -42,7 +41,7 @@ public class GradleProjectPropertiesTest { @Mock private Jar mockJar2; @Mock private Project mockProject; @Mock private GradleBuildLogger mockGradleBuildLogger; - @Mock private SourceFilesConfiguration mockSourceFilesConfiguration; + @Mock private GradleLayerConfigurations mockGradleLayerConfigurations; private Manifest manifest; private GradleProjectProperties gradleProjectProperties; @@ -54,7 +53,7 @@ public void setup() { gradleProjectProperties = new GradleProjectProperties( - mockProject, mockGradleBuildLogger, mockSourceFilesConfiguration); + mockProject, mockGradleBuildLogger, mockGradleLayerConfigurations); } @Test From 84f5ef6463351b45096c6ea8b1f5d0c25c2e8c14 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Tue, 10 Jul 2018 09:15:34 -0400 Subject: [PATCH 03/19] Fixes format. --- .../tools/jib/frontend/BuildStepsRunner.java | 4 +- .../tools/jib/frontend/MainClassFinder.java | 3 +- .../jib/frontend/MainClassFinderTest.java | 39 ++++++++++++------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java index e48496d0b3..e5dbacdabd 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java @@ -21,7 +21,6 @@ import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.builder.BuildLogger; import com.google.cloud.tools.jib.builder.BuildSteps; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException; import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; @@ -61,8 +60,7 @@ public static BuildStepsRunner forBuildImage(BuildConfiguration buildConfigurati * @return a {@link BuildStepsRunner} for building to a Docker daemon * @throws CacheDirectoryCreationException if the {@code cacheDirectory} could not be created */ - public static BuildStepsRunner forBuildToDockerDaemon( - BuildConfiguration buildConfiguration) + public static BuildStepsRunner forBuildToDockerDaemon(BuildConfiguration buildConfiguration) throws CacheDirectoryCreationException { return new BuildStepsRunner( BuildSteps.forBuildToDockerDaemon( diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java index 36d0cbf12f..009ae52e68 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/MainClassFinder.java @@ -75,7 +75,8 @@ public static String resolveMainClass( try { // Adds each file in the classes output directory to the classes files list. - ImmutableList classesFiles = projectProperties.getClassesLayerEntry().getSourceFiles(); + ImmutableList classesFiles = + projectProperties.getClassesLayerEntry().getSourceFiles(); List mainClasses = new ArrayList<>(); Set visitedRoots = new HashSet<>(); for (Path classPath : classesFiles) { diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java index 35646867f8..a09be631c5 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java @@ -135,7 +135,8 @@ public void testResolveMainClass() throws MainClassInferenceException { @Test public void testResolveMainClass_notValid() throws MainClassInferenceException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockProjectProperties.getClassesLayerEntry()).thenReturn(new LayerEntry(fakeClassesPath, "ignored")); + Mockito.when(mockProjectProperties.getClassesLayerEntry()) + .thenReturn(new LayerEntry(fakeClassesPath, "ignored")); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -148,12 +149,15 @@ public void testResolveMainClass_multipleInferredWithBackup() Mockito.when(mockProjectProperties.getClassesLayerEntry()) .thenReturn( new LayerEntry( - ImmutableList.of( - Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), - Paths.get( - Resources.getResource("class-finder-tests/multiple/HelloWorld.class").toURI()), - Paths.get( - Resources.getResource("class-finder-tests/multiple/NotMain.class").toURI())), "ignored")); + ImmutableList.of( + Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), + Paths.get( + Resources.getResource("class-finder-tests/multiple/HelloWorld.class") + .toURI()), + Paths.get( + Resources.getResource("class-finder-tests/multiple/NotMain.class") + .toURI())), + "ignored")); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -165,12 +169,15 @@ public void testResolveMainClass_multipleInferredWithoutBackup() throws URISynta Mockito.when(mockProjectProperties.getClassesLayerEntry()) .thenReturn( new LayerEntry( - ImmutableList.of( - Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), - Paths.get( - Resources.getResource("class-finder-tests/multiple/HelloWorld.class").toURI()), - Paths.get( - Resources.getResource("class-finder-tests/multiple/NotMain.class").toURI())), "ignored")); + ImmutableList.of( + Paths.get(Resources.getResource("class-finder-tests/multiple/multi").toURI()), + Paths.get( + Resources.getResource("class-finder-tests/multiple/HelloWorld.class") + .toURI()), + Paths.get( + Resources.getResource("class-finder-tests/multiple/NotMain.class") + .toURI())), + "ignored")); try { MainClassFinder.resolveMainClass(null, mockProjectProperties); Assert.fail(); @@ -184,7 +191,8 @@ public void testResolveMainClass_multipleInferredWithoutBackup() throws URISynta @Test public void testResolveMainClass_noneInferredWithBackup() throws MainClassInferenceException { Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); - Mockito.when(mockProjectProperties.getClassesLayerEntry()).thenReturn(new LayerEntry(ImmutableList.of(), "ignored")); + Mockito.when(mockProjectProperties.getClassesLayerEntry()) + .thenReturn(new LayerEntry(ImmutableList.of(), "ignored")); Assert.assertEquals( "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); @@ -192,7 +200,8 @@ public void testResolveMainClass_noneInferredWithBackup() throws MainClassInfere @Test public void testResolveMainClass_noneInferredWithoutBackup() { - Mockito.when(mockProjectProperties.getClassesLayerEntry()).thenReturn(new LayerEntry(ImmutableList.of(), "ignored")); + Mockito.when(mockProjectProperties.getClassesLayerEntry()) + .thenReturn(new LayerEntry(ImmutableList.of(), "ignored")); try { MainClassFinder.resolveMainClass(null, mockProjectProperties); Assert.fail(); From e3d835d4cd278e6ec6747dc0ec90d12468090209 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Thu, 19 Jul 2018 16:59:57 -0400 Subject: [PATCH 04/19] Fixes auth issue. --- .../jib/registry/credentials/DockerCredentialHelper.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java index 88ea117257..84a5b8e419 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java @@ -22,6 +22,7 @@ import com.google.cloud.tools.jib.http.Authorizations; import com.google.cloud.tools.jib.json.JsonTemplate; import com.google.cloud.tools.jib.json.JsonTemplateMapper; +import com.google.common.base.Strings; import com.google.common.io.CharStreams; import java.io.IOException; import java.io.InputStreamReader; @@ -103,7 +104,8 @@ public Authorization retrieve() try { DockerCredentialsTemplate dockerCredentials = JsonTemplateMapper.readJson(output, DockerCredentialsTemplate.class); - if (dockerCredentials.Username == null || dockerCredentials.Secret == null) { + if (Strings.isNullOrEmpty(dockerCredentials.Username) + || Strings.isNullOrEmpty(dockerCredentials.Secret)) { throw new NonexistentServerUrlDockerCredentialHelperException( credentialHelper, serverUrl, output); } From 00176feb44083ab34860373c521ec22eeaab3851 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Fri, 20 Jul 2018 12:34:12 -0400 Subject: [PATCH 05/19] Refactors entrypoint building. --- .../tools/jib/builder/BuildConfiguration.java | 49 +++++++++++------- .../cloud/tools/jib/builder/BuildSteps.java | 20 ++------ .../jib/builder/SourceFilesConfiguration.java | 29 ----------- .../jib/builder/steps/BuildImageStep.java | 7 +-- .../tools/jib/builder/steps/StepsRunner.java | 5 +- .../jib/docker/DockerContextGenerator.java | 10 ++-- .../JavaEntrypointBuilder.java} | 51 ++++++++++--------- .../credentials/DockerCredentialHelper.java | 2 +- .../jib/builder/BuildConfigurationTest.java | 16 +++--- .../jib/builder/steps/BuildImageStepTest.java | 4 +- .../jib/frontend/BuildStepsRunnerTest.java | 2 - .../JavaEntrypointBuilderTest.java} | 25 +++++---- .../jib/frontend/MainClassFinderTest.java | 2 - .../tools/jib/gradle/BuildDockerTask.java | 24 +++------ .../tools/jib/gradle/BuildImageTask.java | 8 +-- .../cloud/tools/jib/gradle/BuildTarTask.java | 29 +++-------- .../tools/jib/gradle/DockerContextTask.java | 3 +- .../jib/gradle/GradleLayerConfigurations.java | 21 ++++---- .../jib/gradle/GradleProjectProperties.java | 4 +- .../gradle/GradleLayerConfigurationsTest.java | 19 ++++--- .../gradle/GradleProjectPropertiesTest.java | 15 +++--- 21 files changed, 149 insertions(+), 196 deletions(-) delete mode 100644 jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java rename jib-core/src/main/java/com/google/cloud/tools/jib/{builder/EntrypointBuilder.java => frontend/JavaEntrypointBuilder.java} (50%) rename jib-core/src/test/java/com/google/cloud/tools/jib/{builder/EntrypointBuilderTest.java => frontend/JavaEntrypointBuilderTest.java} (67%) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index dd5732146e..1272f36809 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -51,7 +51,6 @@ public static class Builder { @Nullable private RegistryCredentials knownTargetRegistryCredentials; @Nullable private String mainClass; private ImmutableList javaArguments = ImmutableList.of(); - private ImmutableList jvmFlags = ImmutableList.of(); private ImmutableMap environmentMap = ImmutableMap.of(); private ImmutableList exposedPorts = ImmutableList.of(); private Class targetFormat = V22ManifestTemplate.class; @@ -59,6 +58,7 @@ public static class Builder { @Nullable private CacheConfiguration baseImageLayersCacheConfiguration; private boolean allowHttp = false; private ImmutableList layerConfigurations = ImmutableList.of(); + private ImmutableList entrypoint = ImmutableList.of(); private BuildLogger buildLogger; @@ -111,14 +111,6 @@ public Builder setJavaArguments(@Nullable List javaArguments) { return this; } - public Builder setJvmFlags(@Nullable List jvmFlags) { - if (jvmFlags != null) { - Preconditions.checkArgument(!jvmFlags.contains(null)); - this.jvmFlags = ImmutableList.copyOf(jvmFlags); - } - return this; - } - public Builder setEnvironment(@Nullable Map environmentMap) { if (environmentMap != null) { Preconditions.checkArgument( @@ -199,6 +191,20 @@ public Builder setCreationTime(Instant creationTime) { return this; } + /** + * Sets the container entrypoint. + * + * @param entrypoint the command to run when the container starts + * @return this + */ + public Builder setEntrypoint(@Nullable List entrypoint) { + if (entrypoint != null) { + Preconditions.checkArgument(!entrypoint.contains(null)); + this.entrypoint = ImmutableList.copyOf(entrypoint); + } + return this; + } + /** @return the corresponding build configuration */ public BuildConfiguration build() { // Validates the parameters. @@ -235,14 +241,14 @@ public BuildConfiguration build() { knownTargetRegistryCredentials, mainClass, javaArguments, - jvmFlags, environmentMap, exposedPorts, targetFormat, applicationLayersCacheConfiguration, baseImageLayersCacheConfiguration, allowHttp, - layerConfigurations); + layerConfigurations, + entrypoint); case 1: throw new IllegalStateException(errorMessages.get(0)); @@ -295,7 +301,6 @@ public static Builder builder(BuildLogger buildLogger) { @Nullable private final RegistryCredentials knownTargetRegistryCredentials; private final String mainClass; private final ImmutableList javaArguments; - private final ImmutableList jvmFlags; private final ImmutableMap environmentMap; private final ImmutableList exposedPorts; private final Class targetFormat; @@ -303,6 +308,7 @@ public static Builder builder(BuildLogger buildLogger) { @Nullable private final CacheConfiguration baseImageLayersCacheConfiguration; private final boolean allowHttp; private final ImmutableList layerConfigurations; + private final ImmutableList entrypoint; /** Instantiate with {@link Builder#build}. */ private BuildConfiguration( @@ -316,14 +322,14 @@ private BuildConfiguration( @Nullable RegistryCredentials knownTargetRegistryCredentials, String mainClass, ImmutableList javaArguments, - ImmutableList jvmFlags, ImmutableMap environmentMap, ImmutableList exposedPorts, Class targetFormat, @Nullable CacheConfiguration applicationLayersCacheConfiguration, @Nullable CacheConfiguration baseImageLayersCacheConfiguration, boolean allowHttp, - ImmutableList layerConfigurations) { + ImmutableList layerConfigurations, + ImmutableList entrypoint) { this.buildLogger = buildLogger; this.creationTime = creationTime; this.baseImageReference = baseImageReference; @@ -334,7 +340,6 @@ private BuildConfiguration( this.knownTargetRegistryCredentials = knownTargetRegistryCredentials; this.mainClass = mainClass; this.javaArguments = javaArguments; - this.jvmFlags = jvmFlags; this.environmentMap = environmentMap; this.exposedPorts = exposedPorts; this.targetFormat = targetFormat; @@ -342,6 +347,7 @@ private BuildConfiguration( this.baseImageLayersCacheConfiguration = baseImageLayersCacheConfiguration; this.allowHttp = allowHttp; this.layerConfigurations = layerConfigurations; + this.entrypoint = entrypoint; } public BuildLogger getBuildLogger() { @@ -412,10 +418,6 @@ public ImmutableList getJavaArguments() { return javaArguments; } - public ImmutableList getJvmFlags() { - return jvmFlags; - } - public ImmutableMap getEnvironment() { return environmentMap; } @@ -465,4 +467,13 @@ public boolean getAllowHttp() { public ImmutableList getLayerConfigurations() { return layerConfigurations; } + + /** + * Gets the container entrypoint. + * + * @return the list of entrypoint tokens + */ + public ImmutableList getEntrypoint() { + return entrypoint; + } } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java index 1124b6fd88..f74032c76b 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildSteps.java @@ -23,7 +23,6 @@ import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; import com.google.cloud.tools.jib.cache.Caches; -import com.google.common.collect.ImmutableList; import java.io.IOException; import java.nio.file.Path; import java.util.concurrent.ExecutionException; @@ -86,7 +85,7 @@ public static BuildSteps forBuildToDockerRegistry( .runPullAndCacheBaseImageLayersStep() .runPushBaseImageLayersStep() .runBuildAndCacheApplicationLayerSteps() - .runBuildImageStep(getEntrypoint(buildConfiguration)) + .runBuildImageStep() .runPushContainerConfigurationStep() .runPushApplicationLayersStep() .runFinalizingPushStep() @@ -116,7 +115,7 @@ public static BuildSteps forBuildToDockerDaemon( .runPullBaseImageStep() .runPullAndCacheBaseImageLayersStep() .runBuildAndCacheApplicationLayerSteps() - .runBuildImageStep(getEntrypoint(buildConfiguration)) + .runBuildImageStep() .runFinalizingBuildStep() .runLoadDockerStep() .waitOnLoadDockerStep()); @@ -145,23 +144,12 @@ public static BuildSteps forBuildToTar( .runPullBaseImageStep() .runPullAndCacheBaseImageLayersStep() .runBuildAndCacheApplicationLayerSteps() - .runBuildImageStep(getEntrypoint(buildConfiguration)) + .runBuildImageStep() .runFinalizingBuildStep() .runWriteTarFileStep(outputPath) .waitOnWriteTarFileStep()); } - /** Creates the container entrypoint for a given configuration. */ - private static ImmutableList getEntrypoint(BuildConfiguration buildConfiguration) { - // TODO: Have classpaths be provided. - return EntrypointBuilder.makeEntrypoint( - SourceFilesConfiguration.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE, - SourceFilesConfiguration.DEFAULT_RESOURCES_PATH_ON_IMAGE, - SourceFilesConfiguration.DEFAULT_CLASSES_PATH_ON_IMAGE, - buildConfiguration.getJvmFlags(), - buildConfiguration.getMainClass()); - } - private final String description; private final BuildConfiguration buildConfiguration; private final Caches.Initializer cachesInitializer; @@ -226,6 +214,6 @@ public void run() buildConfiguration.getBuildLogger().lifecycle(""); buildConfiguration .getBuildLogger() - .lifecycle("Container entrypoint set to " + getEntrypoint(buildConfiguration)); + .lifecycle("Container entrypoint set to " + buildConfiguration.getEntrypoint()); } } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java deleted file mode 100644 index 77a4d78904..0000000000 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2018 Google LLC. All rights reserved. - * - * 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 - * - * http://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 com.google.cloud.tools.jib.builder; - -/** - * Immutable configuration that defines where the source files for each of the application layers - * are. - */ -// TODO: Move these constants to another place. -public interface SourceFilesConfiguration { - - String DEFAULT_DEPENDENCIES_PATH_ON_IMAGE = "/app/libs/"; - String DEFAULT_RESOURCES_PATH_ON_IMAGE = "/app/resources/"; - String DEFAULT_CLASSES_PATH_ON_IMAGE = "/app/classes/"; -} diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java index 94a48e2108..4e7273bd79 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java @@ -41,7 +41,6 @@ class BuildImageStep private final BuildConfiguration buildConfiguration; private final PullAndCacheBaseImageLayersStep pullAndCacheBaseImageLayersStep; private final ImmutableList buildAndCacheApplicationLayerSteps; - private final ImmutableList entrypoint; private final ListeningExecutorService listeningExecutorService; private final ListenableFuture>> listenableFuture; @@ -50,13 +49,11 @@ class BuildImageStep ListeningExecutorService listeningExecutorService, BuildConfiguration buildConfiguration, PullAndCacheBaseImageLayersStep pullAndCacheBaseImageLayersStep, - ImmutableList buildAndCacheApplicationLayerSteps, - ImmutableList entrypoint) { + ImmutableList buildAndCacheApplicationLayerSteps) { this.listeningExecutorService = listeningExecutorService; this.buildConfiguration = buildConfiguration; this.pullAndCacheBaseImageLayersStep = pullAndCacheBaseImageLayersStep; this.buildAndCacheApplicationLayerSteps = buildAndCacheApplicationLayerSteps; - this.entrypoint = entrypoint; listenableFuture = Futures.whenAllSucceed(pullAndCacheBaseImageLayersStep.getFuture()) @@ -101,7 +98,7 @@ private Image afterCachedLayersSteps() } imageBuilder.setCreated(buildConfiguration.getCreationTime()); imageBuilder.setEnvironment(buildConfiguration.getEnvironment()); - imageBuilder.setEntrypoint(entrypoint); + imageBuilder.setEntrypoint(buildConfiguration.getEntrypoint()); imageBuilder.setJavaArguments(buildConfiguration.getJavaArguments()); imageBuilder.setExposedPorts(buildConfiguration.getExposedPorts()); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java index ffed408ee3..bd904de8ce 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java @@ -120,14 +120,13 @@ public StepsRunner runBuildAndCacheApplicationLayerSteps() { return this; } - public StepsRunner runBuildImageStep(ImmutableList entrypoint) { + public StepsRunner runBuildImageStep() { buildImageStep = new BuildImageStep( listeningExecutorService, buildConfiguration, Preconditions.checkNotNull(pullAndCacheBaseImageLayersStep), - Preconditions.checkNotNull(buildAndCacheApplicationLayerSteps), - entrypoint); + Preconditions.checkNotNull(buildAndCacheApplicationLayerSteps)); return this; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java index f92fb9b22d..ed653685d7 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java @@ -18,8 +18,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.cloud.tools.jib.builder.EntrypointBuilder; import com.google.cloud.tools.jib.filesystem.FileOperations; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -58,6 +58,7 @@ public class DockerContextGenerator { private List javaArguments = Collections.emptyList(); private List exposedPorts = Collections.emptyList(); + // TODO: Just take the LayerConfigurations. public DockerContextGenerator( LayerEntry dependenciesLayerEntry, LayerEntry snapshotDependenciesLayerEntry, @@ -218,12 +219,7 @@ String makeDockerfile() throws JsonProcessingException { .append("\nENTRYPOINT ") .append( objectMapper.writeValueAsString( - EntrypointBuilder.makeEntrypoint( - dependenciesLayerEntry.getExtractionPath(), - resourcesLayerEntry.getExtractionPath(), - classesLayerEntry.getExtractionPath(), - jvmFlags, - mainClass))) + JavaEntrypointBuilder.makeDefaultEntrypoint(jvmFlags, mainClass))) .append("\nCMD ") .append(objectMapper.writeValueAsString(javaArguments)); return dockerfile.toString(); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilder.java similarity index 50% rename from jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java rename to jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilder.java index c8e00d5304..151b4d1120 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilder.java @@ -14,48 +14,51 @@ * the License. */ -package com.google.cloud.tools.jib.builder; +package com.google.cloud.tools.jib.frontend; -import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** Builds an image entrypoint for the Java application. */ -public class EntrypointBuilder { +public class JavaEntrypointBuilder { + + public static final String DEFAULT_DEPENDENCIES_PATH_ON_IMAGE = "/app/libs/"; + public static final String DEFAULT_RESOURCES_PATH_ON_IMAGE = "/app/resources/"; + public static final String DEFAULT_CLASSES_PATH_ON_IMAGE = "/app/classes/"; + + public static List makeDefaultEntrypoint(List jvmFlags, String mainClass) { + return makeEntrypoint( + Arrays.asList( + DEFAULT_DEPENDENCIES_PATH_ON_IMAGE + "*", + DEFAULT_RESOURCES_PATH_ON_IMAGE, + DEFAULT_CLASSES_PATH_ON_IMAGE), + jvmFlags, + mainClass); + } /** * Builds the container entrypoint. * *

The entrypoint is {@code java [jvm flags] -cp [classpaths] [main class]}. * - * @param dependenciesExtractionPath extraction path of dependencies - * @param resourcesExtractionPath extraction path of resources - * @param classesExtractionPath extraction path of classes + * @param classpathElements paths to add to the classpath (will be separated by {@code :} * @param jvmFlags the JVM flags to start the container with * @param mainClass the name of the main class to run on startup * @return a list of the entrypoint tokens */ - public static ImmutableList makeEntrypoint( - String dependenciesExtractionPath, - String resourcesExtractionPath, - String classesExtractionPath, - List jvmFlags, - String mainClass) { - // TODO: Entrypoint should be built and added to BuildConfiguration rather than built here. - ImmutableList classPaths = - ImmutableList.of( - dependenciesExtractionPath + "*", resourcesExtractionPath, classesExtractionPath); - - String classPathsString = String.join(":", classPaths); - - ImmutableList.Builder entrypointBuilder = - ImmutableList.builderWithExpectedSize(4 + jvmFlags.size()); + public static List makeEntrypoint( + List classpathElements, List jvmFlags, String mainClass) { + String classpathString = String.join(":", classpathElements); + + List entrypointBuilder = new ArrayList<>(4 + jvmFlags.size()); entrypointBuilder.add("java"); entrypointBuilder.addAll(jvmFlags); entrypointBuilder.add("-cp"); - entrypointBuilder.add(classPathsString); + entrypointBuilder.add(classpathString); entrypointBuilder.add(mainClass); - return entrypointBuilder.build(); + return entrypointBuilder; } - private EntrypointBuilder() {} + private JavaEntrypointBuilder() {} } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java index 8518e9725c..e292867814 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelper.java @@ -22,8 +22,8 @@ import com.google.cloud.tools.jib.http.Authorizations; import com.google.cloud.tools.jib.json.JsonTemplate; import com.google.cloud.tools.jib.json.JsonTemplateMapper; -import com.google.common.base.Strings; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; import com.google.common.io.CharStreams; import java.io.IOException; import java.io.InputStreamReader; diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java index bc7abc8231..cb9313121c 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java @@ -59,7 +59,6 @@ public void testBuilder() { Mockito.mock(RegistryCredentials.class); String expectedMainClass = "mainclass"; List expectedJavaArguments = Arrays.asList("arg1", "arg2"); - List expectedJvmFlags = Arrays.asList("some", "jvm", "flags"); Map expectedEnvironment = ImmutableMap.of("key", "value"); ImmutableList expectedExposedPorts = ImmutableList.of(new Port(1000, Protocol.TCP), new Port(2000, Protocol.TCP)); @@ -69,8 +68,9 @@ public void testBuilder() { CacheConfiguration expectedBaseImageLayersCacheConfiguration = CacheConfiguration.forPath(Paths.get("base/image/layers")); List expectedLayerConfigurations = - Arrays.asList( + Collections.singletonList( LayerConfiguration.builder().addEntry(Collections.emptyList(), "destination").build()); + List expectedEntrypoint = Arrays.asList("some", "entrypoint"); BuildConfiguration.Builder buildConfigurationBuilder = BuildConfiguration.builder(Mockito.mock(BuildLogger.class)) @@ -87,14 +87,14 @@ public void testBuilder() { .setKnownTargetRegistryCredentials(expectedKnownTargetRegistryCredentials) .setMainClass(expectedMainClass) .setJavaArguments(expectedJavaArguments) - .setJvmFlags(expectedJvmFlags) .setEnvironment(expectedEnvironment) .setExposedPorts(expectedExposedPorts) .setTargetFormat(OCIManifestTemplate.class) .setApplicationLayersCacheConfiguration(expectedApplicationLayersCacheConfiguration) .setBaseImageLayersCacheConfiguration(expectedBaseImageLayersCacheConfiguration) .setAllowHttp(true) - .setLayerConfigurations(expectedLayerConfigurations); + .setLayerConfigurations(expectedLayerConfigurations) + .setEntrypoint(expectedEntrypoint); BuildConfiguration buildConfiguration = buildConfigurationBuilder.build(); Assert.assertEquals(expectedCreationTime, buildConfiguration.getCreationTime()); @@ -112,7 +112,6 @@ public void testBuilder() { buildConfiguration.getTargetImageCredentialHelperName()); Assert.assertEquals(expectedMainClass, buildConfiguration.getMainClass()); Assert.assertEquals(expectedJavaArguments, buildConfiguration.getJavaArguments()); - Assert.assertEquals(expectedJvmFlags, buildConfiguration.getJvmFlags()); Assert.assertEquals(expectedEnvironment, buildConfiguration.getEnvironment()); Assert.assertEquals(expectedExposedPorts, buildConfiguration.getExposedPorts()); Assert.assertEquals(expectedTargetFormat, buildConfiguration.getTargetFormat()); @@ -124,6 +123,7 @@ public void testBuilder() { buildConfiguration.getBaseImageLayersCacheConfiguration()); Assert.assertTrue(buildConfiguration.getAllowHttp()); Assert.assertEquals(expectedLayerConfigurations, buildConfiguration.getLayerConfigurations()); + Assert.assertEquals(expectedEntrypoint, buildConfiguration.getEntrypoint()); } @Test @@ -154,7 +154,6 @@ public void testBuilder_default() { Assert.assertNull(buildConfiguration.getTargetImageCredentialHelperName()); Assert.assertNull(buildConfiguration.getKnownTargetRegistryCredentials()); Assert.assertEquals(Collections.emptyList(), buildConfiguration.getJavaArguments()); - Assert.assertEquals(Collections.emptyList(), buildConfiguration.getJvmFlags()); Assert.assertEquals(Collections.emptyMap(), buildConfiguration.getEnvironment()); Assert.assertEquals(Collections.emptyList(), buildConfiguration.getExposedPorts()); Assert.assertEquals(V22ManifestTemplate.class, buildConfiguration.getTargetFormat()); @@ -162,6 +161,7 @@ public void testBuilder_default() { Assert.assertNull(buildConfiguration.getBaseImageLayersCacheConfiguration()); Assert.assertFalse(buildConfiguration.getAllowHttp()); Assert.assertEquals(Collections.emptyList(), buildConfiguration.getLayerConfigurations()); + Assert.assertEquals(Collections.emptyList(), buildConfiguration.getEntrypoint()); } @Test @@ -215,10 +215,10 @@ public void testBuilder_nullValues() { Assert.assertNull(ex.getMessage()); } - // JVM flags element should not be null. + // Entrypoint element should not be null. try { BuildConfiguration.builder(Mockito.mock(BuildLogger.class)) - .setJvmFlags(Arrays.asList("first", null)); + .setEntrypoint(Arrays.asList("first", null)); Assert.fail("The IllegalArgumentException should be thrown."); } catch (IllegalArgumentException ex) { Assert.assertNull(ex.getMessage()); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java index ea865b518c..e9f1000a38 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java @@ -68,6 +68,7 @@ public void setUp() throws DigestException { Mockito.when(mockBuildConfiguration.getEnvironment()).thenReturn(ImmutableMap.of()); Mockito.when(mockBuildConfiguration.getJavaArguments()).thenReturn(ImmutableList.of()); Mockito.when(mockBuildConfiguration.getExposedPorts()).thenReturn(ImmutableList.of()); + Mockito.when(mockBuildConfiguration.getEntrypoint()).thenReturn(ImmutableList.of()); Mockito.when(mockPullAndCacheBaseImageLayersStep.getFuture()) .thenReturn( @@ -92,8 +93,7 @@ public void test_validateAsyncDependencies() throws ExecutionException, Interrup ImmutableList.of( mockBuildAndCacheApplicationLayerStep, mockBuildAndCacheApplicationLayerStep, - mockBuildAndCacheApplicationLayerStep), - ImmutableList.of()); + mockBuildAndCacheApplicationLayerStep)); Image image = buildImageStep.getFuture().get().getFuture().get(); Assert.assertEquals( testDescriptorDigest, image.getLayers().asList().get(0).getBlobDescriptor().getDigest()); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java index a1806c1ebe..f80fbc7ac9 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/BuildStepsRunnerTest.java @@ -21,7 +21,6 @@ import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.builder.BuildLogger; import com.google.cloud.tools.jib.builder.BuildSteps; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException; import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; @@ -63,7 +62,6 @@ public class BuildStepsRunnerTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Mock private BuildSteps mockBuildSteps; - @Mock private SourceFilesConfiguration mockSourceFilesConfiguration; @Mock private BuildLogger mockBuildLogger; @Mock private RegistryUnauthorizedException mockRegistryUnauthorizedException; @Mock private RegistryCredentialsNotSentException mockRegistryCredentialsNotSentException; diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilderTest.java similarity index 67% rename from jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java rename to jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilderTest.java index 3b5816dfc3..976df595af 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilderTest.java @@ -14,24 +14,29 @@ * the License. */ -package com.google.cloud.tools.jib.builder; +package com.google.cloud.tools.jib.frontend; import java.util.Arrays; import java.util.List; import org.junit.Assert; import org.junit.Test; -/** Tests for {@link EntrypointBuilder}. */ -public class EntrypointBuilderTest { +/** Tests for {@link JavaEntrypointBuilder}. */ +public class JavaEntrypointBuilderTest { @Test public void testMakeEntrypoint() { - String expectedDependenciesPath = "/app/libs/"; + String expectedDependenciesPath = "/app/libs/*"; String expectedResourcesPath = "/app/resources/"; String expectedClassesPath = "/app/classes/"; List expectedJvmFlags = Arrays.asList("-flag", "anotherFlag"); String expectedMainClass = "SomeMainClass"; + List entrypoint = + JavaEntrypointBuilder.makeEntrypoint( + Arrays.asList(expectedDependenciesPath, expectedResourcesPath, expectedClassesPath), + expectedJvmFlags, + expectedMainClass); Assert.assertEquals( Arrays.asList( "java", @@ -40,11 +45,11 @@ public void testMakeEntrypoint() { "-cp", "/app/libs/*:/app/resources/:/app/classes/", "SomeMainClass"), - EntrypointBuilder.makeEntrypoint( - expectedDependenciesPath, - expectedResourcesPath, - expectedClassesPath, - expectedJvmFlags, - expectedMainClass)); + entrypoint); + + // Checks that this is also the default entrypoint. + Assert.assertEquals( + JavaEntrypointBuilder.makeDefaultEntrypoint(expectedJvmFlags, expectedMainClass), + entrypoint); } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java index a09be631c5..29cde64bbf 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/MainClassFinderTest.java @@ -17,7 +17,6 @@ package com.google.cloud.tools.jib.frontend; import com.google.cloud.tools.jib.builder.BuildLogger; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import com.google.common.io.Resources; @@ -40,7 +39,6 @@ public class MainClassFinderTest { @Mock private BuildLogger mockBuildLogger; - @Mock private SourceFilesConfiguration mockSourceFilesConfiguration; @Mock private ProjectProperties mockProjectProperties; @Mock private HelpfulSuggestions mockHelpfulSuggestions; diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index e33fcf2885..441749d7f3 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -19,12 +19,12 @@ import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException; import com.google.cloud.tools.jib.configuration.CacheConfiguration; -import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.docker.DockerClient; import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException; import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; @@ -33,11 +33,7 @@ import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; import com.google.common.base.Preconditions; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.time.Instant; -import java.util.stream.Collectors; -import java.util.stream.Stream; import javax.annotation.Nullable; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; @@ -102,7 +98,8 @@ public void buildDocker() throws InvalidImageReferenceException, IOException { } GradleProjectProperties gradleProjectProperties = - GradleProjectProperties.getForProject(getProject(), gradleBuildLogger); + GradleProjectProperties.getForProject( + getProject(), gradleBuildLogger, jibExtension.getExtraDirectory().toPath()); String mainClass = gradleProjectProperties.getMainClass(jibExtension); ImageReference targetImage = gradleProjectProperties.getGeneratedTargetDockerTag(jibExtension, gradleBuildLogger); @@ -117,18 +114,11 @@ public void buildDocker() throws InvalidImageReferenceException, IOException { .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) .setMainClass(mainClass) .setJavaArguments(jibExtension.getArgs()) - .setJvmFlags(jibExtension.getJvmFlags()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) - .setAllowHttp(jibExtension.getAllowInsecureRegistries()); - if (Files.exists(jibExtension.getExtraDirectory().toPath())) { - try (Stream extraFilesLayerDirectoryFiles = - Files.list(jibExtension.getExtraDirectory().toPath())) { - buildConfigurationBuilder.setExtraFilesLayerConfiguration( - LayerConfiguration.builder() - .addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/") - .build()); - } - } + .setAllowHttp(jibExtension.getAllowInsecureRegistries()) + .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) + .setEntrypoint(JavaEntrypointBuilder + .makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index f7efcb400e..fe6c9c7c50 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -23,6 +23,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; @@ -105,7 +106,8 @@ public void buildImage() throws InvalidImageReferenceException, IOException { } GradleProjectProperties gradleProjectProperties = - GradleProjectProperties.getForProject(getProject(), gradleBuildLogger); + GradleProjectProperties.getForProject( + getProject(), gradleBuildLogger, jibExtension.getExtraDirectory().toPath()); String mainClass = gradleProjectProperties.getMainClass(jibExtension); // Builds the BuildConfiguration. @@ -119,11 +121,11 @@ public void buildImage() throws InvalidImageReferenceException, IOException { .setKnownTargetRegistryCredentials(knownTargetRegistryCredentials) .setMainClass(mainClass) .setJavaArguments(jibExtension.getArgs()) - .setJvmFlags(jibExtension.getJvmFlags()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setTargetFormat(jibExtension.getFormat()) .setAllowHttp(jibExtension.getAllowInsecureRegistries()) - .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()); + .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) + .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java index 64086da31a..f5246a861c 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java @@ -19,11 +19,11 @@ import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException; import com.google.cloud.tools.jib.configuration.CacheConfiguration; -import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException; import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; @@ -32,12 +32,8 @@ import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; import com.google.common.base.Preconditions; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import java.time.Instant; -import java.util.stream.Collectors; -import java.util.stream.Stream; import javax.annotation.Nullable; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; @@ -130,7 +126,8 @@ public void buildTar() throws InvalidImageReferenceException, IOException { } GradleProjectProperties gradleProjectProperties = - GradleProjectProperties.getForProject(getProject(), gradleBuildLogger); + GradleProjectProperties.getForProject( + getProject(), gradleBuildLogger, jibExtension.getExtraDirectory().toPath()); String mainClass = gradleProjectProperties.getMainClass(jibExtension); ImageReference targetImage = gradleProjectProperties.getGeneratedTargetDockerTag(jibExtension, gradleBuildLogger); @@ -145,18 +142,11 @@ public void buildTar() throws InvalidImageReferenceException, IOException { .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) .setMainClass(mainClass) .setJavaArguments(jibExtension.getArgs()) - .setJvmFlags(jibExtension.getJvmFlags()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) - .setAllowHttp(jibExtension.getAllowInsecureRegistries()); - if (Files.exists(jibExtension.getExtraDirectory().toPath())) { - try (Stream extraFilesLayerDirectoryFiles = - Files.list(jibExtension.getExtraDirectory().toPath())) { - buildConfigurationBuilder.setExtraFilesLayerConfiguration( - LayerConfiguration.builder() - .addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/") - .build()); - } - } + .setAllowHttp(jibExtension.getAllowInsecureRegistries()) + .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) + .setEntrypoint(JavaEntrypointBuilder + .makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( @@ -180,10 +170,7 @@ public void buildTar() throws InvalidImageReferenceException, IOException { // Uses a directory in the Gradle build cache as the Jib cache. try { - BuildStepsRunner.forBuildTar( - Paths.get(getTargetPath()), - buildConfiguration, - gradleProjectProperties.getSourceFilesConfiguration()) + BuildStepsRunner.forBuildTar(Paths.get(getTargetPath()), buildConfiguration) .build(HELPFUL_SUGGESTIONS); } catch (CacheDirectoryCreationException | BuildStepsExecutionException ex) { diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java index b7a8906bba..a5effd5953 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java @@ -104,7 +104,8 @@ public void generateDockerContext() { SystemPropertyValidator.checkHttpTimeoutProperty(GradleException::new); GradleProjectProperties gradleProjectProperties = - GradleProjectProperties.getForProject(getProject(), gradleBuildLogger); + GradleProjectProperties.getForProject( + getProject(), gradleBuildLogger, jibExtension.getExtraDirectory().toPath()); String mainClass = gradleProjectProperties.getMainClass(jibExtension); String targetDir = getTargetDir(); diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 2b026e5a5d..7ea0415f08 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -16,7 +16,7 @@ package com.google.cloud.tools.jib.gradle; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; @@ -43,11 +43,12 @@ class GradleLayerConfigurations { /** * Resolves the source files configuration for a Gradle {@link Project}. * - * @param project - * @param gradleBuildLogger - * @param extraDirectory - * @return - * @throws IOException + * @param project the Gradle {@link Project} + * @param gradleBuildLogger the build logger for providing feedback about the resolution + * @param extraDirectory path to the directory for the extra files layer + * @return a {@link GradleLayerConfigurations} for building the layers for the Gradle {@link + * Project} + * @throws IOException if an I/O exception occurred during resolution */ static GradleLayerConfigurations getForProject( Project project, GradleBuildLogger gradleBuildLogger, Path extraDirectory) @@ -121,18 +122,18 @@ static GradleLayerConfigurations getForProject( return new GradleLayerConfigurations( LayerConfiguration.builder() .addEntry( - dependenciesFiles, SourceFilesConfiguration.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .build(), LayerConfiguration.builder() .addEntry( snapshotDependenciesFiles, - SourceFilesConfiguration.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .build(), LayerConfiguration.builder() - .addEntry(resourcesFiles, SourceFilesConfiguration.DEFAULT_RESOURCES_PATH_ON_IMAGE) + .addEntry(resourcesFiles, JavaEntrypointBuilder.DEFAULT_RESOURCES_PATH_ON_IMAGE) .build(), LayerConfiguration.builder() - .addEntry(classesFiles, SourceFilesConfiguration.DEFAULT_CLASSES_PATH_ON_IMAGE) + .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) .build(), LayerConfiguration.builder().addEntry(extraFiles, "/").build()); } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java index c58c50f5d7..f93b2ea204 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java @@ -51,12 +51,12 @@ class GradleProjectProperties implements ProjectProperties { /** @return a GradleProjectProperties from the given project and logger. */ static GradleProjectProperties getForProject( - Project project, GradleBuildLogger gradleBuildLogger) { + Project project, GradleBuildLogger gradleBuildLogger, Path extraDirectory) { try { return new GradleProjectProperties( project, gradleBuildLogger, - GradleLayerConfigurations.getForProject(project, gradleBuildLogger)); + GradleLayerConfigurations.getForProject(project, gradleBuildLogger, extraDirectory)); } catch (IOException ex) { throw new GradleException("Obtaining project build output files failed", ex); diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java index 0f38d38665..6bb5711b78 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java @@ -111,7 +111,8 @@ public void setUp() throws URISyntaxException, IOException { Mockito.when(mockMainSourceSet.getRuntimeClasspath()).thenReturn(runtimeFileCollection); testGradleLayerConfigurations = - GradleLayerConfigurations.getForProject(mockProject, mockGradleBuildLogger, Paths.get("nonexistent/path")); + GradleLayerConfigurations.getForProject( + mockProject, mockGradleBuildLogger, Paths.get("nonexistent/path")); } @Test @@ -163,7 +164,8 @@ public void test_noClassesFiles() throws IOException { .thenReturn(new TestFileCollection(ImmutableSet.of(nonexistentFile))); testGradleLayerConfigurations = - GradleLayerConfigurations.getForProject(mockProject, mockGradleBuildLogger, Paths.get("nonexistent/path")); + GradleLayerConfigurations.getForProject( + mockProject, mockGradleBuildLogger, Paths.get("nonexistent/path")); Mockito.verify(mockGradleBuildLogger) .warn("Could not find build output directory '" + nonexistentFile + "'"); @@ -175,12 +177,15 @@ public void test_noClassesFiles() throws IOException { public void test_extraFiles() throws URISyntaxException, IOException { Path extraFilesDirectory = Paths.get(Resources.getResource("layer").toURI()); - testGradleLayerConfigurations = GradleLayerConfigurations.getForProject(mockProject, mockGradleBuildLogger, extraFilesDirectory); + testGradleLayerConfigurations = + GradleLayerConfigurations.getForProject( + mockProject, mockGradleBuildLogger, extraFilesDirectory); - ImmutableList expectedExtraFiles = ImmutableList.of( - Paths.get(Resources.getResource("layer/a/b/bar").toURI()), - Paths.get(Resources.getResource("layer/c/cat").toURI()), - Paths.get(Resources.getResource("layer/foo").toURI())); + ImmutableList expectedExtraFiles = + ImmutableList.of( + Paths.get(Resources.getResource("layer/a").toURI()), + Paths.get(Resources.getResource("layer/c").toURI()), + Paths.get(Resources.getResource("layer/foo").toURI())); Assert.assertEquals( expectedExtraFiles, diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java index 4363de831b..696a46f5d7 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleProjectPropertiesTest.java @@ -43,8 +43,9 @@ public class GradleProjectPropertiesTest { @Mock private Jar mockJar2; @Mock private Project mockProject; @Mock private GradleBuildLogger mockGradleBuildLogger; - @Mock private GradleLayerConfigurations mockGradleLayerConfigurations; @Mock private JibExtension mockJibExtension; + @Mock private GradleBuildLogger mockBuildLogger; + @Mock private GradleLayerConfigurations mockGradleLayerConfigurations; private Manifest manifest; private GradleProjectProperties gradleProjectProperties; @@ -69,7 +70,7 @@ public void testGetMainClassFromJar_success() { @Test public void testGetMainClassFromJar_missing() { Mockito.when(mockProject.getTasksByName("jar", false)).thenReturn(Collections.emptySet()); - Assert.assertEquals(null, gradleProjectProperties.getMainClassFromJar()); + Assert.assertNull(gradleProjectProperties.getMainClassFromJar()); } @Test @@ -77,17 +78,17 @@ public void testGetMainClassFromJar_multiple() { manifest.attributes(ImmutableMap.of("Main-Class", "some.main.class")); Mockito.when(mockProject.getTasksByName("jar", false)) .thenReturn(ImmutableSet.of(mockJar, mockJar2)); - Assert.assertEquals(null, gradleProjectProperties.getMainClassFromJar()); + Assert.assertNull(gradleProjectProperties.getMainClassFromJar()); } @Test public void testGetDockerTag_configured() throws InvalidImageReferenceException { Mockito.when(mockJibExtension.getTargetImage()).thenReturn("a/b:c"); ImageReference result = - gradleProjectProperties.getGeneratedTargetDockerTag(mockJibExtension, mockGradleBuildLogger); + gradleProjectProperties.getGeneratedTargetDockerTag(mockJibExtension, mockBuildLogger); Assert.assertEquals("a/b", result.getRepository()); Assert.assertEquals("c", result.getTag()); - Mockito.verify(mockGradleBuildLogger, Mockito.never()).lifecycle(Mockito.any()); + Mockito.verify(mockBuildLogger, Mockito.never()).lifecycle(Mockito.any()); } @Test @@ -96,10 +97,10 @@ public void testGetDockerTag_notConfigured() throws InvalidImageReferenceExcepti Mockito.when(mockProject.getVersion()).thenReturn("project-version"); Mockito.when(mockJibExtension.getTargetImage()).thenReturn(null); ImageReference result = - gradleProjectProperties.getGeneratedTargetDockerTag(mockJibExtension, mockGradleBuildLogger); + gradleProjectProperties.getGeneratedTargetDockerTag(mockJibExtension, mockBuildLogger); Assert.assertEquals("project-name", result.getRepository()); Assert.assertEquals("project-version", result.getTag()); - Mockito.verify(mockGradleBuildLogger) + Mockito.verify(mockBuildLogger) .lifecycle( "Tagging image with generated image reference project-name:project-version. If you'd " + "like to specify a different tag, you can set the jib.to.image parameter in your " From 36791acd65b07cf502d0a25b6e6a7b26e5a4b1b5 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Fri, 20 Jul 2018 12:44:11 -0400 Subject: [PATCH 06/19] Fixes tests. --- .../builder/BuildStepsIntegrationTest.java | 15 ++++++++--- .../tools/jib/builder/BuildConfiguration.java | 19 +------------- .../jib/builder/BuildConfigurationTest.java | 25 +++---------------- .../tools/jib/gradle/BuildDockerTask.java | 4 +-- .../tools/jib/gradle/BuildImageTask.java | 3 ++- .../cloud/tools/jib/gradle/BuildTarTask.java | 4 +-- .../jib/gradle/GradleLayerConfigurations.java | 8 +++--- 7 files changed, 24 insertions(+), 54 deletions(-) diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java index 0bfa0287e3..e69862db3a 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java @@ -23,6 +23,7 @@ import com.google.cloud.tools.jib.cache.Caches; import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.image.InvalidImageReferenceException; import com.google.cloud.tools.jib.registry.LocalRegistry; @@ -89,12 +90,14 @@ public void testSteps_forBuildToDockerRegistry() BuildConfiguration.builder(logger) .setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) .setTargetImage(ImageReference.of("localhost:5000", "testimage", "testtag")) - .setMainClass("HelloWorld") .setJavaArguments(Collections.singletonList("An argument.")) .setExposedPorts( ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp"))) .setAllowHttp(true) .setLayerConfigurations(fakeLayerConfigurations) + .setEntrypoint( + JavaEntrypointBuilder.makeDefaultEntrypoint( + Collections.emptyList(), "HelloWorld")) .build()); buildImageSteps.run(); @@ -129,10 +132,12 @@ public void testSteps_forBuildToDockerRegistry_dockerHubBaseImage() BuildConfiguration.builder(logger) .setBaseImage(ImageReference.parse("openjdk:8-jre-alpine")) .setTargetImage(ImageReference.of("localhost:5000", "testimage", "testtag")) - .setMainClass("HelloWorld") .setJavaArguments(Collections.singletonList("An argument.")) .setAllowHttp(true) .setLayerConfigurations(fakeLayerConfigurations) + .setEntrypoint( + JavaEntrypointBuilder.makeDefaultEntrypoint( + Collections.emptyList(), "HelloWorld")) .build()) .run(); @@ -150,11 +155,12 @@ public void testSteps_forBuildToDockerDaemon() BuildConfiguration.builder(logger) .setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) .setTargetImage(ImageReference.of(null, "testdocker", null)) - .setMainClass("HelloWorld") .setJavaArguments(Collections.singletonList("An argument.")) .setExposedPorts( ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp"))) .setLayerConfigurations(fakeLayerConfigurations) + .setEntrypoint( + JavaEntrypointBuilder.makeDefaultEntrypoint(Collections.emptyList(), "HelloWorld")) .build(); Path cacheDirectory = temporaryFolder.newFolder().toPath(); @@ -184,9 +190,10 @@ public void testSteps_forBuildToTarball() BuildConfiguration.builder(logger) .setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) .setTargetImage(ImageReference.of(null, "testtar", null)) - .setMainClass("HelloWorld") .setJavaArguments(Collections.singletonList("An argument.")) .setLayerConfigurations(fakeLayerConfigurations) + .setEntrypoint( + JavaEntrypointBuilder.makeDefaultEntrypoint(Collections.emptyList(), "HelloWorld")) .build(); Path outputPath = temporaryFolder.newFolder().toPath().resolve("test.tar"); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index 1272f36809..4b9a1093d6 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -49,7 +49,6 @@ public static class Builder { @Nullable private ImageReference targetImageReference; @Nullable private String targetImageCredentialHelperName; @Nullable private RegistryCredentials knownTargetRegistryCredentials; - @Nullable private String mainClass; private ImmutableList javaArguments = ImmutableList.of(); private ImmutableMap environmentMap = ImmutableMap.of(); private ImmutableList exposedPorts = ImmutableList.of(); @@ -98,11 +97,6 @@ public Builder setKnownTargetRegistryCredentials( return this; } - public Builder setMainClass(@Nullable String mainClass) { - this.mainClass = mainClass; - return this; - } - public Builder setJavaArguments(@Nullable List javaArguments) { if (javaArguments != null) { Preconditions.checkArgument(!javaArguments.contains(null)); @@ -215,13 +209,10 @@ public BuildConfiguration build() { if (targetImageReference == null) { errorMessages.add("target image is required but not set"); } - if (mainClass == null) { - errorMessages.add("main class is required but not set"); - } switch (errorMessages.size()) { case 0: // No errors - if (baseImageReference == null || targetImageReference == null || mainClass == null) { + if (baseImageReference == null || targetImageReference == null) { throw new IllegalStateException("Required fields should not be null"); } if (baseImageReference.usesDefaultTag()) { @@ -239,7 +230,6 @@ public BuildConfiguration build() { targetImageReference, targetImageCredentialHelperName, knownTargetRegistryCredentials, - mainClass, javaArguments, environmentMap, exposedPorts, @@ -299,7 +289,6 @@ public static Builder builder(BuildLogger buildLogger) { private final ImageReference targetImageReference; @Nullable private final String targetImageCredentialHelperName; @Nullable private final RegistryCredentials knownTargetRegistryCredentials; - private final String mainClass; private final ImmutableList javaArguments; private final ImmutableMap environmentMap; private final ImmutableList exposedPorts; @@ -320,7 +309,6 @@ private BuildConfiguration( ImageReference targetImageReference, @Nullable String targetImageCredentialHelperName, @Nullable RegistryCredentials knownTargetRegistryCredentials, - String mainClass, ImmutableList javaArguments, ImmutableMap environmentMap, ImmutableList exposedPorts, @@ -338,7 +326,6 @@ private BuildConfiguration( this.targetImageReference = targetImageReference; this.targetImageCredentialHelperName = targetImageCredentialHelperName; this.knownTargetRegistryCredentials = knownTargetRegistryCredentials; - this.mainClass = mainClass; this.javaArguments = javaArguments; this.environmentMap = environmentMap; this.exposedPorts = exposedPorts; @@ -410,10 +397,6 @@ public RegistryCredentials getKnownTargetRegistryCredentials() { return knownTargetRegistryCredentials; } - public String getMainClass() { - return mainClass; - } - public ImmutableList getJavaArguments() { return javaArguments; } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java index cb9313121c..b52b6c18fe 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java @@ -57,7 +57,6 @@ public void testBuilder() { String expectedTargetImageCredentialHelperName = "anotherCredentialHelper"; RegistryCredentials expectedKnownTargetRegistryCredentials = Mockito.mock(RegistryCredentials.class); - String expectedMainClass = "mainclass"; List expectedJavaArguments = Arrays.asList("arg1", "arg2"); Map expectedEnvironment = ImmutableMap.of("key", "value"); ImmutableList expectedExposedPorts = @@ -85,7 +84,6 @@ public void testBuilder() { expectedTargetServerUrl, expectedTargetImageName, expectedTargetTag)) .setTargetImageCredentialHelperName(expectedTargetImageCredentialHelperName) .setKnownTargetRegistryCredentials(expectedKnownTargetRegistryCredentials) - .setMainClass(expectedMainClass) .setJavaArguments(expectedJavaArguments) .setEnvironment(expectedEnvironment) .setExposedPorts(expectedExposedPorts) @@ -110,7 +108,6 @@ public void testBuilder() { Assert.assertEquals( expectedTargetImageCredentialHelperName, buildConfiguration.getTargetImageCredentialHelperName()); - Assert.assertEquals(expectedMainClass, buildConfiguration.getMainClass()); Assert.assertEquals(expectedJavaArguments, buildConfiguration.getJavaArguments()); Assert.assertEquals(expectedEnvironment, buildConfiguration.getEnvironment()); Assert.assertEquals(expectedExposedPorts, buildConfiguration.getExposedPorts()); @@ -135,7 +132,6 @@ public void testBuilder_default() { String expectedTargetServerUrl = "someotherserver"; String expectedTargetImageName = "targetimage"; String expectedTargetTag = "targettag"; - String expectedMainClass = "mainclass"; BuildConfiguration buildConfiguration = BuildConfiguration.builder(Mockito.mock(BuildLogger.class)) @@ -145,7 +141,6 @@ public void testBuilder_default() { .setTargetImage( ImageReference.of( expectedTargetServerUrl, expectedTargetImageName, expectedTargetTag)) - .setMainClass(expectedMainClass) .build(); Assert.assertEquals(buildConfiguration.getCreationTime(), Instant.EPOCH); @@ -166,29 +161,15 @@ public void testBuilder_default() { @Test public void testBuilder_missingValues() { - // Main class is missing + // Target image is missing try { BuildConfiguration.builder(Mockito.mock(BuildLogger.class)) .setBaseImage(Mockito.mock(ImageReference.class)) - .setTargetImage(Mockito.mock(ImageReference.class)) .build(); Assert.fail("Build configuration should not be built with missing values"); } catch (IllegalStateException ex) { - Assert.assertEquals("main class is required but not set", ex.getMessage()); - } - - // Main class and target image are missing - try { - BuildConfiguration.builder(Mockito.mock(BuildLogger.class)) - .setBaseImage(Mockito.mock(ImageReference.class)) - .build(); - Assert.fail("Build configuration should not be built with missing values"); - - } catch (IllegalStateException ex) { - Assert.assertEquals( - "target image is required but not set and main class is required but not set", - ex.getMessage()); + Assert.assertEquals("target image is required but not set", ex.getMessage()); } // All required fields missing @@ -198,7 +179,7 @@ public void testBuilder_missingValues() { } catch (IllegalStateException ex) { Assert.assertEquals( - "base image is required but not set, target image is required but not set, and main class is required but not set", + "base image is required but not set and target image is required but not set", ex.getMessage()); } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index 441749d7f3..3f43c09ae0 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -117,8 +117,8 @@ public void buildDocker() throws InvalidImageReferenceException, IOException { .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setAllowHttp(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointBuilder - .makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + .setEntrypoint( + JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index fe6c9c7c50..7214e668f7 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -125,7 +125,8 @@ public void buildImage() throws InvalidImageReferenceException, IOException { .setTargetFormat(jibExtension.getFormat()) .setAllowHttp(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + .setEntrypoint( + JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java index f5246a861c..8c512e8072 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java @@ -145,8 +145,8 @@ public void buildTar() throws InvalidImageReferenceException, IOException { .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setAllowHttp(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointBuilder - .makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + .setEntrypoint( + JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 7ea0415f08..8b51d59b2e 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -16,8 +16,8 @@ package com.google.cloud.tools.jib.gradle; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.configuration.LayerConfiguration; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import java.io.File; @@ -121,13 +121,11 @@ static GradleLayerConfigurations getForProject( return new GradleLayerConfigurations( LayerConfiguration.builder() - .addEntry( - dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .addEntry(dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .build(), LayerConfiguration.builder() .addEntry( - snapshotDependenciesFiles, - JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + snapshotDependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .build(), LayerConfiguration.builder() .addEntry(resourcesFiles, JavaEntrypointBuilder.DEFAULT_RESOURCES_PATH_ON_IMAGE) From fcde23c90dc9db064083cef5bbf7e7cb6a78c7eb Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Fri, 20 Jul 2018 12:59:53 -0400 Subject: [PATCH 07/19] Fixes jib-gradle-plugin. --- .../cloud/tools/jib/gradle/JibPluginIntegrationTest.java | 2 +- .../com/google/cloud/tools/jib/gradle/BuildDockerTask.java | 4 +--- .../com/google/cloud/tools/jib/gradle/BuildImageTask.java | 4 +--- .../java/com/google/cloud/tools/jib/gradle/BuildTarTask.java | 4 +--- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java index f9df14cbd8..75380d0085 100644 --- a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java +++ b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java @@ -246,7 +246,7 @@ public void testDockerContext() throws IOException, InterruptedException { + " \"2002/udp\": {},\n" + " \"2003/udp\": {}")); Assert.assertEquals( - "Hello, world. An argument.\n", new Command("docker", "run", imageName).run()); + "Hello, world. An argument.\nfoo\ncat\n", new Command("docker", "run", imageName).run()); // Checks that generating the Docker context again is skipped. BuildTask upToDateJibDockerContextTask = diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index 3f43c09ae0..333f90100a 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -32,7 +32,6 @@ import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; import com.google.common.base.Preconditions; -import java.io.IOException; import java.time.Instant; import javax.annotation.Nullable; import org.gradle.api.DefaultTask; @@ -75,7 +74,7 @@ public void setTargetImage(String targetImage) { } @TaskAction - public void buildDocker() throws InvalidImageReferenceException, IOException { + public void buildDocker() throws InvalidImageReferenceException { if (!new DockerClient().isDockerInstalled()) { throw new GradleException(HELPFUL_SUGGESTIONS.forDockerNotInstalled()); } @@ -112,7 +111,6 @@ public void buildDocker() throws InvalidImageReferenceException, IOException { .setTargetImage(targetImage) .setBaseImageCredentialHelperName(jibExtension.getFrom().getCredHelper()) .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) - .setMainClass(mainClass) .setJavaArguments(jibExtension.getArgs()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setAllowHttp(jibExtension.getAllowInsecureRegistries()) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index 7214e668f7..e47a9a9887 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -32,7 +32,6 @@ import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import java.io.IOException; import java.time.Instant; import javax.annotation.Nullable; import org.gradle.api.DefaultTask; @@ -75,7 +74,7 @@ public void setTargetImage(String targetImage) { } @TaskAction - public void buildImage() throws InvalidImageReferenceException, IOException { + public void buildImage() throws InvalidImageReferenceException { // Asserts required @Input parameters are not null. Preconditions.checkNotNull(jibExtension); GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger()); @@ -119,7 +118,6 @@ public void buildImage() throws InvalidImageReferenceException, IOException { .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) .setTargetImageCredentialHelperName(jibExtension.getTo().getCredHelper()) .setKnownTargetRegistryCredentials(knownTargetRegistryCredentials) - .setMainClass(mainClass) .setJavaArguments(jibExtension.getArgs()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setTargetFormat(jibExtension.getFormat()) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java index 8c512e8072..af44bc30f8 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java @@ -31,7 +31,6 @@ import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; import com.google.common.base.Preconditions; -import java.io.IOException; import java.nio.file.Paths; import java.time.Instant; import javax.annotation.Nullable; @@ -107,7 +106,7 @@ private String getTargetPath() { } @TaskAction - public void buildTar() throws InvalidImageReferenceException, IOException { + public void buildTar() throws InvalidImageReferenceException { // Asserts required @Input parameters are not null. Preconditions.checkNotNull(jibExtension); GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger()); @@ -140,7 +139,6 @@ public void buildTar() throws InvalidImageReferenceException, IOException { .setTargetImage(targetImage) .setBaseImageCredentialHelperName(jibExtension.getFrom().getCredHelper()) .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) - .setMainClass(mainClass) .setJavaArguments(jibExtension.getArgs()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setAllowHttp(jibExtension.getAllowInsecureRegistries()) From 06e5b46b6c8094c7057d4b38902626dd904a7e03 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Fri, 20 Jul 2018 14:37:25 -0400 Subject: [PATCH 08/19] Changes for jib-maven-plugin. --- .../jib/gradle/GradleLayerConfigurations.java | 2 + .../gradle/GradleLayerConfigurationsTest.java | 6 + .../tools/jib/maven/BuildDockerMojo.java | 31 +-- .../cloud/tools/jib/maven/BuildImageMojo.java | 31 +-- .../cloud/tools/jib/maven/BuildTarMojo.java | 30 +-- .../tools/jib/maven/DockerContextMojo.java | 13 +- .../jib/maven/JibPluginConfiguration.java | 3 +- .../jib/maven/MavenLayerConfigurations.java | 179 ++++++++++++++++++ .../jib/maven/MavenProjectProperties.java | 47 ++++- .../maven/MavenSourceFilesConfiguration.java | 136 ------------- .../DockerContextMojoIntegrationTest.java | 2 +- ...java => MavenLayerConfigurationsTest.java} | 55 ++++-- .../jib/maven/MavenProjectPropertiesTest.java | 15 +- 13 files changed, 303 insertions(+), 247 deletions(-) create mode 100644 jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java delete mode 100644 jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java rename jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/{MavenSourceFilesConfigurationTest.java => MavenLayerConfigurationsTest.java} (66%) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 8b51d59b2e..478ce983ed 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -142,6 +142,8 @@ static GradleLayerConfigurations getForProject( private final LayerConfiguration classesLayerConfiguration; private final LayerConfiguration extraFilesLayerConfiguration; + // TODO: Consolidate with MavenLayerConfigurations. + /** Instantiate with {@link #getForProject}. */ private GradleLayerConfigurations( LayerConfiguration dependenciesLayerConfiguration, LayerConfiguration snapshotDependenciesLayerConfiguration, diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java index 6bb5711b78..183053bbd2 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java @@ -197,10 +197,16 @@ public void test_correctPathsOnImage() { Assert.assertEquals( "/app/libs/", testGradleLayerConfigurations.getDependenciesLayerEntry().getExtractionPath()); + Assert.assertEquals( + "/app/libs/", + testGradleLayerConfigurations.getSnapshotDependenciesLayerEntry().getExtractionPath()); Assert.assertEquals( "/app/resources/", testGradleLayerConfigurations.getResourcesLayerEntry().getExtractionPath()); Assert.assertEquals( "/app/classes/", testGradleLayerConfigurations.getClassesLayerEntry().getExtractionPath()); + Assert.assertEquals( + "/", + testGradleLayerConfigurations.getExtraFilesLayerEntry().getExtractionPath()); } } diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java index f34f8c9528..b040297555 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java @@ -19,24 +19,19 @@ import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException; import com.google.cloud.tools.jib.configuration.CacheConfiguration; -import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.docker.DockerClient; import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException; import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.time.Instant; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.ResolutionScope; @@ -67,7 +62,7 @@ public void execute() throws MojoExecutionException { // Parses 'from' and 'to' into image reference. MavenProjectProperties mavenProjectProperties = - MavenProjectProperties.getForProject(getProject(), mavenBuildLogger); + MavenProjectProperties.getForProject(getProject(), mavenBuildLogger, getExtraDirectory()); ImageReference baseImage = parseImageReference(getBaseImage(), "from"); ImageReference targetImage = mavenProjectProperties.getGeneratedTargetDockerTag(getTargetImage(), mavenBuildLogger); @@ -94,24 +89,12 @@ public void execute() throws MojoExecutionException { .setBaseImageCredentialHelperName(getBaseImageCredentialHelperName()) .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) .setTargetImage(targetImage) - .setMainClass(mainClass) .setJavaArguments(getArgs()) - .setJvmFlags(getJvmFlags()) .setEnvironment(getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) - .setAllowHttp(getAllowInsecureRegistries()); - if (getExtraDirectory() != null && Files.exists(getExtraDirectory())) { - try (Stream extraFilesLayerDirectoryFiles = Files.list(getExtraDirectory())) { - buildConfigurationBuilder.setExtraFilesLayerConfiguration( - LayerConfiguration.builder() - .addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/") - .build()); - - } catch (IOException ex) { - throw new MojoExecutionException( - "Failed to list directory for extra files: " + getExtraDirectory(), ex); - } - } + .setAllowHttp(getAllowInsecureRegistries()) + .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) + .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( @@ -134,9 +117,7 @@ public void execute() throws MojoExecutionException { RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX); try { - BuildStepsRunner.forBuildToDockerDaemon( - buildConfiguration, mavenProjectProperties.getSourceFilesConfiguration()) - .build(HELPFUL_SUGGESTIONS); + BuildStepsRunner.forBuildToDockerDaemon(buildConfiguration).build(HELPFUL_SUGGESTIONS); getLog().info(""); } catch (CacheDirectoryCreationException | BuildStepsExecutionException ex) { diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java index 62a840f291..10037738e6 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java @@ -19,11 +19,11 @@ import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException; import com.google.cloud.tools.jib.configuration.CacheConfiguration; -import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException; import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageFormat; import com.google.cloud.tools.jib.image.ImageReference; @@ -32,13 +32,8 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.time.Instant; import java.util.Arrays; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; @@ -103,7 +98,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { mavenSettingsServerCredentials.retrieve(targetImage.getRegistry()); MavenProjectProperties mavenProjectProperties = - MavenProjectProperties.getForProject(getProject(), mavenBuildLogger); + MavenProjectProperties.getForProject(getProject(), mavenBuildLogger, getExtraDirectory()); String mainClass = mavenProjectProperties.getMainClass(this); // Builds the BuildConfiguration. @@ -115,25 +110,13 @@ public void execute() throws MojoExecutionException, MojoFailureException { .setTargetImage(targetImage) .setTargetImageCredentialHelperName(getTargetImageCredentialHelperName()) .setKnownTargetRegistryCredentials(knownTargetRegistryCredentials) - .setMainClass(mainClass) .setJavaArguments(getArgs()) - .setJvmFlags(getJvmFlags()) .setEnvironment(getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) .setTargetFormat(ImageFormat.valueOf(getFormat()).getManifestTemplateClass()) - .setAllowHttp(getAllowInsecureRegistries()); - if (getExtraDirectory() != null && Files.exists(getExtraDirectory())) { - try (Stream extraFilesLayerDirectoryFiles = Files.list(getExtraDirectory())) { - buildConfigurationBuilder.setExtraFilesLayerConfiguration( - LayerConfiguration.builder() - .addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/") - .build()); - - } catch (IOException ex) { - throw new MojoExecutionException( - "Failed to list directory for extra files: " + getExtraDirectory(), ex); - } - } + .setAllowHttp(getAllowInsecureRegistries()) + .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) + .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( @@ -156,9 +139,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX); try { - BuildStepsRunner.forBuildImage( - buildConfiguration, mavenProjectProperties.getSourceFilesConfiguration()) - .build(HELPFUL_SUGGESTIONS); + BuildStepsRunner.forBuildImage(buildConfiguration).build(HELPFUL_SUGGESTIONS); getLog().info(""); } catch (CacheDirectoryCreationException | BuildStepsExecutionException ex) { diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java index 4787261e18..2f165ed12c 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java @@ -19,24 +19,19 @@ import com.google.cloud.tools.jib.builder.BuildConfiguration; import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException; import com.google.cloud.tools.jib.configuration.CacheConfiguration; -import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException; import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import java.time.Instant; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.ResolutionScope; @@ -65,7 +60,7 @@ public void execute() throws MojoExecutionException { // Parses 'from' and 'to' into image reference. MavenProjectProperties mavenProjectProperties = - MavenProjectProperties.getForProject(getProject(), mavenBuildLogger); + MavenProjectProperties.getForProject(getProject(), mavenBuildLogger, getExtraDirectory()); ImageReference baseImage = parseImageReference(getBaseImage(), "from"); ImageReference targetImage = mavenProjectProperties.getGeneratedTargetDockerTag(getTargetImage(), mavenBuildLogger); @@ -92,24 +87,12 @@ public void execute() throws MojoExecutionException { .setBaseImageCredentialHelperName(getBaseImageCredentialHelperName()) .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) .setTargetImage(targetImage) - .setMainClass(mainClass) .setJavaArguments(getArgs()) - .setJvmFlags(getJvmFlags()) .setEnvironment(getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) - .setAllowHttp(getAllowInsecureRegistries()); - if (getExtraDirectory() != null && Files.exists(getExtraDirectory())) { - try (Stream extraFilesLayerDirectoryFiles = Files.list(getExtraDirectory())) { - buildConfigurationBuilder.setExtraFilesLayerConfiguration( - LayerConfiguration.builder() - .addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/") - .build()); - - } catch (IOException ex) { - throw new MojoExecutionException( - "Failed to list directory for extra files: " + getExtraDirectory(), ex); - } - } + .setAllowHttp(getAllowInsecureRegistries()) + .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) + .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( @@ -134,8 +117,7 @@ public void execute() throws MojoExecutionException { try { BuildStepsRunner.forBuildTar( Paths.get(getProject().getBuild().getDirectory()).resolve("jib-image.tar"), - buildConfiguration, - mavenProjectProperties.getSourceFilesConfiguration()) + buildConfiguration) .build(HELPFUL_SUGGESTIONS); getLog().info(""); diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java index 9911661fee..bff24b268a 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java @@ -26,7 +26,6 @@ import java.nio.file.Paths; import javax.annotation.Nullable; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; @@ -47,7 +46,7 @@ public class DockerContextMojo extends JibPluginConfiguration { private String targetDir; @Override - public void execute() throws MojoExecutionException, MojoFailureException { + public void execute() throws MojoExecutionException { MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog()); handleDeprecatedParameters(mavenBuildLogger); SystemPropertyValidator.checkHttpTimeoutProperty(MojoExecutionException::new); @@ -55,7 +54,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { Preconditions.checkNotNull(targetDir); MavenProjectProperties mavenProjectProperties = - MavenProjectProperties.getForProject(getProject(), mavenBuildLogger); + MavenProjectProperties.getForProject(getProject(), mavenBuildLogger, getExtraDirectory()); String mainClass = mavenProjectProperties.getMainClass(this); try { @@ -63,8 +62,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { // here. ExposedPortsParser.parse(getExposedPorts()); - // TODO: Add support for extra files layer. - new DockerContextGenerator(mavenProjectProperties.getSourceFilesConfiguration()) + new DockerContextGenerator( + mavenProjectProperties.getDependenciesLayerEntry(), + mavenProjectProperties.getSnapshotDependenciesLayerEntry(), + mavenProjectProperties.getResourcesLayerEntry(), + mavenProjectProperties.getClassesLayerEntry(), + mavenProjectProperties.getExtraFilesLayerEntry()) .setBaseImage(getBaseImage()) .setJvmFlags(getJvmFlags()) .setMainClass(mainClass) diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java index 28b16c082c..59dfa41ed1 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java @@ -231,10 +231,9 @@ boolean getAllowInsecureRegistries() { return allowInsecureRegistries; } - @Nullable Path getExtraDirectory() { // TODO: Should inform user about nonexistent directory if using custom directory. - return Paths.get(extraDirectory); + return Paths.get(Preconditions.checkNotNull(extraDirectory)); } @VisibleForTesting diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java new file mode 100644 index 0000000000..7196bc1661 --- /dev/null +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java @@ -0,0 +1,179 @@ +/* + * Copyright 2018 Google LLC. All rights reserved. + * + * 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 + * + * http://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 com.google.cloud.tools.jib.maven; + +import com.google.cloud.tools.jib.configuration.LayerConfiguration; +import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.image.LayerEntry; +import com.google.common.collect.ImmutableList; +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.project.MavenProject; + +/** Builds {@link LayerConfiguration}s based on inputs from a {@link MavenProject}. */ +class MavenLayerConfigurations { + + /** + * Resolves the source files configuration for a Maven {@link MavenProject}. + * + * @param project the {@link MavenProject} + * @param extraDirectory path to the directory for the extra files layer + * @return a new {@link MavenLayerConfigurations} for the project + * @throws IOException if collecting the project files fails + */ + static MavenLayerConfigurations getForProject(MavenProject project, Path extraDirectory) + throws IOException { + Path classesSourceDirectory = Paths.get(project.getBuild().getSourceDirectory()); + Path classesOutputDirectory = Paths.get(project.getBuild().getOutputDirectory()); + + List dependenciesFiles = new ArrayList<>(); + List snapshotDependenciesFiles = new ArrayList<>(); + List resourcesFiles = new ArrayList<>(); + List classesFiles = new ArrayList<>(); + List extraFiles = new ArrayList<>(); + + // Gets all the dependencies. + for (Artifact artifact : project.getArtifacts()) { + if (artifact.isSnapshot()) { + snapshotDependenciesFiles.add(artifact.getFile().toPath()); + } else { + dependenciesFiles.add(artifact.getFile().toPath()); + } + } + + // Gets the classes files in the 'classes' output directory. It finds the files that are classes + // files by matching them against the .java source files. All other files are deemed resources. + try (Stream classFileStream = Files.list(classesOutputDirectory)) { + classFileStream.forEach( + classFile -> { + /* + * Adds classFile to classesFiles if it is a .class file or is a directory that also + * exists in the classes source directory; otherwise, adds file to resourcesFiles. + */ + if (Files.isDirectory(classFile) + && Files.exists( + classesSourceDirectory.resolve(classesOutputDirectory.relativize(classFile)))) { + classesFiles.add(classFile); + return; + } + + if (FileSystems.getDefault().getPathMatcher("glob:**.class").matches(classFile)) { + classesFiles.add(classFile); + return; + } + + resourcesFiles.add(classFile); + }); + } + + // Adds all the extra files. + if (Files.exists(extraDirectory)) { + try (Stream extraFilesLayerDirectoryFiles = Files.list(extraDirectory)) { + extraFiles = extraFilesLayerDirectoryFiles.collect(Collectors.toList()); + + } catch (IOException ex) { + throw new IOException("Failed to list directory for extra files: " + extraDirectory, ex); + } + } + + // Sort all files by path for consistent ordering. + Collections.sort(dependenciesFiles); + Collections.sort(snapshotDependenciesFiles); + Collections.sort(resourcesFiles); + Collections.sort(classesFiles); + Collections.sort(extraFiles); + + return new MavenLayerConfigurations( + LayerConfiguration.builder() + .addEntry(dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .build(), + LayerConfiguration.builder() + .addEntry( + snapshotDependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .build(), + LayerConfiguration.builder() + .addEntry(resourcesFiles, JavaEntrypointBuilder.DEFAULT_RESOURCES_PATH_ON_IMAGE) + .build(), + LayerConfiguration.builder() + .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) + .build(), + LayerConfiguration.builder().addEntry(extraFiles, "/").build()); + } + + private final LayerConfiguration dependenciesLayerConfiguration; + private final LayerConfiguration snapshotDependenciesLayerConfiguration; + private final LayerConfiguration resourcesLayerConfiguration; + private final LayerConfiguration classesLayerConfiguration; + private final LayerConfiguration extraFilesLayerConfiguration; + + /** Instantiate with {@link #getForProject}. */ + private MavenLayerConfigurations( + LayerConfiguration dependenciesLayerConfiguration, + LayerConfiguration snapshotDependenciesLayerConfiguration, + LayerConfiguration resourcesLayerConfiguration, + LayerConfiguration classesLayerConfiguration, + LayerConfiguration extraFilesLayerConfiguration) { + this.dependenciesLayerConfiguration = dependenciesLayerConfiguration; + this.snapshotDependenciesLayerConfiguration = snapshotDependenciesLayerConfiguration; + this.resourcesLayerConfiguration = resourcesLayerConfiguration; + this.classesLayerConfiguration = classesLayerConfiguration; + this.extraFilesLayerConfiguration = extraFilesLayerConfiguration; + } + + /** + * Gets the list of {@link LayerConfiguration}s to use to build the container image. + * + * @return the list of {@link LayerConfiguration}s + */ + ImmutableList getLayerConfigurations() { + return ImmutableList.of( + dependenciesLayerConfiguration, + snapshotDependenciesLayerConfiguration, + resourcesLayerConfiguration, + classesLayerConfiguration, + extraFilesLayerConfiguration); + } + + LayerEntry getDependenciesLayerEntry() { + return dependenciesLayerConfiguration.getLayerEntries().get(0); + } + + LayerEntry getSnapshotDependenciesLayerEntry() { + return snapshotDependenciesLayerConfiguration.getLayerEntries().get(0); + } + + LayerEntry getResourcesLayerEntry() { + return resourcesLayerConfiguration.getLayerEntries().get(0); + } + + LayerEntry getClassesLayerEntry() { + return classesLayerConfiguration.getLayerEntries().get(0); + } + + LayerEntry getExtraFilesLayerEntry() { + return extraFilesLayerConfiguration.getLayerEntries().get(0); + } +} diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java index 45239fc581..7b2c175cec 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java @@ -17,14 +17,16 @@ package com.google.cloud.tools.jib.maven; import com.google.cloud.tools.jib.builder.BuildLogger; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; import com.google.cloud.tools.jib.frontend.MainClassFinder; import com.google.cloud.tools.jib.frontend.MainClassInferenceException; import com.google.cloud.tools.jib.frontend.ProjectProperties; import com.google.cloud.tools.jib.image.ImageReference; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; @@ -43,14 +45,18 @@ class MavenProjectProperties implements ProjectProperties { /** * @param project the {@link MavenProject} for the plugin. * @param mavenBuildLogger the logger used for printing status messages. + * @param extraDirectory path to the directory for the extra files layer * @return a MavenProjectProperties from the given project and logger. * @throws MojoExecutionException if no class files are found in the output directory. */ static MavenProjectProperties getForProject( - MavenProject project, MavenBuildLogger mavenBuildLogger) throws MojoExecutionException { + MavenProject project, MavenBuildLogger mavenBuildLogger, Path extraDirectory) + throws MojoExecutionException { try { return new MavenProjectProperties( - project, mavenBuildLogger, MavenSourceFilesConfiguration.getForProject(project)); + project, + mavenBuildLogger, + MavenLayerConfigurations.getForProject(project, extraDirectory)); } catch (IOException ex) { throw new MojoExecutionException( "Obtaining project build output files failed; make sure you have compiled your project " @@ -62,21 +68,46 @@ static MavenProjectProperties getForProject( private final MavenProject project; private final MavenBuildLogger mavenBuildLogger; - private final SourceFilesConfiguration sourceFilesConfiguration; + private final MavenLayerConfigurations mavenLayerConfigurations; @VisibleForTesting MavenProjectProperties( MavenProject project, MavenBuildLogger mavenBuildLogger, - SourceFilesConfiguration sourceFilesConfiguration) { + MavenLayerConfigurations mavenLayerConfigurations) { this.project = project; this.mavenBuildLogger = mavenBuildLogger; - this.sourceFilesConfiguration = sourceFilesConfiguration; + this.mavenLayerConfigurations = mavenLayerConfigurations; } @Override - public SourceFilesConfiguration getSourceFilesConfiguration() { - return sourceFilesConfiguration; + public ImmutableList getLayerConfigurations() { + return mavenLayerConfigurations.getLayerConfigurations(); + } + + @Override + public LayerEntry getDependenciesLayerEntry() { + return mavenLayerConfigurations.getDependenciesLayerEntry(); + } + + @Override + public LayerEntry getSnapshotDependenciesLayerEntry() { + return mavenLayerConfigurations.getSnapshotDependenciesLayerEntry(); + } + + @Override + public LayerEntry getResourcesLayerEntry() { + return mavenLayerConfigurations.getResourcesLayerEntry(); + } + + @Override + public LayerEntry getClassesLayerEntry() { + return mavenLayerConfigurations.getClassesLayerEntry(); + } + + @Override + public LayerEntry getExtraFilesLayerEntry() { + return mavenLayerConfigurations.getExtraFilesLayerEntry(); } @Override diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java deleted file mode 100644 index a3d919eaf5..0000000000 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright 2018 Google LLC. All rights reserved. - * - * 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 - * - * http://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 com.google.cloud.tools.jib.maven; - -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; -import com.google.common.collect.ImmutableList; -import java.io.IOException; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Stream; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.project.MavenProject; - -/** {@link SourceFilesConfiguration} implementation based on inputs from a {@link MavenProject}. */ -class MavenSourceFilesConfiguration implements SourceFilesConfiguration { - - /** - * Resolves the source files configuration for a Maven {@link MavenProject}. - * - * @param project the {@link MavenProject} - * @return a new {@link MavenSourceFilesConfiguration} for the project - * @throws IOException if collecting the project files fails - */ - static MavenSourceFilesConfiguration getForProject(MavenProject project) throws IOException { - return new MavenSourceFilesConfiguration(project); - } - - private final ImmutableList dependenciesFiles; - private final ImmutableList snapshotDependenciesFiles; - private final ImmutableList resourcesFiles; - private final ImmutableList classesFiles; - - /** Instantiate with {@link #getForProject}. */ - private MavenSourceFilesConfiguration(MavenProject project) throws IOException { - Path classesSourceDirectory = Paths.get(project.getBuild().getSourceDirectory()); - Path classesOutputDirectory = Paths.get(project.getBuild().getOutputDirectory()); - - List dependenciesFiles = new ArrayList<>(); - List snapshotDependenciesFiles = new ArrayList<>(); - List resourcesFiles = new ArrayList<>(); - List classesFiles = new ArrayList<>(); - - // Gets all the dependencies. - for (Artifact artifact : project.getArtifacts()) { - if (artifact.isSnapshot()) { - snapshotDependenciesFiles.add(artifact.getFile().toPath()); - } else { - dependenciesFiles.add(artifact.getFile().toPath()); - } - } - - // Gets the classes files in the 'classes' output directory. It finds the files that are classes - // files by matching them against the .java source files. All other files are deemed resources. - try (Stream classFileStream = Files.list(classesOutputDirectory)) { - classFileStream.forEach( - classFile -> { - /* - * Adds classFile to classesFiles if it is a .class file or is a directory that also - * exists in the classes source directory; otherwise, adds file to resourcesFiles. - */ - if (Files.isDirectory(classFile) - && Files.exists( - classesSourceDirectory.resolve(classesOutputDirectory.relativize(classFile)))) { - classesFiles.add(classFile); - return; - } - - if (FileSystems.getDefault().getPathMatcher("glob:**.class").matches(classFile)) { - classesFiles.add(classFile); - return; - } - - resourcesFiles.add(classFile); - }); - } - - // Sort all files by path for consistent ordering. - this.dependenciesFiles = ImmutableList.sortedCopyOf(dependenciesFiles); - this.snapshotDependenciesFiles = ImmutableList.sortedCopyOf(snapshotDependenciesFiles); - this.resourcesFiles = ImmutableList.sortedCopyOf(resourcesFiles); - this.classesFiles = ImmutableList.sortedCopyOf(classesFiles); - } - - @Override - public ImmutableList getDependenciesFiles() { - return dependenciesFiles; - } - - @Override - public ImmutableList getSnapshotDependenciesFiles() { - return snapshotDependenciesFiles; - } - - @Override - public ImmutableList getResourcesFiles() { - return resourcesFiles; - } - - @Override - public ImmutableList getClassesFiles() { - return classesFiles; - } - - @Override - public String getDependenciesPathOnImage() { - return DEFAULT_DEPENDENCIES_PATH_ON_IMAGE; - } - - @Override - public String getResourcesPathOnImage() { - return DEFAULT_RESOURCES_PATH_ON_IMAGE; - } - - @Override - public String getClassesPathOnImage() { - return DEFAULT_CLASSES_PATH_ON_IMAGE; - } -} diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/DockerContextMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/DockerContextMojoIntegrationTest.java index 8929ea48b9..66b6a7b190 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/DockerContextMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/DockerContextMojoIntegrationTest.java @@ -62,6 +62,6 @@ public void testExecute() throws VerificationException, IOException, Interrupted + " \"2003/udp\": {}")); Assert.assertEquals( - "Hello, world. An argument.\n", new Command("docker", "run", imageName).run()); + "Hello, world. An argument.\nfoo\ncat\n", new Command("docker", "run", imageName).run()); } } diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurationsTest.java similarity index 66% rename from jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java rename to jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurationsTest.java index cecf83e1f2..e9fb8177e7 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurationsTest.java @@ -26,6 +26,7 @@ import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Build; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import org.junit.Assert; import org.junit.Before; @@ -36,19 +37,19 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -/** Tests for {@link MavenSourceFilesConfiguration}. */ +/** Tests for {@link MavenLayerConfigurations}. */ @RunWith(MockitoJUnitRunner.class) -public class MavenSourceFilesConfigurationTest { +public class MavenLayerConfigurationsTest { @Rule public TestRepository testRepository = new TestRepository(); @Mock private MavenProject mockMavenProject; @Mock private Build mockBuild; - private MavenSourceFilesConfiguration testMavenSourceFilesConfiguration; + private MavenLayerConfigurations testMavenLayerConfigurations; @Before - public void setUp() throws IOException, URISyntaxException { + public void setUp() throws IOException, URISyntaxException, MojoExecutionException { Path sourcePath = Paths.get(Resources.getResource("application/source").toURI()); Path outputPath = Paths.get(Resources.getResource("application/output").toURI()); @@ -66,8 +67,8 @@ public void setUp() throws IOException, URISyntaxException { testRepository.findArtifact("com.test", "dependencyX", "1.0.0-SNAPSHOT")); Mockito.when(mockMavenProject.getArtifacts()).thenReturn(artifacts); - testMavenSourceFilesConfiguration = - MavenSourceFilesConfiguration.getForProject(mockMavenProject); + testMavenLayerConfigurations = + MavenLayerConfigurations.getForProject(mockMavenProject, Paths.get("nonexistent/path")); } @Test @@ -95,22 +96,50 @@ public void test_correctFiles() throws URISyntaxException { Paths.get(Resources.getResource("application/output/some.class").toURI())); Assert.assertEquals( - expectedDependenciesFiles, testMavenSourceFilesConfiguration.getDependenciesFiles()); + expectedDependenciesFiles, + testMavenLayerConfigurations.getDependenciesLayerEntry().getSourceFiles()); Assert.assertEquals( expectedSnapshotDependenciesFiles, - testMavenSourceFilesConfiguration.getSnapshotDependenciesFiles()); + testMavenLayerConfigurations.getSnapshotDependenciesLayerEntry().getSourceFiles()); Assert.assertEquals( - expectedResourcesFiles, testMavenSourceFilesConfiguration.getResourcesFiles()); - Assert.assertEquals(expectedClassesFiles, testMavenSourceFilesConfiguration.getClassesFiles()); + expectedResourcesFiles, + testMavenLayerConfigurations.getResourcesLayerEntry().getSourceFiles()); + Assert.assertEquals( + expectedClassesFiles, testMavenLayerConfigurations.getClassesLayerEntry().getSourceFiles()); + } + + @Test + public void test_extraFiles() throws URISyntaxException, IOException, MojoExecutionException { + Path extraFilesDirectory = Paths.get(Resources.getResource("layer").toURI()); + + testMavenLayerConfigurations = + MavenLayerConfigurations.getForProject(mockMavenProject, extraFilesDirectory); + + ImmutableList expectedExtraFiles = + ImmutableList.of( + Paths.get(Resources.getResource("layer/a").toURI()), + Paths.get(Resources.getResource("layer/c").toURI()), + Paths.get(Resources.getResource("layer/foo").toURI())); + + Assert.assertEquals( + expectedExtraFiles, + testMavenLayerConfigurations.getExtraFilesLayerEntry().getSourceFiles()); } @Test public void test_correctPathsOnImage() { Assert.assertEquals( - "/app/libs/", testMavenSourceFilesConfiguration.getDependenciesPathOnImage()); + "/app/libs/", testMavenLayerConfigurations.getDependenciesLayerEntry().getExtractionPath()); + Assert.assertEquals( + "/app/libs/", + testMavenLayerConfigurations.getSnapshotDependenciesLayerEntry().getExtractionPath()); + Assert.assertEquals( + "/app/resources/", + testMavenLayerConfigurations.getResourcesLayerEntry().getExtractionPath()); + Assert.assertEquals( + "/app/classes/", testMavenLayerConfigurations.getClassesLayerEntry().getExtractionPath()); Assert.assertEquals( - "/app/resources/", testMavenSourceFilesConfiguration.getResourcesPathOnImage()); - Assert.assertEquals("/app/classes/", testMavenSourceFilesConfiguration.getClassesPathOnImage()); + "/", testMavenLayerConfigurations.getExtraFilesLayerEntry().getExtractionPath()); } private Artifact makeArtifact(Path path) { diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java index 8bc97e4fd6..d77341aea9 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java @@ -16,7 +16,6 @@ package com.google.cloud.tools.jib.maven; -import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.image.ImageReference; import org.apache.maven.model.Plugin; import org.apache.maven.project.MavenProject; @@ -35,7 +34,7 @@ public class MavenProjectPropertiesTest { @Mock private MavenProject mockMavenProject; @Mock private MavenBuildLogger mockMavenBuildLogger; - @Mock private SourceFilesConfiguration mockSourcesFilesConfiguration; + @Mock private MavenLayerConfigurations mockMavenLayerConfigurations; @Mock private Plugin mockJarPlugin; @Mock private MavenBuildLogger mockBuildLogger; @@ -52,7 +51,7 @@ public void setup() { Mockito.when(mockMavenProject.getVersion()).thenReturn("project-version"); mavenProjectProperties = new MavenProjectProperties( - mockMavenProject, mockMavenBuildLogger, mockSourcesFilesConfiguration); + mockMavenProject, mockMavenBuildLogger, mockMavenLayerConfigurations); jarPluginConfiguration = new Xpp3Dom(""); archive = new Xpp3Dom("archive"); manifest = new Xpp3Dom("manifest"); @@ -80,7 +79,7 @@ public void testGetMainClassFromJar_missingMainClass() { jarPluginConfiguration.addChild(archive); archive.addChild(manifest); - Assert.assertEquals(null, mavenProjectProperties.getMainClassFromJar()); + Assert.assertNull(mavenProjectProperties.getMainClassFromJar()); } @Test @@ -90,7 +89,7 @@ public void testGetMainClassFromJar_missingManifest() { Mockito.when(mockJarPlugin.getConfiguration()).thenReturn(jarPluginConfiguration); jarPluginConfiguration.addChild(archive); - Assert.assertEquals(null, mavenProjectProperties.getMainClassFromJar()); + Assert.assertNull(mavenProjectProperties.getMainClassFromJar()); } @Test @@ -99,7 +98,7 @@ public void testGetMainClassFromJar_missingArchive() { .thenReturn(mockJarPlugin); Mockito.when(mockJarPlugin.getConfiguration()).thenReturn(jarPluginConfiguration); - Assert.assertEquals(null, mavenProjectProperties.getMainClassFromJar()); + Assert.assertNull(mavenProjectProperties.getMainClassFromJar()); } @Test @@ -107,12 +106,12 @@ public void testGetMainClassFromJar_missingConfiguration() { Mockito.when(mockMavenProject.getPlugin("org.apache.maven.plugins:maven-jar-plugin")) .thenReturn(mockJarPlugin); - Assert.assertEquals(null, mavenProjectProperties.getMainClassFromJar()); + Assert.assertNull(mavenProjectProperties.getMainClassFromJar()); } @Test public void testGetMainClassFromJar_missingPlugin() { - Assert.assertEquals(null, mavenProjectProperties.getMainClassFromJar()); + Assert.assertNull(mavenProjectProperties.getMainClassFromJar()); } @Test From 7162e85753623dc3e8432a106cf98899fdb4262f Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Sat, 21 Jul 2018 19:35:41 -0400 Subject: [PATCH 09/19] Simplifies DockerContextGenerator and labels layers to avoid indexing. --- .../tools/jib/builder/BuildConfiguration.java | 4 +- .../jib/configuration/LayerConfiguration.java | 2 +- .../jib/docker/DockerContextGenerator.java | 130 +++++++++++------- .../tools/jib/frontend/BuildStepsRunner.java | 45 ++---- .../jib/gradle/GradleLayerConfigurations.java | 12 +- .../gradle/GradleLayerConfigurationsTest.java | 3 +- .../jib/maven/MavenLayerConfigurations.java | 12 +- 7 files changed, 124 insertions(+), 84 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index 4b9a1093d6..5a5c4c3917 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -188,7 +188,7 @@ public Builder setCreationTime(Instant creationTime) { /** * Sets the container entrypoint. * - * @param entrypoint the command to run when the container starts + * @param entrypoint the tokenized command to run when the container starts * @return this */ public Builder setEntrypoint(@Nullable List entrypoint) { @@ -443,7 +443,7 @@ public boolean getAllowHttp() { } /** - * Gets the configurations for building the container. + * Gets the configurations for building the layers. * * @return the list of layer configurations */ diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java index 3a247cc598..9e194ee834 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java @@ -34,7 +34,7 @@ public static class Builder { private Builder() {} /** - * Sets an optional label for this layer. + * Sets a label for this layer. * * @param label the label * @return this diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java index ed653685d7..4bbd1282e7 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java @@ -23,6 +23,7 @@ import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import com.google.common.io.MoreFiles; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -36,21 +37,67 @@ /** * Generates a Docker context that mimics how Jib builds the image. * - *

The image consists of a base image layer and three application layers under the directories: + *

The image consists of a base image layer and 5 application layers under the directories: * *

    - *
  • libs/ (dependency jars) - *
  • resources/ - *
  • classes + *
  • {@code libs/} (dependency jars) + *
  • {@code snapshot-libs/} (snapshot dependency jars) + *
  • {@code resources/} (resource files) + *
  • {@code classes/} ({@code .class} files) + *
  • {@code root/} (extra files) *
+ * + * Empty application layers are omitted. */ public class DockerContextGenerator { - private final LayerEntry dependenciesLayerEntry; - private final LayerEntry snapshotDependenciesLayerEntry; - private final LayerEntry resourcesLayerEntry; - private final LayerEntry classesLayerEntry; - private final LayerEntry extraFilesLayerEntry; + private static final String DEPENDENCIES_LAYER_DIRECTORY = "libs"; + private static final String SNAPSHOT_DEPENDENCIES_LAYER_DIRECTORY = "snapshot-libs"; + private static final String RESOURCES_LAYER_DIRECTORY = "resources"; + private static final String CLASSES_LAYER_DIRECTORY = "classes"; + private static final String EXTRA_FILES_LAYER_DIRECTORY = "root"; + + /** Represents a Dockerfile {@code COPY} directive. */ + private static class CopyDirective { + + /** The source files to put into the context. */ + private final ImmutableList sourceFiles; + + /** The directory in the context to put the source files for the layer */ + private final String directoryInContext; + + /** The extraction path in the image. */ + private final String extractionPath; + + private CopyDirective( + ImmutableList sourceFiles, String directoryInContext, String extractionPath) { + this.sourceFiles = sourceFiles; + this.directoryInContext = directoryInContext; + this.extractionPath = extractionPath; + } + } + + /** + * Adds a copy directive for the {@code layerEntry} if it's not empty. + * + * @param listBuilder the {@link ImmutableList.Builder} to add to + * @param layerEntry the layer entry + * @param directoryInContext the directory in the context to put the source files for the layer + */ + private static void addIfNotEmpty( + ImmutableList.Builder listBuilder, + LayerEntry layerEntry, + String directoryInContext) { + if (layerEntry.getSourceFiles().isEmpty()) { + return; + } + + listBuilder.add( + new CopyDirective( + layerEntry.getSourceFiles(), directoryInContext, layerEntry.getExtractionPath())); + } + + private final ImmutableList copyDirectives; @Nullable private String baseImage; private List jvmFlags = Collections.emptyList(); @@ -65,11 +112,16 @@ public DockerContextGenerator( LayerEntry resourcesLayerEntry, LayerEntry classesLayerEntry, LayerEntry extraFilesLayerEntry) { - this.dependenciesLayerEntry = dependenciesLayerEntry; - this.snapshotDependenciesLayerEntry = snapshotDependenciesLayerEntry; - this.resourcesLayerEntry = resourcesLayerEntry; - this.classesLayerEntry = classesLayerEntry; - this.extraFilesLayerEntry = extraFilesLayerEntry; + ImmutableList.Builder copyDirectivesBuilder = ImmutableList.builder(); + addIfNotEmpty(copyDirectivesBuilder, dependenciesLayerEntry, DEPENDENCIES_LAYER_DIRECTORY); + addIfNotEmpty( + copyDirectivesBuilder, + snapshotDependenciesLayerEntry, + SNAPSHOT_DEPENDENCIES_LAYER_DIRECTORY); + addIfNotEmpty(copyDirectivesBuilder, resourcesLayerEntry, RESOURCES_LAYER_DIRECTORY); + addIfNotEmpty(copyDirectivesBuilder, classesLayerEntry, CLASSES_LAYER_DIRECTORY); + addIfNotEmpty(copyDirectivesBuilder, extraFilesLayerEntry, EXTRA_FILES_LAYER_DIRECTORY); + copyDirectives = copyDirectivesBuilder.build(); } /** @@ -149,24 +201,14 @@ public void generate(Path targetDirectory) throws IOException { Files.createDirectory(targetDirectory); - // Creates the directories. - Path dependenciesDir = targetDirectory.resolve("libs"); - Path snapshotDependenciesDir = targetDirectory.resolve("snapshot-libs"); - Path resourcesDIr = targetDirectory.resolve("resources"); - Path classesDir = targetDirectory.resolve("classes"); - Path extraFilesDir = targetDirectory.resolve("root"); - Files.createDirectory(dependenciesDir); - Files.createDirectory(snapshotDependenciesDir); - Files.createDirectory(resourcesDIr); - Files.createDirectory(classesDir); - Files.createDirectory(extraFilesDir); - - // Copies dependencies. - FileOperations.copy(dependenciesLayerEntry.getSourceFiles(), dependenciesDir); - FileOperations.copy(snapshotDependenciesLayerEntry.getSourceFiles(), snapshotDependenciesDir); - FileOperations.copy(resourcesLayerEntry.getSourceFiles(), resourcesDIr); - FileOperations.copy(classesLayerEntry.getSourceFiles(), classesDir); - FileOperations.copy(extraFilesLayerEntry.getSourceFiles(), extraFilesDir); + for (CopyDirective copyDirective : copyDirectives) { + // Creates the directories. + Path directoryInContext = targetDirectory.resolve(copyDirective.directoryInContext); + Files.createDirectory(directoryInContext); + + // Copies dependencies. + FileOperations.copy(copyDirective.sourceFiles, directoryInContext); + } // Creates the Dockerfile. Files.write( @@ -180,8 +222,10 @@ public void generate(Path targetDirectory) throws IOException { * FROM [base image] * * COPY libs [path/to/dependencies] + * COPY snapshot-libs [path/to/dependencies] * COPY resources [path/to/resources] * COPY classes [path/to/classes] + * COPY root [path/to/classes] * * EXPOSE [port] * [More EXPOSE instructions, if necessary] @@ -195,21 +239,13 @@ public void generate(Path targetDirectory) throws IOException { String makeDockerfile() throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); StringBuilder dockerfile = new StringBuilder(); - dockerfile - .append("FROM ") - .append(Preconditions.checkNotNull(baseImage)) - .append("\n\nCOPY libs ") - .append(dependenciesLayerEntry.getExtractionPath()); - if (!snapshotDependenciesLayerEntry.getSourceFiles().isEmpty()) { - dockerfile.append("\nCOPY snapshot-libs ").append(dependenciesLayerEntry.getExtractionPath()); - } - dockerfile - .append("\nCOPY resources ") - .append(resourcesLayerEntry.getExtractionPath()) - .append("\nCOPY classes ") - .append(classesLayerEntry.getExtractionPath()); - if (!extraFilesLayerEntry.getSourceFiles().isEmpty()) { - dockerfile.append("\nCOPY root ").append(extraFilesLayerEntry.getExtractionPath()); + dockerfile.append("FROM ").append(Preconditions.checkNotNull(baseImage)).append("\n"); + for (CopyDirective copyDirective : copyDirectives) { + dockerfile + .append("\nCOPY ") + .append(copyDirective.directoryInContext) + .append(" ") + .append(copyDirective.extractionPath); } dockerfile.append("\n"); for (String port : exposedPorts) { diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java index e21b72fc43..4de038d24d 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java @@ -27,6 +27,7 @@ import com.google.cloud.tools.jib.cache.Caches; import com.google.cloud.tools.jib.cache.Caches.Initializer; import com.google.cloud.tools.jib.configuration.CacheConfiguration; +import com.google.cloud.tools.jib.configuration.LayerConfiguration; import com.google.cloud.tools.jib.registry.InsecureRegistryException; import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException; import com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException; @@ -154,6 +155,13 @@ private static void handleRegistryUnauthorizedException( } } + private static String capitalizeFirstLetter(String string) { + if (string.length() == 0) { + return string; + } + return Character.toUpperCase(string.charAt(0)) + string.substring(1); + } + private final BuildSteps buildSteps; @VisibleForTesting @@ -178,36 +186,13 @@ public void build(HelpfulSuggestions helpfulSuggestions) throws BuildStepsExecut // Logs the different source files used. buildLogger.info("Containerizing application with the following files:"); - buildLogger.info("\tClasses:"); - // TODO: IMPORTANT: Don't use the indexes. - buildSteps - .getBuildConfiguration() - .getLayerConfigurations() - .get(2) - .getLayerEntries() - .get(0) - .getSourceFiles() - .forEach(classesFile -> buildLogger.info("\t\t" + classesFile)); - - buildLogger.info("\tResources:"); - buildSteps - .getBuildConfiguration() - .getLayerConfigurations() - .get(1) - .getLayerEntries() - .get(0) - .getSourceFiles() - .forEach(resourceFile -> buildLogger.info("\t\t" + resourceFile)); - - buildLogger.info("\tDependencies:"); - buildSteps - .getBuildConfiguration() - .getLayerConfigurations() - .get(0) - .getLayerEntries() - .get(0) - .getSourceFiles() - .forEach(dependencyFile -> buildLogger.info("\t\t" + dependencyFile)); + for (LayerConfiguration layerConfiguration : + buildSteps.getBuildConfiguration().getLayerConfigurations()) { + buildLogger.info("\t" + capitalizeFirstLetter(layerConfiguration.getLabel()) + ":"); + for (Path sourceFile : layerConfiguration.getLayerEntries().get(0).getSourceFiles()) { + buildLogger.info("\t\t" + sourceFile); + } + } buildSteps.run(); diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 478ce983ed..e095025d5a 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -40,6 +40,12 @@ class GradleLayerConfigurations { /** Name of the `main` {@link SourceSet} to use as source files. */ private static final String MAIN_SOURCE_SET_NAME = "main"; + private static final String DEPENDENCIES_LAYER_LABEL = "dependencies"; + private static final String SNAPSHOT_DEPENDENCIES_LAYER_LABEL = "snapshot dependencies"; + private static final String RESOURCES_LAYER_LABEL = "resources"; + private static final String CLASSES_LAYER_LABEL = "classes"; + private static final String EXTRA_FILES_LAYER_LABEL = "extra files"; + /** * Resolves the source files configuration for a Gradle {@link Project}. * @@ -122,18 +128,22 @@ static GradleLayerConfigurations getForProject( return new GradleLayerConfigurations( LayerConfiguration.builder() .addEntry(dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .setLabel(DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry( snapshotDependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .setLabel(SNAPSHOT_DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry(resourcesFiles, JavaEntrypointBuilder.DEFAULT_RESOURCES_PATH_ON_IMAGE) + .setLabel(RESOURCES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) + .setLabel(CLASSES_LAYER_LABEL) .build(), - LayerConfiguration.builder().addEntry(extraFiles, "/").build()); + LayerConfiguration.builder().addEntry(extraFiles, "/").setLabel(EXTRA_FILES_LAYER_LABEL).build()); } private final LayerConfiguration dependenciesLayerConfiguration; diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java index 183053bbd2..56d372eff3 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurationsTest.java @@ -206,7 +206,6 @@ public void test_correctPathsOnImage() { Assert.assertEquals( "/app/classes/", testGradleLayerConfigurations.getClassesLayerEntry().getExtractionPath()); Assert.assertEquals( - "/", - testGradleLayerConfigurations.getExtraFilesLayerEntry().getExtractionPath()); + "/", testGradleLayerConfigurations.getExtraFilesLayerEntry().getExtractionPath()); } } diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java index 7196bc1661..249a7fe146 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java @@ -36,6 +36,12 @@ /** Builds {@link LayerConfiguration}s based on inputs from a {@link MavenProject}. */ class MavenLayerConfigurations { + private static final String DEPENDENCIES_LAYER_LABEL = "dependencies"; + private static final String SNAPSHOT_DEPENDENCIES_LAYER_LABEL = "snapshot dependencies"; + private static final String RESOURCES_LAYER_LABEL = "resources"; + private static final String CLASSES_LAYER_LABEL = "classes"; + private static final String EXTRA_FILES_LAYER_LABEL = "extra files"; + /** * Resolves the source files configuration for a Maven {@link MavenProject}. * @@ -109,18 +115,22 @@ static MavenLayerConfigurations getForProject(MavenProject project, Path extraDi return new MavenLayerConfigurations( LayerConfiguration.builder() .addEntry(dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .setLabel(DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry( snapshotDependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .setLabel(SNAPSHOT_DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry(resourcesFiles, JavaEntrypointBuilder.DEFAULT_RESOURCES_PATH_ON_IMAGE) + .setLabel(RESOURCES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) + .setLabel(CLASSES_LAYER_LABEL) .build(), - LayerConfiguration.builder().addEntry(extraFiles, "/").build()); + LayerConfiguration.builder().addEntry(extraFiles, "/").setLabel(EXTRA_FILES_LAYER_LABEL).build()); } private final LayerConfiguration dependenciesLayerConfiguration; From e871c145664c169cee5714c25cdc84e072b2e322 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Sat, 21 Jul 2018 19:55:00 -0400 Subject: [PATCH 10/19] Fixes words. --- jib-gradle-plugin/CHANGELOG.md | 14 +++++++++++--- .../tools/jib/maven/MavenLayerConfigurations.java | 7 +++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index 69e640e869..ab4fb52e5a 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -9,14 +9,22 @@ All notable changes to this project will be documented in this file. ### Fixed +## 0.9.8 + +### Added + +- Docker context generation now includes snapshot dependencies and extra files ([#516](https://github.com/GoogleContainerTools/jib/pull/516/files)) + +### Changed + +- Only builds non-empty layers ([#516](https://github.com/GoogleContainerTools/jib/pull/516/files)) + ## 0.9.7 ### Added - Snapshot dependencies are added as their own layer ([#584](https://github.com/GoogleContainerTools/jib/pull/584)) - `jibBuildTar` task to build an image tarball at `build/jib-image.tar`, which can be loaded into docker using `docker load` ([#514](https://github.com/GoogleContainerTools/jib/issues/514)) -- For Docker Hub, also tries registry aliases when getting a credential from the Docker config -- Docker context generation now includes snapshot dependencies and extra files ([]()) - `container.useCurrentTimestamp` parameter to set the image creation time to the build time ([#413](https://github.com/GoogleContainerTools/jib/issues/413)) - Authentication over HTTP using the `sendCredentialsOverHttp` system property ([#599](https://github.com/GoogleContainerTools/jib/issues/599)) - HTTP connection and read timeouts for registry interactions configurable with the `jib.httpTimeout` system property ([#656](https://github.com/GoogleContainerTools/jib/pull/656)) @@ -24,7 +32,7 @@ All notable changes to this project will be documented in this file. ### Changed -- Only builds non-empty layers ([]()) +- Docker context export command-line option `--targetDir` to `--jibTargetDir` ([#662](https://github.com/GoogleContainerTools/jib/issues/662)) ### Fixed diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java index 249a7fe146..b51ed75c56 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java @@ -43,7 +43,7 @@ class MavenLayerConfigurations { private static final String EXTRA_FILES_LAYER_LABEL = "extra files"; /** - * Resolves the source files configuration for a Maven {@link MavenProject}. + * Resolves the source files configuration for a {@link MavenProject}. * * @param project the {@link MavenProject} * @param extraDirectory path to the directory for the extra files layer @@ -130,7 +130,10 @@ static MavenLayerConfigurations getForProject(MavenProject project, Path extraDi .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) .setLabel(CLASSES_LAYER_LABEL) .build(), - LayerConfiguration.builder().addEntry(extraFiles, "/").setLabel(EXTRA_FILES_LAYER_LABEL).build()); + LayerConfiguration.builder() + .addEntry(extraFiles, "/") + .setLabel(EXTRA_FILES_LAYER_LABEL) + .build()); } private final LayerConfiguration dependenciesLayerConfiguration; From 77339ff6e04b477c217dc503a022fa561c744d53 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Sat, 21 Jul 2018 19:57:04 -0400 Subject: [PATCH 11/19] Prints all layer entries. --- .../google/cloud/tools/jib/frontend/BuildStepsRunner.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java index 4de038d24d..2f7cd792f5 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildStepsRunner.java @@ -28,6 +28,7 @@ import com.google.cloud.tools.jib.cache.Caches.Initializer; import com.google.cloud.tools.jib.configuration.CacheConfiguration; import com.google.cloud.tools.jib.configuration.LayerConfiguration; +import com.google.cloud.tools.jib.image.LayerEntry; import com.google.cloud.tools.jib.registry.InsecureRegistryException; import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException; import com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException; @@ -189,8 +190,11 @@ public void build(HelpfulSuggestions helpfulSuggestions) throws BuildStepsExecut for (LayerConfiguration layerConfiguration : buildSteps.getBuildConfiguration().getLayerConfigurations()) { buildLogger.info("\t" + capitalizeFirstLetter(layerConfiguration.getLabel()) + ":"); - for (Path sourceFile : layerConfiguration.getLayerEntries().get(0).getSourceFiles()) { - buildLogger.info("\t\t" + sourceFile); + + for (LayerEntry layerEntry : layerConfiguration.getLayerEntries()) { + for (Path sourceFile : layerEntry.getSourceFiles()) { + buildLogger.info("\t\t" + sourceFile); + } } } From 614dbc73726a40c586dccc4efe4c13cc4546af5b Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Sat, 21 Jul 2018 21:50:06 -0400 Subject: [PATCH 12/19] Fixes format. --- .../cloud/tools/jib/gradle/GradleLayerConfigurations.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index e095025d5a..99d715d4a5 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -143,7 +143,10 @@ static GradleLayerConfigurations getForProject( .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) .setLabel(CLASSES_LAYER_LABEL) .build(), - LayerConfiguration.builder().addEntry(extraFiles, "/").setLabel(EXTRA_FILES_LAYER_LABEL).build()); + LayerConfiguration.builder() + .addEntry(extraFiles, "/") + .setLabel(EXTRA_FILES_LAYER_LABEL) + .build()); } private final LayerConfiguration dependenciesLayerConfiguration; From 5a8247a3c15cdd20c0640f78d3440c183923d96c Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Mon, 23 Jul 2018 22:00:40 -0700 Subject: [PATCH 13/19] Changes fields to nullable. --- .../builder/BuildStepsIntegrationTest.java | 2 +- .../tools/jib/builder/BuildConfiguration.java | 58 ++++++++++----- .../jib/configuration/LayerConfiguration.java | 2 +- .../google/cloud/tools/jib/image/Image.java | 70 ++++++++++++------- .../json/ContainerConfigurationTemplate.java | 8 +-- .../jib/image/json/ImageToJsonTranslator.java | 11 ++- .../jib/builder/BuildConfigurationTest.java | 8 +-- 7 files changed, 105 insertions(+), 54 deletions(-) diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java index e69862db3a..9640c732eb 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java @@ -62,7 +62,7 @@ private static ImmutableList getFilesList(String resourcePath) private static final TestBuildLogger logger = new TestBuildLogger(); - @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); private ImmutableList fakeLayerConfigurations; diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index 5a5c4c3917..e47a71754a 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -49,15 +49,16 @@ public static class Builder { @Nullable private ImageReference targetImageReference; @Nullable private String targetImageCredentialHelperName; @Nullable private RegistryCredentials knownTargetRegistryCredentials; - private ImmutableList javaArguments = ImmutableList.of(); - private ImmutableMap environmentMap = ImmutableMap.of(); - private ImmutableList exposedPorts = ImmutableList.of(); + // TODO: Shoule rename to not be java-specific. + @Nullable private ImmutableList javaArguments; + @Nullable private ImmutableMap environmentMap; + @Nullable private ImmutableList exposedPorts; private Class targetFormat = V22ManifestTemplate.class; @Nullable private CacheConfiguration applicationLayersCacheConfiguration; @Nullable private CacheConfiguration baseImageLayersCacheConfiguration; private boolean allowHttp = false; private ImmutableList layerConfigurations = ImmutableList.of(); - private ImmutableList entrypoint = ImmutableList.of(); + @Nullable private ImmutableList entrypoint; private BuildLogger buildLogger; @@ -106,7 +107,9 @@ public Builder setJavaArguments(@Nullable List javaArguments) { } public Builder setEnvironment(@Nullable Map environmentMap) { - if (environmentMap != null) { + if (environmentMap == null) { + this.environmentMap = null; + } else { Preconditions.checkArgument( !Iterables.any(environmentMap.keySet(), Objects::isNull) && !Iterables.any(environmentMap.values(), Objects::isNull)); @@ -116,7 +119,9 @@ public Builder setEnvironment(@Nullable Map environmentMap) { } public Builder setExposedPorts(@Nullable List exposedPorts) { - if (exposedPorts != null) { + if (exposedPorts == null) { + this.exposedPorts = null; + } else { Preconditions.checkArgument(!exposedPorts.contains(null)); this.exposedPorts = ImmutableList.copyOf(exposedPorts); } @@ -192,7 +197,9 @@ public Builder setCreationTime(Instant creationTime) { * @return this */ public Builder setEntrypoint(@Nullable List entrypoint) { - if (entrypoint != null) { + if (entrypoint == null) { + this.entrypoint = null; + } else { Preconditions.checkArgument(!entrypoint.contains(null)); this.entrypoint = ImmutableList.copyOf(entrypoint); } @@ -289,15 +296,15 @@ public static Builder builder(BuildLogger buildLogger) { private final ImageReference targetImageReference; @Nullable private final String targetImageCredentialHelperName; @Nullable private final RegistryCredentials knownTargetRegistryCredentials; - private final ImmutableList javaArguments; - private final ImmutableMap environmentMap; - private final ImmutableList exposedPorts; + @Nullable private final ImmutableList javaArguments; + @Nullable private final ImmutableMap environmentMap; + @Nullable private final ImmutableList exposedPorts; private final Class targetFormat; @Nullable private final CacheConfiguration applicationLayersCacheConfiguration; @Nullable private final CacheConfiguration baseImageLayersCacheConfiguration; private final boolean allowHttp; private final ImmutableList layerConfigurations; - private final ImmutableList entrypoint; + @Nullable private final ImmutableList entrypoint; /** Instantiate with {@link Builder#build}. */ private BuildConfiguration( @@ -309,15 +316,15 @@ private BuildConfiguration( ImageReference targetImageReference, @Nullable String targetImageCredentialHelperName, @Nullable RegistryCredentials knownTargetRegistryCredentials, - ImmutableList javaArguments, - ImmutableMap environmentMap, - ImmutableList exposedPorts, + @Nullable ImmutableList javaArguments, + @Nullable ImmutableMap environmentMap, + @Nullable ImmutableList exposedPorts, Class targetFormat, @Nullable CacheConfiguration applicationLayersCacheConfiguration, @Nullable CacheConfiguration baseImageLayersCacheConfiguration, boolean allowHttp, ImmutableList layerConfigurations, - ImmutableList entrypoint) { + @Nullable ImmutableList entrypoint) { this.buildLogger = buildLogger; this.creationTime = creationTime; this.baseImageReference = baseImageReference; @@ -397,14 +404,32 @@ public RegistryCredentials getKnownTargetRegistryCredentials() { return knownTargetRegistryCredentials; } + /** + * Gets the arguments to pass to the entrypoint. + * + * @return the list of arguments, or {@code null} if not set + */ + @Nullable public ImmutableList getJavaArguments() { return javaArguments; } + /** + * Gets the map from environment variable names to values for the container. + * + * @return the map of environment variables, or {@code null} if not set + */ + @Nullable public ImmutableMap getEnvironment() { return environmentMap; } + /** + * Gets the ports to expose on the container. + * + * @return the list of exposed ports, or {@code null} if not set + */ + @Nullable public ImmutableList getExposedPorts() { return exposedPorts; } @@ -454,8 +479,9 @@ public ImmutableList getLayerConfigurations() { /** * Gets the container entrypoint. * - * @return the list of entrypoint tokens + * @return the list of entrypoint tokens, or {@code null} if not set */ + @Nullable public ImmutableList getEntrypoint() { return entrypoint; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java index 9e194ee834..b87774e6a8 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/LayerConfiguration.java @@ -77,7 +77,7 @@ public static Builder builder() { } private final ImmutableList layerEntries; - private String label; + private final String label; /** * Constructs a new layer configuration. diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java index b9d4c56384..58704e2b7a 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java @@ -30,12 +30,12 @@ public class Image { public static class Builder { private final ImageLayers.Builder imageLayersBuilder = ImageLayers.builder(); - private final ImmutableList.Builder environmentBuilder = ImmutableList.builder(); + private ImmutableList.Builder environmentBuilder = ImmutableList.builder(); @Nullable private Instant created; - private ImmutableList entrypoint = ImmutableList.of(); - private ImmutableList javaArguments = ImmutableList.of(); - private ImmutableList exposedPorts = ImmutableList.of(); + @Nullable private ImmutableList entrypoint; + @Nullable private ImmutableList javaArguments; + @Nullable private ImmutableList exposedPorts; /** * Sets the image creation time. @@ -54,9 +54,13 @@ public Builder setCreated(Instant created) { * @param environment the map of environment variables * @return this */ - public Builder setEnvironment(Map environment) { - for (Map.Entry environmentVariable : environment.entrySet()) { - setEnvironmentVariable(environmentVariable.getKey(), environmentVariable.getValue()); + public Builder setEnvironment(@Nullable Map environment) { + if (environment == null) { + this.environmentBuilder = ImmutableList.builder(); + } else { + for (Map.Entry environmentVariable : environment.entrySet()) { + setEnvironmentVariable(environmentVariable.getKey(), environmentVariable.getValue()); + } } return this; } @@ -90,8 +94,12 @@ public Builder addEnvironmentVariableDefinition(String environmentVariableDef * @param entrypoint the list of entrypoint tokens * @return this */ - public Builder setEntrypoint(List entrypoint) { - this.entrypoint = ImmutableList.copyOf(entrypoint); + public Builder setEntrypoint(@Nullable List entrypoint) { + if (entrypoint == null) { + this.entrypoint = null; + } else { + this.entrypoint = ImmutableList.copyOf(entrypoint); + } return this; } @@ -101,8 +109,12 @@ public Builder setEntrypoint(List entrypoint) { * @param javaArguments the list of main args to add * @return this */ - public Builder setJavaArguments(List javaArguments) { - this.javaArguments = ImmutableList.copyOf(javaArguments); + public Builder setJavaArguments(@Nullable List javaArguments) { + if (javaArguments == null) { + this.javaArguments = null; + } else { + this.javaArguments = ImmutableList.copyOf(javaArguments); + } return this; } @@ -112,8 +124,12 @@ public Builder setJavaArguments(List javaArguments) { * @param exposedPorts the list of exposed ports to add * @return this */ - public Builder setExposedPorts(ImmutableList exposedPorts) { - this.exposedPorts = exposedPorts; + public Builder setExposedPorts(@Nullable ImmutableList exposedPorts) { + if (exposedPorts == null) { + this.exposedPorts = null; + } else { + this.exposedPorts = exposedPorts; + } return this; } @@ -134,8 +150,8 @@ public Image build() { created, imageLayersBuilder.build(), environmentBuilder.build(), - ImmutableList.copyOf(entrypoint), - ImmutableList.copyOf(javaArguments), + entrypoint, + javaArguments, exposedPorts); } } @@ -151,27 +167,27 @@ public static Builder builder() { private final ImageLayers layers; /** Environment variable definitions for running the image, in the format {@code NAME=VALUE}. */ - private final ImmutableList environmentBuilder; + @Nullable private final ImmutableList environment; /** Initial command to run when running the image. */ - private final ImmutableList entrypoint; + @Nullable private final ImmutableList entrypoint; /** Arguments to pass into main when running the image. */ - private final ImmutableList javaArguments; + @Nullable private final ImmutableList javaArguments; /** Ports that the container listens on. */ - private final ImmutableList exposedPorts; + @Nullable private final ImmutableList exposedPorts; private Image( @Nullable Instant created, ImageLayers layers, - ImmutableList environment, - ImmutableList entrypoint, - ImmutableList javaArguments, - ImmutableList exposedPorts) { + @Nullable ImmutableList environment, + @Nullable ImmutableList entrypoint, + @Nullable ImmutableList javaArguments, + @Nullable ImmutableList exposedPorts) { this.created = created; this.layers = layers; - this.environmentBuilder = environment; + this.environment = environment; this.entrypoint = entrypoint; this.javaArguments = javaArguments; this.exposedPorts = exposedPorts; @@ -182,18 +198,22 @@ public Instant getCreated() { return created; } + @Nullable public ImmutableList getEnvironment() { - return environmentBuilder; + return environment; } + @Nullable public ImmutableList getEntrypoint() { return entrypoint; } + @Nullable public ImmutableList getJavaArguments() { return javaArguments; } + @Nullable public ImmutableList getExposedPorts() { return exposedPorts; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java index 2ff134c05c..9d8f0f2b4f 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java @@ -109,19 +109,19 @@ public void setCreated(@Nullable String created) { this.created = created; } - public void setContainerEnvironment(List environment) { + public void setContainerEnvironment(@Nullable List environment) { config.Env = environment; } - public void setContainerEntrypoint(List command) { + public void setContainerEntrypoint(@Nullable List command) { config.Entrypoint = command; } - public void setContainerCmd(List cmd) { + public void setContainerCmd(@Nullable List cmd) { config.Cmd = cmd; } - public void setContainerExposedPorts(Map> exposedPorts) { + public void setContainerExposedPorts(@Nullable Map> exposedPorts) { config.ExposedPorts = exposedPorts; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java index ad0145d11e..5731005163 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java @@ -29,6 +29,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; /** * Translates an {@link Image} into a manifest or container configuration JSON BLOB. @@ -48,12 +49,16 @@ public class ImageToJsonTranslator { * Converts a list of {@link Port}s to the corresponding container config format for exposed ports * (e.g. {@code Port(1000, Protocol.TCP)} -> {@code {"1000/tcp":{}}}). * - * @param exposedPorts the list of {@link Port}s to translate + * @param exposedPorts the list of {@link Port}s to translate, or {@code null} * @return a sorted map with the string representation of the ports as keys and empty maps as - * values + * values, or {@code null} if {@code exposedPorts} is {@code null} */ @VisibleForTesting - static Map> portListToMap(List exposedPorts) { + @Nullable + static Map> portListToMap(@Nullable List exposedPorts) { + if (exposedPorts == null) { + return null; + } ImmutableSortedMap.Builder> result = new ImmutableSortedMap.Builder<>(String::compareTo); for (Port port : exposedPorts) { diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java index b52b6c18fe..744cb08f60 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java @@ -148,15 +148,15 @@ public void testBuilder_default() { Assert.assertNull(buildConfiguration.getKnownBaseRegistryCredentials()); Assert.assertNull(buildConfiguration.getTargetImageCredentialHelperName()); Assert.assertNull(buildConfiguration.getKnownTargetRegistryCredentials()); - Assert.assertEquals(Collections.emptyList(), buildConfiguration.getJavaArguments()); - Assert.assertEquals(Collections.emptyMap(), buildConfiguration.getEnvironment()); - Assert.assertEquals(Collections.emptyList(), buildConfiguration.getExposedPorts()); + Assert.assertNull(buildConfiguration.getJavaArguments()); + Assert.assertNull(buildConfiguration.getEnvironment()); + Assert.assertNull(buildConfiguration.getExposedPorts()); Assert.assertEquals(V22ManifestTemplate.class, buildConfiguration.getTargetFormat()); Assert.assertNull(buildConfiguration.getApplicationLayersCacheConfiguration()); Assert.assertNull(buildConfiguration.getBaseImageLayersCacheConfiguration()); Assert.assertFalse(buildConfiguration.getAllowHttp()); Assert.assertEquals(Collections.emptyList(), buildConfiguration.getLayerConfigurations()); - Assert.assertEquals(Collections.emptyList(), buildConfiguration.getEntrypoint()); + Assert.assertNull(buildConfiguration.getEntrypoint()); } @Test From bc46b0b70c8a8f4bf283a30b718c3fb3f45b4521 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Mon, 23 Jul 2018 22:12:27 -0700 Subject: [PATCH 14/19] Renames JavaEntrypointConstructor. --- .../cloud/tools/jib/docker/DockerContextGenerator.java | 4 ++-- ...ointBuilder.java => JavaEntrypointConstructor.java} | 8 ++++---- .../google/cloud/tools/jib/gradle/BuildDockerTask.java | 4 ++-- .../google/cloud/tools/jib/gradle/BuildImageTask.java | 4 ++-- .../google/cloud/tools/jib/gradle/BuildTarTask.java | 4 ++-- .../tools/jib/gradle/GradleLayerConfigurations.java | 10 +++++----- .../google/cloud/tools/jib/maven/BuildDockerMojo.java | 4 ++-- .../google/cloud/tools/jib/maven/BuildImageMojo.java | 4 ++-- .../com/google/cloud/tools/jib/maven/BuildTarMojo.java | 4 ++-- .../tools/jib/maven/MavenLayerConfigurations.java | 10 +++++----- 10 files changed, 28 insertions(+), 28 deletions(-) rename jib-core/src/main/java/com/google/cloud/tools/jib/frontend/{JavaEntrypointBuilder.java => JavaEntrypointConstructor.java} (92%) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java index 4bbd1282e7..2d5b031cab 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java @@ -19,7 +19,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.cloud.tools.jib.filesystem.FileOperations; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -255,7 +255,7 @@ String makeDockerfile() throws JsonProcessingException { .append("\nENTRYPOINT ") .append( objectMapper.writeValueAsString( - JavaEntrypointBuilder.makeDefaultEntrypoint(jvmFlags, mainClass))) + JavaEntrypointConstructor.makeDefaultEntrypoint(jvmFlags, mainClass))) .append("\nCMD ") .append(objectMapper.writeValueAsString(javaArguments)); return dockerfile.toString(); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointConstructor.java similarity index 92% rename from jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilder.java rename to jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointConstructor.java index 151b4d1120..d18fcf09eb 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointBuilder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaEntrypointConstructor.java @@ -20,8 +20,8 @@ import java.util.Arrays; import java.util.List; -/** Builds an image entrypoint for the Java application. */ -public class JavaEntrypointBuilder { +/** Constructs an image entrypoint for the Java application. */ +public class JavaEntrypointConstructor { public static final String DEFAULT_DEPENDENCIES_PATH_ON_IMAGE = "/app/libs/"; public static final String DEFAULT_RESOURCES_PATH_ON_IMAGE = "/app/resources/"; @@ -38,7 +38,7 @@ public static List makeDefaultEntrypoint(List jvmFlags, String m } /** - * Builds the container entrypoint. + * Constructs the container entrypoint. * *

The entrypoint is {@code java [jvm flags] -cp [classpaths] [main class]}. * @@ -60,5 +60,5 @@ public static List makeEntrypoint( return entrypointBuilder; } - private JavaEntrypointBuilder() {} + private JavaEntrypointConstructor() {} } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index 333f90100a..9dd7e21b0f 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -24,7 +24,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; @@ -116,7 +116,7 @@ public void buildDocker() throws InvalidImageReferenceException { .setAllowHttp(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) .setEntrypoint( - JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index e47a9a9887..a59f4eb0d7 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -23,7 +23,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; @@ -124,7 +124,7 @@ public void buildImage() throws InvalidImageReferenceException { .setAllowHttp(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) .setEntrypoint( - JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java index af44bc30f8..bf2176c293 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java @@ -23,7 +23,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; @@ -144,7 +144,7 @@ public void buildTar() throws InvalidImageReferenceException { .setAllowHttp(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) .setEntrypoint( - JavaEntrypointBuilder.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 99d715d4a5..08706ed0b7 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -17,7 +17,7 @@ package com.google.cloud.tools.jib.gradle; import com.google.cloud.tools.jib.configuration.LayerConfiguration; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import java.io.File; @@ -127,20 +127,20 @@ static GradleLayerConfigurations getForProject( return new GradleLayerConfigurations( LayerConfiguration.builder() - .addEntry(dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .addEntry(dependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry( - snapshotDependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + snapshotDependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(SNAPSHOT_DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() - .addEntry(resourcesFiles, JavaEntrypointBuilder.DEFAULT_RESOURCES_PATH_ON_IMAGE) + .addEntry(resourcesFiles, JavaEntrypointConstructor.DEFAULT_RESOURCES_PATH_ON_IMAGE) .setLabel(RESOURCES_LAYER_LABEL) .build(), LayerConfiguration.builder() - .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) + .addEntry(classesFiles, JavaEntrypointConstructor.DEFAULT_CLASSES_PATH_ON_IMAGE) .setLabel(CLASSES_LAYER_LABEL) .build(), LayerConfiguration.builder() diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java index b040297555..ecf89f85a8 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java @@ -24,7 +24,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.RegistryClient; @@ -94,7 +94,7 @@ public void execute() throws MojoExecutionException { .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) .setAllowHttp(getAllowInsecureRegistries()) .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(getJvmFlags(), mainClass)); + .setEntrypoint(JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java index 10037738e6..85408ce416 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java @@ -23,7 +23,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageFormat; import com.google.cloud.tools.jib.image.ImageReference; @@ -116,7 +116,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { .setTargetFormat(ImageFormat.valueOf(getFormat()).getManifestTemplateClass()) .setAllowHttp(getAllowInsecureRegistries()) .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(getJvmFlags(), mainClass)); + .setEntrypoint(JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java index 2f165ed12c..567c8da1f6 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java @@ -23,7 +23,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.RegistryClient; @@ -92,7 +92,7 @@ public void execute() throws MojoExecutionException { .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) .setAllowHttp(getAllowInsecureRegistries()) .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointBuilder.makeDefaultEntrypoint(getJvmFlags(), mainClass)); + .setEntrypoint(JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java index b51ed75c56..63e48dac48 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java @@ -17,7 +17,7 @@ package com.google.cloud.tools.jib.maven; import com.google.cloud.tools.jib.configuration.LayerConfiguration; -import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.image.LayerEntry; import com.google.common.collect.ImmutableList; import java.io.IOException; @@ -114,20 +114,20 @@ static MavenLayerConfigurations getForProject(MavenProject project, Path extraDi return new MavenLayerConfigurations( LayerConfiguration.builder() - .addEntry(dependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .addEntry(dependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry( - snapshotDependenciesFiles, JavaEntrypointBuilder.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + snapshotDependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(SNAPSHOT_DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() - .addEntry(resourcesFiles, JavaEntrypointBuilder.DEFAULT_RESOURCES_PATH_ON_IMAGE) + .addEntry(resourcesFiles, JavaEntrypointConstructor.DEFAULT_RESOURCES_PATH_ON_IMAGE) .setLabel(RESOURCES_LAYER_LABEL) .build(), LayerConfiguration.builder() - .addEntry(classesFiles, JavaEntrypointBuilder.DEFAULT_CLASSES_PATH_ON_IMAGE) + .addEntry(classesFiles, JavaEntrypointConstructor.DEFAULT_CLASSES_PATH_ON_IMAGE) .setLabel(CLASSES_LAYER_LABEL) .build(), LayerConfiguration.builder() From 0ee1e8600462c38fe708887520cafe45851e4705 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Mon, 23 Jul 2018 22:28:10 -0700 Subject: [PATCH 15/19] Some more comments. --- .../jib/builder/BuildStepsIntegrationTest.java | 9 ++++----- .../tools/jib/builder/BuildConfiguration.java | 15 ++------------- .../tools/jib/docker/DockerContextGenerator.java | 1 - 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java index a0ba29a483..d73112c53f 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java @@ -50,7 +50,7 @@ public class BuildStepsIntegrationTest { /** Lists the files in the {@code resourcePath} resources directory. */ - private static ImmutableList getFilesList(String resourcePath) + private static ImmutableList getResourceFilesList(String resourcePath) throws URISyntaxException, IOException { try (Stream fileStream = Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { @@ -71,13 +71,13 @@ public void setUp() throws IOException, URISyntaxException { fakeLayerConfigurations = ImmutableList.of( LayerConfiguration.builder() - .addEntry(getFilesList("application/dependencies"), "/app/libs/") + .addEntry(getResourceFilesList("application/dependencies"), "/app/libs/") .build(), LayerConfiguration.builder() - .addEntry(getFilesList("application/resources"), "/app/resources/") + .addEntry(getResourceFilesList("application/resources"), "/app/resources/") .build(), LayerConfiguration.builder() - .addEntry(getFilesList("application/classes"), "/app/classes/") + .addEntry(getResourceFilesList("application/classes"), "/app/classes/") .build()); } @@ -99,7 +99,6 @@ public void testSteps_forBuildToDockerRegistry() JavaEntrypointConstructor.makeDefaultEntrypoint( Collections.emptyList(), "HelloWorld")) .build()); - buildImageSteps.run(); long lastTime = System.nanoTime(); buildImageSteps.run(); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index ca305859e4..ba9ef38bac 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -254,19 +254,8 @@ public BuildConfiguration build() { throw new IllegalStateException(errorMessages.get(0) + " and " + errorMessages.get(1)); default: - // Appends the descriptions in correct grammar. - StringBuilder errorMessage = new StringBuilder(errorMessages.get(0)); - for (int errorMessageIndex = 1; - errorMessageIndex < errorMessages.size(); - errorMessageIndex++) { - if (errorMessageIndex == errorMessages.size() - 1) { - errorMessage.append(", and "); - } else { - errorMessage.append(", "); - } - errorMessage.append(errorMessages.get(errorMessageIndex)); - } - throw new IllegalStateException(errorMessage.toString()); + // Should never reach here. + throw new IllegalStateException(); } } } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java index 2d5b031cab..2840a007c7 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java @@ -180,7 +180,6 @@ public DockerContextGenerator setExposedPorts(List exposedPorts) { return this; } - // TODO: Don't generate empty layers. /** * Creates the Docker context in {@code #targetDirectory}. * From 35962060508df8399b7c2a6e0364d6f32f93703f Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Mon, 23 Jul 2018 22:29:41 -0700 Subject: [PATCH 16/19] Fixes format. --- .../tools/jib/gradle/BuildDockerTask.java | 3 ++- .../cloud/tools/jib/gradle/BuildImageTask.java | 5 +++-- .../cloud/tools/jib/gradle/BuildTarTask.java | 5 +++-- .../jib/gradle/GradleLayerConfigurations.java | 6 ++++-- .../cloud/tools/jib/maven/BuildDockerMojo.java | 5 +++-- .../cloud/tools/jib/maven/BuildImageMojo.java | 5 +++-- .../cloud/tools/jib/maven/BuildTarMojo.java | 18 +----------------- .../jib/maven/MavenLayerConfigurations.java | 6 ++++-- 8 files changed, 23 insertions(+), 30 deletions(-) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index 1a7e5f1fa5..24831b5535 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -116,7 +116,8 @@ public void buildDocker() throws InvalidImageReferenceException { .setAllowInsecureRegistries(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) .setEntrypoint( - JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + JavaEntrypointConstructor.makeDefaultEntrypoint( + jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index 178ddaf23a..1a4f114b83 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -121,10 +121,11 @@ public void buildImage() throws InvalidImageReferenceException { .setJavaArguments(jibExtension.getArgs()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setTargetFormat(jibExtension.getFormat()) - .setAllowInsecureRegistries(jibExtension.getAllowInsecureRegistries()) + .setAllowInsecureRegistries(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) .setEntrypoint( - JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + JavaEntrypointConstructor.makeDefaultEntrypoint( + jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java index 57540eda09..7cac24b230 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java @@ -141,10 +141,11 @@ public void buildTar() throws InvalidImageReferenceException { .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) .setJavaArguments(jibExtension.getArgs()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) - .setAllowInsecureRegistries(jibExtension.getAllowInsecureRegistries()) + .setAllowInsecureRegistries(jibExtension.getAllowInsecureRegistries()) .setLayerConfigurations(gradleProjectProperties.getLayerConfigurations()) .setEntrypoint( - JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass)); + JavaEntrypointConstructor.makeDefaultEntrypoint( + jibExtension.getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java index 08706ed0b7..641f285426 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleLayerConfigurations.java @@ -127,12 +127,14 @@ static GradleLayerConfigurations getForProject( return new GradleLayerConfigurations( LayerConfiguration.builder() - .addEntry(dependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .addEntry( + dependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry( - snapshotDependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + snapshotDependenciesFiles, + JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(SNAPSHOT_DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java index d841730c1d..af7ae1f47e 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java @@ -92,9 +92,10 @@ public void execute() throws MojoExecutionException { .setJavaArguments(getArgs()) .setEnvironment(getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) - .setAllowInsecureRegistries(getAllowInsecureRegistries()) + .setAllowInsecureRegistries(getAllowInsecureRegistries()) .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); + .setEntrypoint( + JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java index 81b284a096..4d88722635 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java @@ -114,9 +114,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { .setEnvironment(getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) .setTargetFormat(ImageFormat.valueOf(getFormat()).getManifestTemplateClass()) - .setAllowInsecureRegistries(getAllowInsecureRegistries()) + .setAllowInsecureRegistries(getAllowInsecureRegistries()) .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); + .setEntrypoint( + JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java index 4a5a409594..3a5e49c04a 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java @@ -90,25 +90,9 @@ public void execute() throws MojoExecutionException { .setJavaArguments(getArgs()) .setEnvironment(getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) -<<<<<<< HEAD - .setAllowHttp(getAllowInsecureRegistries()) + .setAllowInsecureRegistries(getAllowInsecureRegistries()) .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) .setEntrypoint(JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); -======= - .setAllowInsecureRegistries(getAllowInsecureRegistries()); - if (getExtraDirectory() != null && Files.exists(getExtraDirectory())) { - try (Stream extraFilesLayerDirectoryFiles = Files.list(getExtraDirectory())) { - buildConfigurationBuilder.setExtraFilesLayerConfiguration( - LayerConfiguration.builder() - .addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/") - .build()); - - } catch (IOException ex) { - throw new MojoExecutionException( - "Failed to list directory for extra files: " + getExtraDirectory(), ex); - } - } ->>>>>>> master CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration( diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java index 63e48dac48..78f3e25411 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenLayerConfigurations.java @@ -114,12 +114,14 @@ static MavenLayerConfigurations getForProject(MavenProject project, Path extraDi return new MavenLayerConfigurations( LayerConfiguration.builder() - .addEntry(dependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + .addEntry( + dependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() .addEntry( - snapshotDependenciesFiles, JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) + snapshotDependenciesFiles, + JavaEntrypointConstructor.DEFAULT_DEPENDENCIES_PATH_ON_IMAGE) .setLabel(SNAPSHOT_DEPENDENCIES_LAYER_LABEL) .build(), LayerConfiguration.builder() From d7da59d7e7f309674bce02c8cf3b964fbe63dc5b Mon Sep 17 00:00:00 2001 From: Q Chen Date: Tue, 24 Jul 2018 16:41:21 -0700 Subject: [PATCH 17/19] Fixes CHANGLOG. --- jib-gradle-plugin/CHANGELOG.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index f335c085ac..009e3327ab 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -5,14 +5,6 @@ All notable changes to this project will be documented in this file. ### Added -### Changed - -### Fixed - -## 0.9.8 - -### Added - - Docker context generation now includes snapshot dependencies and extra files ([#516](https://github.com/GoogleContainerTools/jib/pull/516/files)) ### Changed From 69764a0f6499ea58dbf8e214f45fd00612308ca7 Mon Sep 17 00:00:00 2001 From: Q Chen Date: Tue, 24 Jul 2018 16:43:59 -0700 Subject: [PATCH 18/19] A typo. --- .../com/google/cloud/tools/jib/builder/BuildConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index ba9ef38bac..4d0412c971 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -49,7 +49,7 @@ public static class Builder { @Nullable private ImageReference targetImageReference; @Nullable private String targetImageCredentialHelperName; @Nullable private RegistryCredentials knownTargetRegistryCredentials; - // TODO: Shoule rename to not be java-specific. + // TODO: Should rename to not be java-specific. @Nullable private ImmutableList javaArguments; @Nullable private ImmutableMap environmentMap; @Nullable private ImmutableList exposedPorts; From 19db09398f0a70f90fa552de60b6ec911b544bad Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Tue, 24 Jul 2018 17:08:00 -0700 Subject: [PATCH 19/19] Fixes format. --- .../java/com/google/cloud/tools/jib/maven/BuildTarMojo.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java index 3a5e49c04a..b5c59fe153 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java @@ -92,7 +92,8 @@ public void execute() throws MojoExecutionException { .setExposedPorts(ExposedPortsParser.parse(getExposedPorts())) .setAllowInsecureRegistries(getAllowInsecureRegistries()) .setLayerConfigurations(mavenProjectProperties.getLayerConfigurations()) - .setEntrypoint(JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); + .setEntrypoint( + JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass)); CacheConfiguration applicationLayersCacheConfiguration = CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory()); buildConfigurationBuilder.setApplicationLayersCacheConfiguration(