Search in sources :

Example 41 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class PluginConfigurationProcessor method computeEntrypoint.

/**
 * Computes the container entrypoint.
 *
 * <p>Computation occurs in this order:
 *
 * <ol>
 *   <li>null (inheriting from the base image), if the user specified value is {@code INHERIT}
 *   <li>the user specified one, if set
 *   <li>for a WAR project, null (inheriting) if a custom base image is specified, and {@code
 *       ["java", "-jar", "/usr/local/jetty/start.jar"]} otherwise (default Jetty base image)
 *   <li>for a non-WAR project, by resolving the main class
 * </ol>
 *
 * @param rawConfiguration raw configuration data
 * @param projectProperties used for providing additional information
 * @param jibContainerBuilder container builder
 * @return the entrypoint
 * @throws MainClassInferenceException if no valid main class is configured or discovered
 * @throws InvalidAppRootException if {@code appRoot} value is not an absolute Unix path
 * @throws InvalidContainerizingModeException if {@code containerizingMode} value is invalid
 */
@Nullable
@VisibleForTesting
static List<String> computeEntrypoint(RawConfiguration rawConfiguration, ProjectProperties projectProperties, JibContainerBuilder jibContainerBuilder) throws MainClassInferenceException, InvalidAppRootException, IOException, InvalidContainerizingModeException {
    Optional<List<String>> rawEntrypoint = rawConfiguration.getEntrypoint();
    List<String> rawExtraClasspath = rawConfiguration.getExtraClasspath();
    boolean entrypointDefined = rawEntrypoint.isPresent() && !rawEntrypoint.get().isEmpty();
    if (entrypointDefined && (rawConfiguration.getMainClass().isPresent() || !rawConfiguration.getJvmFlags().isEmpty() || !rawExtraClasspath.isEmpty() || rawConfiguration.getExpandClasspathDependencies())) {
        projectProperties.log(LogEvent.warn("mainClass, extraClasspath, jvmFlags, and expandClasspathDependencies are ignored " + "when entrypoint is specified"));
    }
    if (projectProperties.isWarProject()) {
        if (entrypointDefined) {
            return rawEntrypoint.get().size() == 1 && "INHERIT".equals(rawEntrypoint.get().get(0)) ? null : rawEntrypoint.get();
        }
        if (rawConfiguration.getMainClass().isPresent() || !rawConfiguration.getJvmFlags().isEmpty() || !rawExtraClasspath.isEmpty() || rawConfiguration.getExpandClasspathDependencies()) {
            projectProperties.log(LogEvent.warn("mainClass, extraClasspath, jvmFlags, and expandClasspathDependencies are ignored " + "for WAR projects"));
        }
        return rawConfiguration.getFromImage().isPresent() ? // Inherit if a custom base image.
        null : Arrays.asList("java", "-jar", "/usr/local/jetty/start.jar");
    }
    List<String> classpath = new ArrayList<>(rawExtraClasspath);
    AbsoluteUnixPath appRoot = getAppRootChecked(rawConfiguration, projectProperties);
    ContainerizingMode mode = getContainerizingModeChecked(rawConfiguration, projectProperties);
    switch(mode) {
        case EXPLODED:
            classpath.add(appRoot.resolve("resources").toString());
            classpath.add(appRoot.resolve("classes").toString());
            break;
        case PACKAGED:
            classpath.add(appRoot.resolve("classpath/*").toString());
            break;
        default:
            throw new IllegalStateException("unknown containerizing mode: " + mode);
    }
    if (projectProperties.getMajorJavaVersion() >= 9 || rawConfiguration.getExpandClasspathDependencies()) {
        List<Path> jars = projectProperties.getDependencies();
        Map<String, Long> occurrences = jars.stream().map(path -> path.getFileName().toString()).collect(Collectors.groupingBy(filename -> filename, Collectors.counting()));
        List<String> duplicates = occurrences.entrySet().stream().filter(entry -> entry.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());
        for (Path jar : jars) {
            // Handle duplicates by appending filesize to the end of the file. This renaming logic
            // must be in sync with the code that does the same in the other place. See
            // https://github.com/GoogleContainerTools/jib/issues/3331
            String jarName = jar.getFileName().toString();
            if (duplicates.contains(jarName)) {
                jarName = jarName.replaceFirst("\\.jar$", "-" + Files.size(jar)) + ".jar";
            }
            classpath.add(appRoot.resolve("libs").resolve(jarName).toString());
        }
    } else {
        classpath.add(appRoot.resolve("libs/*").toString());
    }
    String classpathString = String.join(":", classpath);
    String mainClass;
    try {
        mainClass = MainClassResolver.resolveMainClass(rawConfiguration.getMainClass().orElse(null), projectProperties);
    } catch (MainClassInferenceException ex) {
        if (entrypointDefined) {
            // We will use the user-given entrypoint, so don't fail.
            mainClass = "could-not-infer-a-main-class";
        } else {
            throw ex;
        }
    }
    addJvmArgFilesLayer(rawConfiguration, projectProperties, jibContainerBuilder, classpathString, mainClass);
    if (projectProperties.getMajorJavaVersion() >= 9) {
        classpathString = "@" + appRoot.resolve(JIB_CLASSPATH_FILE);
    }
    if (entrypointDefined) {
        return rawEntrypoint.get().size() == 1 && "INHERIT".equals(rawEntrypoint.get().get(0)) ? null : rawEntrypoint.get();
    }
    List<String> entrypoint = new ArrayList<>(4 + rawConfiguration.getJvmFlags().size());
    entrypoint.add("java");
    entrypoint.addAll(rawConfiguration.getJvmFlags());
    entrypoint.add("-cp");
    entrypoint.add(classpathString);
    entrypoint.add(mainClass);
    return entrypoint;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) CredHelperConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.CredHelperConfiguration) Arrays(java.util.Arrays) ImageReference(com.google.cloud.tools.jib.api.ImageReference) PlatformConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) ExtraDirectoriesConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration) Credential(com.google.cloud.tools.jib.api.Credential) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) Map(java.util.Map) Splitter(com.google.common.base.Splitter) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Verify(com.google.common.base.Verify) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) Set(java.util.Set) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) Stream(java.util.stream.Stream) ImageFormat(com.google.cloud.tools.jib.api.buildplan.ImageFormat) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) Optional(java.util.Optional) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Ports(com.google.cloud.tools.jib.api.Ports) DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Jib(com.google.cloud.tools.jib.api.Jib) ImmutableList(com.google.common.collect.ImmutableList) CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Files(java.nio.file.Files) TarImage(com.google.cloud.tools.jib.api.TarImage) IOException(java.io.IOException) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ArrayList(java.util.ArrayList) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 42 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_overlappingPermissions.

@Test
public void testExtraDirectoryLayerConfiguration_overlappingPermissions() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    Map<String, FilePermissions> permissionsMap = ImmutableMap.of("/a**", FilePermissions.fromOctalString("123"), // Should be ignored, since first match takes priority
    "/a/b**", FilePermissions.fromOctalString("000"), // Should override first match since explicit path is used instead of glob
    "/a/b/bar", FilePermissions.fromOctalString("765"));
    FileEntriesLayer fileEntriesLayer = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Collections.emptyList(), Collections.emptyList(), permissionsMap, (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(fileEntriesLayer.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/a", "/a/b", "/a/b/bar", "/c", "/c/cat", "/foo");
    Map<AbsoluteUnixPath, FilePermissions> expectedPermissions = ImmutableMap.<AbsoluteUnixPath, FilePermissions>builder().put(AbsoluteUnixPath.get("/a"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b/bar"), FilePermissions.fromOctalString("765")).put(AbsoluteUnixPath.get("/c"), FilePermissions.DEFAULT_FOLDER_PERMISSIONS).put(AbsoluteUnixPath.get("/c/cat"), FilePermissions.DEFAULT_FILE_PERMISSIONS).put(AbsoluteUnixPath.get("/foo"), FilePermissions.DEFAULT_FILE_PERMISSIONS).build();
    for (FileEntry entry : fileEntriesLayer.getEntries()) {
        assertThat(entry.getPermissions()).isEqualTo(expectedPermissions.get(entry.getExtractionPath()));
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 43 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_globPermissions.

@Test
public void testExtraDirectoryLayerConfiguration_globPermissions() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    Map<String, FilePermissions> permissionsMap = ImmutableMap.of("/a", FilePermissions.fromOctalString("123"), "/a/*", FilePermissions.fromOctalString("456"), "**/bar", FilePermissions.fromOctalString("765"));
    FileEntriesLayer fileEntriesLayer = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Collections.emptyList(), Collections.emptyList(), permissionsMap, (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(fileEntriesLayer.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/a", "/a/b", "/a/b/bar", "/c", "/c/cat", "/foo");
    Map<AbsoluteUnixPath, FilePermissions> expectedPermissions = ImmutableMap.<AbsoluteUnixPath, FilePermissions>builder().put(AbsoluteUnixPath.get("/a"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b"), FilePermissions.fromOctalString("456")).put(AbsoluteUnixPath.get("/a/b/bar"), FilePermissions.fromOctalString("765")).put(AbsoluteUnixPath.get("/c"), FilePermissions.DEFAULT_FOLDER_PERMISSIONS).put(AbsoluteUnixPath.get("/c/cat"), FilePermissions.DEFAULT_FILE_PERMISSIONS).put(AbsoluteUnixPath.get("/foo"), FilePermissions.DEFAULT_FILE_PERMISSIONS).build();
    for (FileEntry entry : fileEntriesLayer.getEntries()) {
        assertThat(entry.getPermissions()).isEqualTo(expectedPermissions.get(entry.getExtractionPath()));
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 44 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project quarkus by quarkusio.

the class JibProcessor method createContainerBuilderFromNative.

private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig, ContainerImageConfig containerImageConfig, NativeImageBuildItem nativeImageBuildItem, List<ContainerImageLabelBuildItem> containerImageLabels) {
    List<String> entrypoint;
    if (jibConfig.nativeEntrypoint.isPresent()) {
        entrypoint = jibConfig.nativeEntrypoint.get();
    } else {
        List<String> nativeArguments = jibConfig.nativeArguments.orElse(Collections.emptyList());
        entrypoint = new ArrayList<>(nativeArguments.size() + 1);
        entrypoint.add("./" + BINARY_NAME_IN_CONTAINER);
        entrypoint.addAll(nativeArguments);
    }
    try {
        AbsoluteUnixPath workDirInContainer = AbsoluteUnixPath.get("/work");
        JibContainerBuilder jibContainerBuilder = Jib.from(toRegistryImage(ImageReference.parse(jibConfig.baseNativeImage), jibConfig.baseRegistryUsername, jibConfig.baseRegistryPassword)).addFileEntriesLayer(FileEntriesLayer.builder().addEntry(nativeImageBuildItem.getPath(), workDirInContainer.resolve(BINARY_NAME_IN_CONTAINER), FilePermissions.fromOctalString("775")).build()).setWorkingDirectory(workDirInContainer).setEntrypoint(entrypoint).setEnvironment(getEnvironmentVariables(jibConfig)).setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels));
        if (jibConfig.useCurrentTimestamp) {
            jibContainerBuilder.setCreationTime(Instant.now());
        }
        for (int port : jibConfig.ports) {
            jibContainerBuilder.addExposedPort(Port.tcp(port));
        }
        return jibContainerBuilder;
    } catch (InvalidImageReferenceException e) {
        throw new RuntimeException(e);
    }
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Example 45 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project quarkus by quarkusio.

the class JibProcessor method createContainerBuilderFromFastJar.

/**
 * We don't use Jib's JavaContainerBuilder here because we need to support the custom fast-jar format
 * We create the following layers (least likely to change to most likely to change):
 *
 * <ul>
 * <li>lib</li>
 * <li>boot-lib</li>
 * <li>quarkus-run.jar</li>
 * <li>quarkus</li>
 * <li>app</li>
 * </ul>
 */
private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImage, JibConfig jibConfig, ContainerImageConfig containerImageConfig, JarBuildItem sourceJarBuildItem, CurateOutcomeBuildItem curateOutcome, List<ContainerImageLabelBuildItem> containerImageLabels, Optional<AppCDSResultBuildItem> appCDSResult) {
    Path componentsPath = sourceJarBuildItem.getPath().getParent();
    Path appLibDir = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.MAIN);
    AbsoluteUnixPath workDirInContainer = AbsoluteUnixPath.get(jibConfig.workingDirectory);
    List<String> entrypoint;
    if (jibConfig.jvmEntrypoint.isPresent()) {
        entrypoint = jibConfig.jvmEntrypoint.get();
    } else {
        List<String> effectiveJvmArguments = determineEffectiveJvmArguments(jibConfig, appCDSResult);
        entrypoint = new ArrayList<>(3 + effectiveJvmArguments.size());
        entrypoint.add("java");
        entrypoint.addAll(effectiveJvmArguments);
        entrypoint.add("-jar");
        entrypoint.add(JarResultBuildStep.QUARKUS_RUN_JAR);
    }
    List<ResolvedDependency> fastChangingLibs = new ArrayList<>();
    Collection<ResolvedDependency> userDependencies = curateOutcome.getApplicationModel().getRuntimeDependencies();
    for (ResolvedDependency artifact : userDependencies) {
        if (artifact == null) {
            continue;
        }
        String artifactVersion = artifact.getVersion();
        if ((artifactVersion == null) || artifactVersion.isEmpty()) {
            continue;
        }
        if (artifactVersion.toLowerCase().contains("snapshot")) {
            fastChangingLibs.add(artifact);
        }
    }
    Set<Path> fastChangingLibPaths = Collections.emptySet();
    List<Path> nonFastChangingLibPaths = null;
    if (!fastChangingLibs.isEmpty()) {
        fastChangingLibPaths = new HashSet<>(fastChangingLibs.size());
        Map<String, Path> libNameToPath = new HashMap<>();
        try (DirectoryStream<Path> allLibPaths = Files.newDirectoryStream(appLibDir)) {
            for (Path libPath : allLibPaths) {
                libNameToPath.put(libPath.getFileName().toString(), libPath);
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        List<String> libFileNames = new ArrayList<>(libNameToPath.keySet());
        for (ResolvedDependency appArtifact : fastChangingLibs) {
            String matchingLibDirFileName = null;
            for (Path appArtifactPath : appArtifact.getResolvedPaths()) {
                for (String libFileName : libFileNames) {
                    if (libFileName.contains(appArtifact.getGroupId()) && libFileName.contains(appArtifactPath.getFileName().toString())) {
                        matchingLibDirFileName = libFileName;
                        break;
                    }
                }
                if (matchingLibDirFileName != null) {
                    break;
                }
            }
            if (matchingLibDirFileName != null) {
                fastChangingLibPaths.add(libNameToPath.get(matchingLibDirFileName));
            }
        }
        Collection<Path> allLibPaths = libNameToPath.values();
        nonFastChangingLibPaths = new ArrayList<>(allLibPaths.size() - fastChangingLibPaths.size());
        for (Path libPath : allLibPaths) {
            if (!fastChangingLibPaths.contains(libPath)) {
                nonFastChangingLibPaths.add(libPath);
            }
        }
    }
    try {
        Instant now = Instant.now();
        JibContainerBuilder jibContainerBuilder = Jib.from(toRegistryImage(ImageReference.parse(baseJvmImage), jibConfig.baseRegistryUsername, jibConfig.baseRegistryPassword));
        if (fastChangingLibPaths.isEmpty()) {
            // just create a layer with the entire lib structure intact
            jibContainerBuilder.addLayer(Collections.singletonList(componentsPath.resolve(JarResultBuildStep.LIB)), workDirInContainer);
        } else {
            // we need to manually create each layer
            // the idea here is that the fast changing libraries are created in a later layer, thus when they do change,
            // docker doesn't have to create an entire layer with all dependencies - only change the fast ones
            FileEntriesLayer.Builder bootLibsLayerBuilder = FileEntriesLayer.builder();
            Path bootLibPath = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.BOOT_LIB);
            try (Stream<Path> boolLibPaths = Files.list(bootLibPath)) {
                boolLibPaths.forEach(lib -> {
                    try {
                        AbsoluteUnixPath libPathInContainer = workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.BOOT_LIB).resolve(lib.getFileName());
                        if (appCDSResult.isPresent()) {
                            // the boot lib jars need to preserve the modification time because otherwise AppCDS won't work
                            bootLibsLayerBuilder.addEntry(lib, libPathInContainer, Files.getLastModifiedTime(lib).toInstant());
                        } else {
                            bootLibsLayerBuilder.addEntry(lib, libPathInContainer);
                        }
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                });
            }
            jibContainerBuilder.addFileEntriesLayer(bootLibsLayerBuilder.build());
            Path deploymentPath = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.DEPLOYMENT_LIB);
            if (Files.exists(deploymentPath)) {
                // this is the case of mutable-jar
                FileEntriesLayer.Builder deploymentLayerBuilder = FileEntriesLayer.builder();
                Files.list(deploymentPath).forEach(lib -> {
                    AbsoluteUnixPath libPathInContainer = workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.DEPLOYMENT_LIB).resolve(lib.getFileName());
                    deploymentLayerBuilder.addEntry(lib, libPathInContainer);
                });
                jibContainerBuilder.addFileEntriesLayer(deploymentLayerBuilder.build());
            }
            jibContainerBuilder.addLayer(nonFastChangingLibPaths, workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.MAIN));
            jibContainerBuilder.addLayer(new ArrayList<>(fastChangingLibPaths), workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.MAIN));
        }
        if (appCDSResult.isPresent()) {
            jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().addEntry(componentsPath.resolve(JarResultBuildStep.QUARKUS_RUN_JAR), workDirInContainer.resolve(JarResultBuildStep.QUARKUS_RUN_JAR), Files.getLastModifiedTime(componentsPath.resolve(JarResultBuildStep.QUARKUS_RUN_JAR)).toInstant()).build());
            jibContainerBuilder.addLayer(Collections.singletonList(appCDSResult.get().getAppCDS()), workDirInContainer);
        } else {
            jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().addEntry(componentsPath.resolve(JarResultBuildStep.QUARKUS_RUN_JAR), workDirInContainer.resolve(JarResultBuildStep.QUARKUS_RUN_JAR)).build());
        }
        jibContainerBuilder.addLayer(Collections.singletonList(componentsPath.resolve(JarResultBuildStep.APP)), workDirInContainer).addLayer(Collections.singletonList(componentsPath.resolve(JarResultBuildStep.QUARKUS)), workDirInContainer);
        if (JibConfig.DEFAULT_WORKING_DIR.equals(jibConfig.workingDirectory)) {
            // this layer ensures that the working directory is writeable
            // see https://github.com/GoogleContainerTools/jib/issues/1270
            // TODO: is this needed for all working directories?
            jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().addEntry(new FileEntry(Files.createTempDirectory("jib"), AbsoluteUnixPath.get(jibConfig.workingDirectory), FilePermissions.DEFAULT_FOLDER_PERMISSIONS, now, DEFAULT_BASE_IMAGE_USER)).build());
        }
        jibContainerBuilder.setWorkingDirectory(workDirInContainer).setEntrypoint(entrypoint).setEnvironment(getEnvironmentVariables(jibConfig)).setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels));
        if (jibConfig.useCurrentTimestamp) {
            jibContainerBuilder.setCreationTime(now);
        }
        for (int port : jibConfig.ports) {
            jibContainerBuilder.addExposedPort(Port.tcp(port));
        }
        return jibContainerBuilder;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (InvalidImageReferenceException e) {
        throw new RuntimeException(e);
    }
}
Also used : HashMap(java.util.HashMap) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency) ArrayList(java.util.ArrayList) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) UncheckedIOException(java.io.UncheckedIOException) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry)

Aggregations

AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)46 Path (java.nio.file.Path)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)27 IOException (java.io.IOException)18 Test (org.junit.Test)18 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)15 Files (java.nio.file.Files)14 Map (java.util.Map)14 List (java.util.List)13 Instant (java.time.Instant)11 ArrayList (java.util.ArrayList)10 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)9 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 Optional (java.util.Optional)8 Predicate (java.util.function.Predicate)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)7 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)7