Skip to content

Commit

Permalink
Merge pull request #97 from JimPanse81/contextdir
Browse files Browse the repository at this point in the history
Added support to define custom context directory instead of '.' (is still default when omitted)
  • Loading branch information
lexemmens committed Apr 16, 2024
2 parents 849a470 + 49011bc commit 16225f6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Expand Up @@ -96,6 +96,17 @@ public Builder setFormat(String format) {
return this;
}

/**
* Sets the context directory to use
*
* @param contextDir the context directory to set
* @return This builder instance
*/
public Builder setContextDir(String contextDir) {
command.withOption(contextDir, null);
return this;
}

/**
* Sets the Containerfile that should be build
*
Expand Down Expand Up @@ -215,8 +226,6 @@ private Map<String, String> getUlimitsFromSystem() {
* @return The constructed command
*/
public Command build() {
// Add current directory
command.withOption(".", null);
return command;
}

Expand Down
Expand Up @@ -40,6 +40,17 @@ public abstract class AbstractImageBuildConfiguration {
*/
protected static final String DEFAULT_CONTAINERFILE = "Containerfile";

/**
* The default context directory
*/
protected static final String DEFAULT_CONTEXT_DIR = ".";

/**
* The context directory to use
*/
@Parameter
protected String contextDir;

/**
* Directory containing the Containerfile
*/
Expand Down Expand Up @@ -213,6 +224,10 @@ public void validate(MavenProject project) {
if (containerFileDir == null) {
containerFileDir = project.getBasedir();
}

if (contextDir == null) {
contextDir = DEFAULT_CONTEXT_DIR;
}

if(args == null) {
args = new HashMap<>();
Expand Down Expand Up @@ -242,6 +257,15 @@ public List<String> getAllTags() {
}
return allTags;
}

/**
* Returns the context directory path
*
* @return Returns the context directory path
*/
public Optional<String> getContextDir(){
return Optional.ofNullable(contextDir);
}

/**
* Returns the path to the target Containerfile
Expand Down Expand Up @@ -515,6 +539,15 @@ public void setCreateLatestTag(boolean createLatestTag) {
public void setFormat(ContainerFormat format) {
this.format = format;
}

/**
* Sets the context directory to use
*
* @param contextDir The directory to set
*/
public void setContextDir(String contextDir) {
this.contextDir = contextDir;
}

/**
* Sets the directory where the Containerfile is located (copied from BatchImageBuildCOnfiguration).
Expand Down
Expand Up @@ -85,6 +85,10 @@ private List<SingleImageConfiguration> convertToSingleImageConfigurations(Log lo

buildConfiguration.setTagWithMavenProjectVersion(getBuild().isTagWithMavenProjectVersion());

if(getBuild().getContextDir().isPresent()) {
buildConfiguration.setContextDir(getBuild().getContextDir().get());
}

imageConfiguration.setBuild(buildConfiguration);
imageConfigurations.add(imageConfiguration);
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -83,6 +84,12 @@ public List<String> build(SingleImageConfiguration image) throws MojoExecutionEx
}

builder.addBuildArgs(image.getBuild().getArgs());

Optional<String> contextDir = image.getBuild().getContextDir();
if (contextDir.isPresent()) {
builder = builder.setContextDir(contextDir.get());
}

builder.addUlimitsArgs(image.getBuild().getUlimits());

return builder.build().execute();
Expand Down

0 comments on commit 16225f6

Please sign in to comment.