Skip to content

Commit

Permalink
Merge pull request #290 from jmesnil/WFMP-186_optimize_docker_image
Browse files Browse the repository at this point in the history
[WFMP-186] Optimize the application image generated by the image goal
  • Loading branch information
jamezp authored Feb 23, 2023
2 parents 96c623f + aaf603a commit 7c74da6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ protected String getGoal() {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// when the application image is built, the deployment step is skipped.
// This allows to create 2 different Docker layers (1 for the server and 1 for the deployments)
this.skipDeployment = true;

super.execute();

if (image == null) {
Expand Down Expand Up @@ -147,7 +151,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

String image = this.image.getApplicationImageName(project.getArtifactId());

boolean buildSuccess = buildApplicationImage(image, runtimeImage);
boolean buildSuccess = buildApplicationImage(image);
if (!buildSuccess) {
throw new MojoExecutionException(String.format("Unable to build application image %s", image));
}
Expand Down Expand Up @@ -188,7 +192,7 @@ private void logToRegistry() throws MojoExecutionException {
}
}

private boolean buildApplicationImage(String image, String runtimeImage) throws IOException {
private boolean buildApplicationImage(String image) throws IOException {
getLog().info(format("Building application image %s using %s.", image, this.image.getDockerBinary()));
String[] dockerArgs = new String[] {"build", "-t", image, "."};

Expand All @@ -206,11 +210,14 @@ private boolean pushApplicationImage(String image) {
return ExecUtil.exec(getLog(), Paths.get(project.getBuild().getDirectory()).toFile(), this.image.getDockerBinary(), dockerArgs);
}

private void generateDockerfile(String runtimeImage, Path targetDir, String wildflyDirectory) throws IOException {
private void generateDockerfile(String runtimeImage, Path targetDir, String wildflyDirectory) throws IOException, MojoExecutionException {

String targetName = getDeploymentTargetName();
Files.writeString(targetDir.resolve("Dockerfile"),
"FROM " + runtimeImage + "\n" +
"COPY --chown=jboss:root " + wildflyDirectory + " $JBOSS_HOME\n" +
"RUN chmod -R ug+rwX $JBOSS_HOME",
"RUN chmod -R ug+rwX $JBOSS_HOME\n" +
"COPY --chown=jboss:root " + getDeploymentContent().getFileName() + " $JBOSS_HOME/standalone/deployments/" + targetName,
StandardCharsets.UTF_8);
}

Expand All @@ -228,5 +235,4 @@ private boolean isImageBinaryAvailable(String imageBinary) {

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,7 @@ protected void serverProvisioned(Path jbossHome) throws MojoExecutionException,
if (Files.exists(deploymentContent)) {
Path standaloneDeploymentDir = Paths.get(project.getBuild().getDirectory(), provisioningDir, "standalone", "deployments").normalize();
try {
String targetName;
if (runtimeName != null) {
targetName = runtimeName;
} else {
targetName = name != null ? name : deploymentContent.getFileName().toString();
}
Path deploymentTarget = standaloneDeploymentDir.resolve(targetName);
Path deploymentTarget = standaloneDeploymentDir.resolve(getDeploymentTargetName());
getLog().info("Copy deployment " + deploymentContent + " to " + deploymentTarget);
Files.copy(deploymentContent, deploymentTarget, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
Expand Down Expand Up @@ -261,6 +255,20 @@ protected void serverProvisioned(Path jbossHome) throws MojoExecutionException,
}
}

/** Return the file name of the deployment to put in the server deployment directory
*
* @throws MojoExecutionException
*/
protected String getDeploymentTargetName() throws MojoExecutionException {
String targetName;
if (runtimeName != null) {
targetName = runtimeName;
} else {
targetName = name != null ? name : getDeploymentContent().getFileName().toString();
}
return targetName;
}

private List<File> resolveFiles(List<File> files) {
if (files == null || files.isEmpty()) {
return files;
Expand Down Expand Up @@ -335,7 +343,7 @@ private void warnExtraConfig(Path extraContentDir) {
}
}

private Path getDeploymentContent() throws MojoExecutionException {
protected Path getDeploymentContent() throws MojoExecutionException {
final PackageType packageType = PackageType.resolve(project);
final String filename;
if (this.filename == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<layer>jaxrs-server</layer>
</layers>
<provisioning-dir>image-server</provisioning-dir>
<filename>test.war</filename>
<image>
<group>wildfly-maven-plugin</group>
</image>
Expand Down

0 comments on commit 7c74da6

Please sign in to comment.