Skip to content

Commit

Permalink
Support for JIB(Java Image Builder) mode in dmp
Browse files Browse the repository at this point in the history
This ports Jib support added in  https://github.com/eclipse/jkube
  • Loading branch information
rohanKanojia committed Sep 4, 2020
1 parent 493aa8b commit 676cfba
Show file tree
Hide file tree
Showing 19 changed files with 1,412 additions and 14 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix JSON error when parsin tafs (#1354)
- Add `skipPush` option to build image configuration ([#1243](https://github.com/fabric8io/docker-maven-plugin/issues/1243))
- Support `squash` in build options to squash newly built layers into a single layer ([#785](https://github.com/fabric8io/docker-maven-plugin/issues/785))
- Support for JIB mode([#1277](https://github.com/fabric8io/docker-maven-plugin/pull/1277))

* **0.33.0** (2020-01-21)
- Update to jnr-unixsocket 0.25 to solve concurrency issues ([#552](https://github.com/fabric8io/docker-maven-plugin/issues/552))
Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<maven.version>3.3.9</maven.version>
<jmockit.version>1.43</jmockit.version>
<surefire.version>3.0.0-M2</surefire.version>
<jib-core.version>0.12.0</jib-core.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -157,6 +158,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-core</artifactId>
<version>${jib-core.version}</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand All @@ -166,7 +173,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-android</version>
<version>27.0.1-jre</version>
</dependency>

<dependency>
Expand Down
274 changes: 274 additions & 0 deletions samples/spring-boot-with-jib/README.md

Large diffs are not rendered by default.

159 changes: 159 additions & 0 deletions samples/spring-boot-with-jib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.fabric8.dmp.samples</groupId>
<artifactId>dmp-sample-parent</artifactId>
<version>0.33-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dmp-sample-spring-boot-jib</artifactId>
<version>0.33-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Docker Maven Plugin :: Spring Boot JIB</name>
<description>Docker Maven Plugin Example with Spring Boot With Build Mode JIB</description>

<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<docker.build.jib>true</docker.build.jib>
<docker.user>fabric8</docker.user>
<spring.version>2.3.3.RELEASE</spring.version>
</properties>

<dependencies>

<!-- Boot generator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- API, java.xml.bind module -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
</dependency>

<!-- Runtime, com.sun.xml.bind module -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33-SNAPSHOT</version>
<configuration>
<images>
<image>
<name>${docker.user}/spring-boot-dmp-sample-jib</name>
<build>
<from>fabric8/java-centos-openjdk8-jdk:1.5.6</from>
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<cmd>java -jar /maven/${project.artifactId}-${project.version}.jar</cmd>
<ports>
<port>8080</port>
</ports>
</build>
</image>
</images>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>Jib-With-Assembly</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33-SNAPSHOT</version>
<configuration>
<images>
<image>
<name>${docker.user}/spring-boot-dmp-sample-jib</name>
<build>
<from>fabric8/java-centos-openjdk8-jdk:1.5.6</from>
<assembly>
<name>my-project-assembly</name>
<inline>
<id>copy-test-file</id>
<files>
<file>
<source>
${project.basedir}/static/testFile.txt
</source>
<outputDirectory>static</outputDirectory>
</file>
<file>
<source>${project.basedir}/target/${project.artifactId}-${project.version}.jar</source>
<outputDirectory>.</outputDirectory>
</file>
</files>
</inline>
<targetDir>/deployments</targetDir>
</assembly>
<cmd>java -jar /my-project-assembly/${project.artifactId}-${project.version}.jar</cmd>
<ports>
<port>8080</port>
</ports>
</build>
</image>
</images>
</configuration>
</plugin>

</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.fabric8.maven.sample.springboot.jib;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.fabric8.maven.sample.springboot.jib;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {

@RequestMapping("/")
public String index() {
return "<h1>Greetings from Spring Boot(Powered by JIB)!!</h1>";
}

}
1 change: 1 addition & 0 deletions samples/spring-boot-with-jib/static/testFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I should be present
4 changes: 4 additions & 0 deletions src/main/asciidoc/inc/_global-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ By default a progress meter is printed out on the console, which is omitted when
| Number of parallel connections are allowed to be opened to the Docker Host. For parsing log output, a connection needs to be kept open (as well for the wait features), so don't put that number to low. Default is 100 which should be suitable for most of the cases.
| `docker.maxConnections`

| *jib*
| Delegate Image Build process to https://github.com/GoogleContainerTools/jib[JIB], `false` by default. Note that this option is applicable only for <<docker:build,build>> and <<docker:push,push>> goals, other goals won't work if this is enabled(since they are dependent on docker)
| `docker.build.jib`

| *outputDirectory*
| Default output directory to be used by this plugin. The default value is `target/docker` and is only used for the goal `{plugin}:build`.
| `docker.target.dir`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ abstract public class AbstractBuildSupportMojo extends AbstractDockerMojo {
@Parameter(property = "docker.pull.registry")
private String pullRegistry;

@Parameter(property = "docker.source.dir", defaultValue="src/main/docker")
private String sourceDirectory;

@Parameter(property = "docker.target.dir", defaultValue="target/docker")
private String outputDirectory;

@Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
private List<MavenProject> reactorProjects;

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/io/fabric8/maven/docker/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.fabric8.maven.docker.util.GavLabel;
import io.fabric8.maven.docker.util.ImageNameFormatter;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.MojoParameters;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
Expand Down Expand Up @@ -167,6 +168,15 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
@Parameter(property = "docker.maxConnections", defaultValue = "100")
private int maxConnections;

@Parameter(property = "docker.build.jib", defaultValue = "false")
public boolean jib;

@Parameter(property = "docker.source.dir", defaultValue="src/main/docker")
public String sourceDirectory;

@Parameter(property = "docker.target.dir", defaultValue="target/docker")
public String outputDirectory;

// Authentication information
@Parameter
private RegistryAuthConfiguration authConfig;
Expand Down Expand Up @@ -370,7 +380,7 @@ public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> imageCo
* @return <code >true</code> as the default value
*/
protected boolean isDockerAccessRequired() {
return true;
return Boolean.FALSE.equals(jib);
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/io/fabric8/maven/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.service.BuildService;
import io.fabric8.maven.docker.service.ImagePullManager;
import io.fabric8.maven.docker.service.JibBuildService;
import io.fabric8.maven.docker.service.ServiceHub;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.EnvUtil;
Expand Down Expand Up @@ -75,8 +76,23 @@ protected void buildAndTag(ServiceHub hub, ImageConfiguration imageConfig)

BuildService.BuildContext buildContext = getBuildContext();
ImagePullManager pullManager = getImagePullManager(determinePullPolicy(imageConfig.getBuildConfiguration()), autoPull);
BuildService buildService = hub.getBuildService();
proceedWithBuildProcess(hub, buildContext, imageConfig, pullManager);
}

private void proceedWithBuildProcess(ServiceHub hub, BuildService.BuildContext buildContext, ImageConfiguration imageConfig, ImagePullManager pullManager) throws MojoExecutionException, IOException {
if (Boolean.TRUE.equals(jib)) {
proceedWithJibBuild(hub, buildContext, imageConfig);
} else {
proceedWithDockerBuild(hub.getBuildService(), buildContext, imageConfig, pullManager);
}
}

private void proceedWithJibBuild(ServiceHub hub, BuildService.BuildContext buildContext, ImageConfiguration imageConfig) throws MojoExecutionException {
log.info("Building Container image with [[B]]JIB(Java Image Builder)[[B]] mode");
new JibBuildService(hub, createMojoParameters(), log).build(imageConfig, buildContext.getRegistryConfig());
}

private void proceedWithDockerBuild(BuildService buildService, BuildService.BuildContext buildContext, ImageConfiguration imageConfig, ImagePullManager pullManager) throws MojoExecutionException, IOException {
File buildArchiveFile = buildService.buildArchive(imageConfig, buildContext, resolveBuildArchiveParameter());
if (Boolean.FALSE.equals(shallBuildArchiveOnly())) {
buildService.buildImage(imageConfig, pullManager, buildContext, buildArchiveFile);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/fabric8/maven/docker/PushMojo.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.fabric8.maven.docker;

import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.service.BuildService;
import io.fabric8.maven.docker.service.JibBuildService;
import io.fabric8.maven.docker.service.ServiceHub;

import io.fabric8.maven.docker.util.MojoParameters;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -41,6 +45,22 @@ public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoEx
return;
}

if (Boolean.TRUE.equals(jib)) {
executeJibPush(hub);
} else {
executeDockerPush(hub);
}
}

private void executeDockerPush(ServiceHub hub) throws MojoExecutionException, DockerAccessException {
hub.getRegistryService().pushImages(getResolvedImages(), retries, getRegistryConfig(pushRegistry), skipTag);
}

private void executeJibPush(ServiceHub hub) throws MojoExecutionException {
log.info("Pushing Container image with [[B]]JIB(Java Image Builder)[[B]] mode");
JibBuildService jibBuildService = new JibBuildService(hub, new MojoParameters(session, project, null, null, null,
settings, sourceDirectory, outputDirectory, null), log);
jibBuildService.push(getResolvedImages(), retries, getRegistryConfig(pushRegistry), skipTag);
}

}
10 changes: 5 additions & 5 deletions src/main/java/io/fabric8/maven/docker/assembly/BuildDirs.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author roland
* @since 27/02/15
*/
class BuildDirs {
public class BuildDirs {

private final String buildTopDir;
private final MojoParameters params;
Expand All @@ -38,22 +38,22 @@ class BuildDirs {
* @param imageName image name for the image to build
* @param params mojo params holding base and global outptput dir
*/
BuildDirs(String imageName, MojoParameters params) {
public BuildDirs(String imageName, MojoParameters params) {
this.params = params;
// Replace tag separator with a slash to avoid problems
// with OSs which gets confused by colons.
this.buildTopDir = imageName != null ? imageName.replace(':', '/') : null;
}

File getOutputDirectory() {
public File getOutputDirectory() {
return getDir("build");
}

File getWorkingDirectory() {
public File getWorkingDirectory() {
return getDir("work");
}

File getTemporaryRootDirectory() {
public File getTemporaryRootDirectory() {
return getDir("tmp");
}

Expand Down
Loading

0 comments on commit 676cfba

Please sign in to comment.