Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(build): Refactor BuildService and separate Maven/Docker specific parts. #1116

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# ChangeLog

* **0.27-SNAPSHOT**
* **1.0-SNAPSHOT** [next gen docker-maven-plugin]
- Removed deprecated configuration and renamed configuration
- `<build><command>` has been removed. Use `<build><cmd><exec>` or `<build><cmd><shell>` instead
- `<build><assembly><dockerFileDir>` has been removed. Use `<build><contextDir>` instead.
- `<build><assembly><exportBaseDir>` has been removed. Use `<build><assembly><exportTargetDir>` instead
- `<build><assembly><ignorePermissions>` has been removed. Use `<build><assembly><permissions>ignore</..` instead
- `<build><dockerFileDir>` has been renamed `<build><contextDir>` and semantics has been changed slightly. You can have now a Dockerfile from outside the context directory.
- `<build><assembly><basedir>` has been removed in favor of `<build><assembly><targetDir>`
- `<run><net>` removed, use `<run><network>` for network configuration

* **0.27-SNAPSHOT** [patch releases]
- Update to JMockit 1.43
- Compiles with Java 11
- Update to jnr-unixsocket version to 0.21 (#1089)
Expand Down
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@
<version>1.1</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.17</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down Expand Up @@ -523,6 +529,18 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
</argLine>
</configuration>
</plugin>

</plugins>
</build>
</profile>
Expand Down Expand Up @@ -574,7 +592,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<version>${surefire.version}</version>
<configuration>
<systemPropertyVariables>
<!-- JaCoCo runtime must know where to dump coverage: -->
Expand Down
16 changes: 11 additions & 5 deletions samples/data-jolokia/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<server.version>8</server.version>
<server.name>tomcat</server.name>
<image>docker.io/fabric8/${server.name}-${server.version}:latest</image>
<jolokia.version>1.3.2</jolokia.version>
<jolokia.version>1.3.6</jolokia.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Expand Down Expand Up @@ -96,7 +96,6 @@
<alias>server</alias>
<name>${image}</name>
<run>
<namingStrategy>none</namingStrategy>
<volumes>
<from>
<image>data</image>
Expand All @@ -105,7 +104,7 @@
<env>
<CATALINA_OPTS>-Xmx32m</CATALINA_OPTS>
<!-- Base starts jolokia by default. With this, it can be disabled -->
<JOLOKIA_OFF>1</JOLOKIA_OFF>
<AB_OFF>1</AB_OFF>
<EMPTY_PROP></EMPTY_PROP>
</env>
<envPropertyFile>${project.basedir}/src/main/docker/environment.properties</envPropertyFile>
Expand Down Expand Up @@ -169,6 +168,9 @@
<descriptor>assembly.xml</descriptor>
</assembly>
</build>
<run>
<containerNamePattern>%a-%t</containerNamePattern>
</run>
<watch>
<interval>5000</interval>
<mode>both</mode>
Expand Down Expand Up @@ -255,7 +257,7 @@
<run>
<env>
<CATALINA_OPTS>-Xmx32m</CATALINA_OPTS>
<JOLOKIA_OFF>1</JOLOKIA_OFF>
<AB_OFF>1</AB_OFF>
</env>
<ports>
<!-- Port mappings: Container internal port (which must be exposed) will be
Expand All @@ -275,7 +277,7 @@
</wait>
<log>
<enabled>true</enabled>
<prefix>TC</prefix>
<prefix>TC></prefix>
<color>yellow</color>
</log>
</run>
Expand Down Expand Up @@ -334,6 +336,10 @@
</assembly>
</build>
<run>
<env>
<CATALINA_OPTS>-Xmx32m</CATALINA_OPTS>
<AB_OFF>1</AB_OFF>
</env>
<ports>
<port>jolokia.port:8080</port>
</ports>
Expand Down
14 changes: 14 additions & 0 deletions samples/dockerfile/Dockerfile-3
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Sample Dockerfile for use with the Docker file mode
FROM ${base}

ENV SAMPLE_BUILD_MODE=dockerfile \
DOCKERFILE_NAME="src/main/Dockerfile-3"

# Arbitrary files can be added
ADD welcome.txt /

# In maven/ the files as specified in the <assembly> section is stored
# and need to be added manually
COPY maven/ /var/lib/jetty/webapps/

EXPOSE 8080
46 changes: 45 additions & 1 deletion samples/dockerfile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<alias>dockerfile</alias>
<build>
<!-- filter>@</filter-->
<dockerFileDir>${project.basedir}/src/main/docker</dockerFileDir>
<contextDir>${project.basedir}/src/main/docker</contextDir>
<assembly>
<descriptorRef>rootWar</descriptorRef>
</assembly>
Expand All @@ -74,4 +74,48 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>alternate</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<build>
<dockerFile>Dockerfile-2</dockerFile>
</build>
</image>
</images>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>outside</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<build>
<dockerFile>${project.basedir}/Dockerfile-3</dockerFile>
</build>
</image>
</images>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
4 changes: 3 additions & 1 deletion samples/dockerfile/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Sample Dockerfile for use with the Docker file mode
FROM ${base}

ENV SAMPLE_BUILD_MODE=dockerfile
ENV SAMPLE_BUILD_MODE=dockerfile \
DOCKERFILE_NAME="src/main/Dockerfile"

LABEL PROJECT_NAME=hello-world \
PROJECT=${project.artifactId}

Expand Down
14 changes: 14 additions & 0 deletions samples/dockerfile/src/main/docker/Dockerfile-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Sample Dockerfile for use with the Docker file mode
FROM ${base}

ENV SAMPLE_BUILD_MODE=dockerfile \
DOCKERFILE_NAME="src/main/Dockerfile-2"

# Arbitrary files can be added
ADD welcome.txt /

# In maven/ the files as specified in the <assembly> section is stored
# and need to be added manually
COPY maven/ /var/lib/jetty/webapps/

EXPOSE 8080
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String txt = FileUtils.readFileToString(new File("/welcome.txt"), Charset.defaultCharset());
resp.getWriter().append(txt).flush();
resp.getWriter().append(txt).append("\nFile: ").append(System.getenv("DOCKERFILE_NAME")).flush();
resp.setHeader("Content-Type", "plain/text");
}
}
54 changes: 32 additions & 22 deletions src/main/java/io/fabric8/maven/docker/AbstractBuildSupportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import java.util.List;
import java.util.Map;

import io.fabric8.maven.docker.service.BuildService;
import io.fabric8.maven.docker.util.MojoParameters;
import io.fabric8.maven.docker.build.maven.MavenArchiveService;
import io.fabric8.maven.docker.build.maven.MavenBuildContext;
import io.fabric8.maven.docker.build.maven.MavenRegistryContext;
import io.fabric8.maven.docker.config.build.ImagePullPolicy;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
Expand All @@ -25,41 +26,50 @@ abstract public class AbstractBuildSupportMojo extends AbstractDockerMojo {
// See also here: http://maven.40175.n5.nabble.com/Mojo-Java-1-5-Component-MavenProject-returns-null-vs-JavaDoc-parameter-expression-quot-project-quot-s-td5733805.html

@Parameter
private MavenArchiveConfiguration archive;
protected MavenArchiveConfiguration archive;

@Component
private MavenFileFilter mavenFileFilter;
protected MavenFileFilter mavenFileFilter;

@Component
private MavenReaderFilter mavenFilterReader;
protected MavenReaderFilter mavenReaderFilter;

@Parameter
private Map<String, String> buildArgs;
protected Map<String, String> buildArgs;

@Parameter(property = "docker.pull.registry")
private String pullRegistry;
protected String pullRegistry;

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

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

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


protected BuildService.BuildContext getBuildContext() throws MojoExecutionException {
return new BuildService.BuildContext.Builder()
.buildArgs(buildArgs)
.mojoParameters(createMojoParameters())
.registryConfig(getRegistryConfig(pullRegistry))
.build();
}
protected MavenBuildContext getBuildContext(MavenArchiveService archiveService) {

protected MojoParameters createMojoParameters() {
return new MojoParameters(session, project, archive, mavenFileFilter, mavenFilterReader,
settings, sourceDirectory, outputDirectory, reactorProjects);
}
MavenRegistryContext registryContext = new MavenRegistryContext.Builder()
.authRegistryAuthFactory(registryAuthFactory)
.defaultImagePullPolicy(imagePullPolicy != null ? ImagePullPolicy.fromString(imagePullPolicy) : null)
.pullRegistry(pullRegistry)
.build();

return new MavenBuildContext.Builder()
.project(project)
.sourceDirectory(sourceDirectory)
.outputDirectory(outputDirectory)
.session(session)
.settings(settings)
.mavenFileFilter(mavenFileFilter)
.mavenReaderFilter(mavenReaderFilter)
.reactorProjects(reactorProjects)
.archiveConfiguration(archive)
.archiveService(archiveService)
.registryContext(registryContext)
.build();
}
}
Loading