Skip to content

Commit

Permalink
Updated the version and made it SNAPSHOT
Browse files Browse the repository at this point in the history
BuildDockerImageMojo - removed all hardcoded values and replaced with default values from pom. Default version to version of pom.
  • Loading branch information
Phillip Kruger committed Aug 9, 2017
1 parent a45f96a commit cfede4e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties

/target/
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.mmiholdings.k8.plugin</groupId>
<artifactId>mmik8-maven-plugin</artifactId>
<version>1.0.1</version>
<version>1.0.2-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<developers>
Expand All @@ -16,6 +16,14 @@
<role>Architect</role>
</roles>
</developer>
<developer>
<name>Phillip Kruger</name>
<id>phillip-kruger</id>
<email>phillip.kruger@phillip-kruger.com</email>
<roles>
<role>Architect</role>
</roles>
</developer>
</developers>

<name>mmik8-maven-plugin Maven Plugin</name>
Expand Down
115 changes: 74 additions & 41 deletions src/main/java/com/mmiholdings/k8/plugin/BuildDockerImageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,117 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.LifecyclePhase;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Mojo(name = "buildImage")
public class BuildDockerImageMojo extends AbstractMojo {


/**
* Maven Plug-in to build the docker image.
* @see https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
*/
@Mojo(name = "buildImage",defaultPhase = LifecyclePhase.INSTALL)
public class BuildDockerImageMojo extends AbstractMojo {

/**
* target as defined in pom.
*/
@Parameter(defaultValue = "${project.build.directory}", readonly = true, required=false)
private File target;

/**
* imageName
* imageName - default to artifactId.
*/
@Parameter( property = "imageName", defaultValue = "Hello World!" )
@Parameter( property = "imageName", defaultValue = "${project.artifactId}", readonly=true, required=false)
private String imageName;

/**
* imageVersion
* imageVersion - default to project version.
*/
@Parameter( property = "imageVersion", defaultValue = "Hello World!" )
@Parameter( property = "imageVersion", defaultValue = "${project.version}", readonly=true, required=false)
private String imageVersion;


@Parameter( property = "artefactName", defaultValue = "Hello World!" )
@Parameter( property = "dockerConfDir", defaultValue = "${project.basedir}/src/main/docker", readonly=true, required=false)
private String dockerConfDir;

/**
* artefactName - the name of the distributable unit. Default to build.finalName
*/
@Parameter( property = "artefactName", defaultValue = "${project.build.finalName}", readonly=true, required=false)
private String artefactName;

@Parameter( property = "artefactVersion", defaultValue = "Hello World!" )
private String artefactVersion;


ProcessBuilderHelper processBuilderHelper = new ProcessBuilderHelper(getLog());

public void execute()
throws MojoExecutionException
{
getLog().info("Building Image");
String s = Paths.get(".").toAbsolutePath().normalize().toString();
String dockerDirectory = s + "/src/main/docker/";
String fullyQualifiedImageName = imageName + ":" + imageVersion;
execute(dockerDirectory, fullyQualifiedImageName);

/**
* artefactType - the type of the distributable unit (jar / war / ear). Default to packaging
*/
@Parameter( property = "artefactType", defaultValue = "${project.packaging}", readonly=true, required=false)
private String artefactType;

private final ProcessBuilderHelper processBuilderHelper = new ProcessBuilderHelper(getLog());
private String deployableUnit;

@Override
public void execute() throws MojoExecutionException {
info("Building Image using Dockerfile in [" + dockerConfDir + "]");

File f = new File(target,artefactName + DOT + artefactType);
this.deployableUnit = f.getAbsolutePath();
info("Including deployable unit [" + deployableUnit + "]");

String fullyQualifiedImageName = imageName + DOUBLE_DOT + imageVersion;
execute(dockerConfDir, fullyQualifiedImageName);
}

private void execute(String dockerDirectory, String imageName) {
try {
if (processBuilderHelper.contains(dockerDirectory, "Dockerfile")) {
if (processBuilderHelper.contains(dockerDirectory, DOCKERFILE)) { // TODO: Make configuratable ?, or loop through all Dockerfiles?
runCopyArtefactCommand(dockerDirectory);
runDockerBuildCommand(dockerDirectory, imageName);
runRemoveArtefactCommand(dockerDirectory);
}
} catch (Exception e) {
} catch (IOException | InterruptedException e) {
getLog().error(e);
}
}

private void runCopyArtefactCommand(String dockerDirectory) throws InterruptedException, IOException {
List<String> command = new ArrayList<String>();
command.add("cp");
command.add("../../../target/" + artefactName + "-" + artefactVersion + ".war");
command.add(".");
List<String> command = new ArrayList<>();
command.add(COPY);
command.add(deployableUnit);
command.add(DOT);
processBuilderHelper.executeCommand(dockerDirectory, command);
}

private void runRemoveArtefactCommand(String dockerDirectory) throws InterruptedException, IOException {
List<String> command = new ArrayList<String>();
command.add("rm");
command.add(artefactName + "-" + artefactVersion + ".war");
List<String> command = new ArrayList<>();
command.add(REMOVE);
command.add(artefactName + DOT + artefactType);
processBuilderHelper.executeCommand(dockerDirectory, command);
}


private void runDockerBuildCommand(String dockerDirectory, String imageName) throws InterruptedException, IOException {
List<String> command = new ArrayList<String>();
command.add("docker");
command.add("build");
command.add("-t");
List<String> command = new ArrayList<>();
command.add(DOCKER);
command.add(BUILD);
command.add(MINUS_T);
command.add(imageName);
command.add(".");
info("Executing [docker build -t " + imageName + "]");
command.add(DOT);
processBuilderHelper.executeCommand(dockerDirectory, command);
}


private void info(String msg){
getLog().info(msg);
}

private static final String DOT = ".";
private static final String DOUBLE_DOT = ":";
private static final String COPY = "cp"; // TODO: Is this Linux specific ? What about poor Windoze users ?
private static final String REMOVE = "rm"; // TODO: Is this Linux specific ? What about poor Windoze users ?
private static final String DOCKER = "docker";
private static final String BUILD = "build";
private static final String MINUS_T = "-t";
private static final String DOCKERFILE = "Dockerfile";
}

0 comments on commit cfede4e

Please sign in to comment.