Search in sources :

Example 1 with LayerType

use of com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType in project jib by google.

the class MavenProjectProperties method classifyDependencies.

@VisibleForTesting
Map<LayerType, List<Path>> classifyDependencies(Set<Artifact> dependencies, Set<Artifact> projectArtifacts) {
    Map<LayerType, List<Path>> classifiedDependencies = new HashMap<>();
    classifiedDependencies.put(LayerType.DEPENDENCIES, new ArrayList<>());
    classifiedDependencies.put(LayerType.SNAPSHOT_DEPENDENCIES, new ArrayList<>());
    classifiedDependencies.put(LayerType.PROJECT_DEPENDENCIES, new ArrayList<>());
    for (Artifact artifact : dependencies) {
        if (projectArtifacts.contains(artifact)) {
            classifiedDependencies.get(LayerType.PROJECT_DEPENDENCIES).add(artifact.getFile().toPath());
        } else if (artifact.isSnapshot()) {
            classifiedDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES).add(artifact.getFile().toPath());
        } else {
            classifiedDependencies.get(LayerType.DEPENDENCIES).add(artifact.getFile().toPath());
        }
    }
    return classifiedDependencies;
}
Also used : HashMap(java.util.HashMap) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) ArrayList(java.util.ArrayList) Artifact(org.apache.maven.artifact.Artifact) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with LayerType

use of com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType in project jib by google.

the class MavenProjectProperties method createJibContainerBuilder.

@Override
public JibContainerBuilder createJibContainerBuilder(JavaContainerBuilder javaContainerBuilder, ContainerizingMode containerizingMode) throws IOException {
    try {
        if (isWarProject()) {
            Path war = getWarArtifact();
            Path explodedWarPath = tempDirectoryProvider.newDirectory();
            ZipUtil.unzip(war, explodedWarPath);
            return JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, explodedWarPath, getProjectDependencies().stream().map(Artifact::getFile).map(File::getName).collect(Collectors.toSet()));
        }
        switch(containerizingMode) {
            case EXPLODED:
                // Add resources, and classes
                Path classesOutputDirectory = Paths.get(project.getBuild().getOutputDirectory());
                // Don't use Path.endsWith(), since Path works on path elements.
                Predicate<Path> isClassFile = path -> path.getFileName().toString().endsWith(".class");
                javaContainerBuilder.addResources(classesOutputDirectory, isClassFile.negate()).addClasses(classesOutputDirectory, isClassFile);
                break;
            case PACKAGED:
                // Add a JAR
                javaContainerBuilder.addToClasspath(getJarArtifact());
                break;
            default:
                throw new IllegalStateException("unknown containerizing mode: " + containerizingMode);
        }
        // Classify and add dependencies
        Map<LayerType, List<Path>> classifiedDependencies = classifyDependencies(project.getArtifacts(), getProjectDependencies());
        javaContainerBuilder.addDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.DEPENDENCIES)));
        javaContainerBuilder.addSnapshotDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES)));
        javaContainerBuilder.addProjectDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.PROJECT_DEPENDENCIES)));
        return javaContainerBuilder.toContainerBuilder();
    } catch (IOException ex) {
        throw new IOException("Obtaining project build output files failed; make sure you have " + (isPackageErrorMessage(containerizingMode) ? "packaged" : "compiled") + " your project " + "before trying to build the image. (Did you accidentally run \"mvn clean " + "jib:build\" instead of \"mvn clean " + (isPackageErrorMessage(containerizingMode) ? "package" : "compile") + " jib:build\"?)", ex);
    }
}
Also used : Path(java.nio.file.Path) JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) JavaContainerBuilderHelper(com.google.cloud.tools.jib.plugins.common.JavaContainerBuilderHelper) ImageReference(com.google.cloud.tools.jib.api.ImageReference) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) PropertyNames(com.google.cloud.tools.jib.plugins.common.PropertyNames) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) TimerEventHandler(com.google.cloud.tools.jib.plugins.common.TimerEventHandler) MavenProject(org.apache.maven.project.MavenProject) Duration(java.time.Duration) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact) Path(java.nio.file.Path) Verify(com.google.common.base.Verify) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) TimerEvent(com.google.cloud.tools.jib.event.events.TimerEvent) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) PluginExtensionLogger(com.google.cloud.tools.jib.plugins.common.PluginExtensionLogger) Collectors(java.util.stream.Collectors) ZipUtil(com.google.cloud.tools.jib.plugins.common.ZipUtil) ContainerizingMode(com.google.cloud.tools.jib.plugins.common.ContainerizingMode) ProjectProperties(com.google.cloud.tools.jib.plugins.common.ProjectProperties) List(java.util.List) ProgressDisplayGenerator(com.google.cloud.tools.jib.plugins.common.logging.ProgressDisplayGenerator) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) NullExtension(com.google.cloud.tools.jib.plugins.extension.NullExtension) PluginExecution(org.apache.maven.model.PluginExecution) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) HashMap(java.util.HashMap) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ExtensionConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtensionConfiguration) Os(org.apache.maven.shared.utils.Os) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) Nullable(javax.annotation.Nullable) ConsoleLoggerBuilder(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLoggerBuilder) MavenSession(org.apache.maven.execution.MavenSession) Files(java.nio.file.Files) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) ProgressEventHandler(com.google.cloud.tools.jib.event.progress.ProgressEventHandler) Log(org.apache.maven.plugin.logging.Log) File(java.io.File) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Plugin(org.apache.maven.model.Plugin) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 3 with LayerType

use of com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType in project jib by GoogleContainerTools.

the class MavenProjectProperties method classifyDependencies.

@VisibleForTesting
Map<LayerType, List<Path>> classifyDependencies(Set<Artifact> dependencies, Set<Artifact> projectArtifacts) {
    Map<LayerType, List<Path>> classifiedDependencies = new HashMap<>();
    classifiedDependencies.put(LayerType.DEPENDENCIES, new ArrayList<>());
    classifiedDependencies.put(LayerType.SNAPSHOT_DEPENDENCIES, new ArrayList<>());
    classifiedDependencies.put(LayerType.PROJECT_DEPENDENCIES, new ArrayList<>());
    for (Artifact artifact : dependencies) {
        if (projectArtifacts.contains(artifact)) {
            classifiedDependencies.get(LayerType.PROJECT_DEPENDENCIES).add(artifact.getFile().toPath());
        } else if (artifact.isSnapshot()) {
            classifiedDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES).add(artifact.getFile().toPath());
        } else {
            classifiedDependencies.get(LayerType.DEPENDENCIES).add(artifact.getFile().toPath());
        }
    }
    return classifiedDependencies;
}
Also used : HashMap(java.util.HashMap) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) ArrayList(java.util.ArrayList) Artifact(org.apache.maven.artifact.Artifact) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with LayerType

use of com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType in project jib by GoogleContainerTools.

the class MavenProjectProperties method createJibContainerBuilder.

@Override
public JibContainerBuilder createJibContainerBuilder(JavaContainerBuilder javaContainerBuilder, ContainerizingMode containerizingMode) throws IOException {
    try {
        if (isWarProject()) {
            Path war = getWarArtifact();
            Path explodedWarPath = tempDirectoryProvider.newDirectory();
            ZipUtil.unzip(war, explodedWarPath);
            return JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, explodedWarPath, getProjectDependencies().stream().map(Artifact::getFile).map(File::getName).collect(Collectors.toSet()));
        }
        switch(containerizingMode) {
            case EXPLODED:
                // Add resources, and classes
                Path classesOutputDirectory = Paths.get(project.getBuild().getOutputDirectory());
                // Don't use Path.endsWith(), since Path works on path elements.
                Predicate<Path> isClassFile = path -> path.getFileName().toString().endsWith(".class");
                javaContainerBuilder.addResources(classesOutputDirectory, isClassFile.negate()).addClasses(classesOutputDirectory, isClassFile);
                break;
            case PACKAGED:
                // Add a JAR
                javaContainerBuilder.addToClasspath(getJarArtifact());
                break;
            default:
                throw new IllegalStateException("unknown containerizing mode: " + containerizingMode);
        }
        // Classify and add dependencies
        Map<LayerType, List<Path>> classifiedDependencies = classifyDependencies(project.getArtifacts(), getProjectDependencies());
        javaContainerBuilder.addDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.DEPENDENCIES)));
        javaContainerBuilder.addSnapshotDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES)));
        javaContainerBuilder.addProjectDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.PROJECT_DEPENDENCIES)));
        return javaContainerBuilder.toContainerBuilder();
    } catch (IOException ex) {
        throw new IOException("Obtaining project build output files failed; make sure you have " + (isPackageErrorMessage(containerizingMode) ? "packaged" : "compiled") + " your project " + "before trying to build the image. (Did you accidentally run \"mvn clean " + "jib:build\" instead of \"mvn clean " + (isPackageErrorMessage(containerizingMode) ? "package" : "compile") + " jib:build\"?)", ex);
    }
}
Also used : Path(java.nio.file.Path) JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) JavaContainerBuilderHelper(com.google.cloud.tools.jib.plugins.common.JavaContainerBuilderHelper) ImageReference(com.google.cloud.tools.jib.api.ImageReference) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) PropertyNames(com.google.cloud.tools.jib.plugins.common.PropertyNames) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) TimerEventHandler(com.google.cloud.tools.jib.plugins.common.TimerEventHandler) MavenProject(org.apache.maven.project.MavenProject) Duration(java.time.Duration) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact) Path(java.nio.file.Path) Verify(com.google.common.base.Verify) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) TimerEvent(com.google.cloud.tools.jib.event.events.TimerEvent) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) PluginExtensionLogger(com.google.cloud.tools.jib.plugins.common.PluginExtensionLogger) Collectors(java.util.stream.Collectors) ZipUtil(com.google.cloud.tools.jib.plugins.common.ZipUtil) ContainerizingMode(com.google.cloud.tools.jib.plugins.common.ContainerizingMode) ProjectProperties(com.google.cloud.tools.jib.plugins.common.ProjectProperties) List(java.util.List) ProgressDisplayGenerator(com.google.cloud.tools.jib.plugins.common.logging.ProgressDisplayGenerator) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) NullExtension(com.google.cloud.tools.jib.plugins.extension.NullExtension) PluginExecution(org.apache.maven.model.PluginExecution) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) HashMap(java.util.HashMap) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ExtensionConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtensionConfiguration) Os(org.apache.maven.shared.utils.Os) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) Nullable(javax.annotation.Nullable) ConsoleLoggerBuilder(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLoggerBuilder) MavenSession(org.apache.maven.execution.MavenSession) Files(java.nio.file.Files) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) ProgressEventHandler(com.google.cloud.tools.jib.event.progress.ProgressEventHandler) Log(org.apache.maven.plugin.logging.Log) File(java.io.File) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Plugin(org.apache.maven.model.Plugin) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Aggregations

LayerType (com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Artifact (org.apache.maven.artifact.Artifact)4 Containerizer (com.google.cloud.tools.jib.api.Containerizer)2 ImageReference (com.google.cloud.tools.jib.api.ImageReference)2 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)2 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)2 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)2 LogEvent (com.google.cloud.tools.jib.api.LogEvent)2 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)2 ProgressEvent (com.google.cloud.tools.jib.event.events.ProgressEvent)2 TimerEvent (com.google.cloud.tools.jib.event.events.TimerEvent)2 ProgressEventHandler (com.google.cloud.tools.jib.event.progress.ProgressEventHandler)2 DirectoryWalker (com.google.cloud.tools.jib.filesystem.DirectoryWalker)2 TempDirectoryProvider (com.google.cloud.tools.jib.filesystem.TempDirectoryProvider)2 JibMavenPluginExtension (com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension)2 ContainerizingMode (com.google.cloud.tools.jib.plugins.common.ContainerizingMode)2