use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.
the class JibBuildService method build.
public void build(String jibImageFormat, ImageConfiguration imageConfig, RegistryService.RegistryConfig registryConfig) throws MojoExecutionException {
try {
log.info("[[B]]JIB[[B]] image build started");
if (imageConfig.getBuildConfiguration().isDockerFileMode()) {
throw new MojoExecutionException("Dockerfile mode is not supported with JIB build strategy");
}
prependRegistry(imageConfig, mojoParameters.getProject().getProperties().getProperty(PUSH_REGISTRY));
BuildDirs buildDirs = new BuildDirs(imageConfig.getName(), mojoParameters);
final Credential pullRegistryCredential = getRegistryCredentials(registryConfig, false, imageConfig, log);
final JibContainerBuilder containerBuilder = containerFromImageConfiguration(jibImageFormat, imageConfig, pullRegistryCredential);
File dockerTarArchive = getAssemblyTarArchive(imageConfig, serviceHub, mojoParameters, log);
for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAssemblyConfigurations()) {
// TODO: Improve Assembly Manager so that the effective assemblyFileEntries computed can be properly shared
// the call to DockerAssemblyManager.getInstance().createDockerTarArchive should not be necessary,
// files should be added using the AssemblyFileEntry list. DockerAssemblyManager, should provide
// a common way to achieve this so that both the tar builder and any other builder could get a hold of
// archive customizers, file entries, etc.
AssemblyFiles assemblyFiles = serviceHub.getDockerAssemblyManager().getAssemblyFiles(imageConfig.getName(), assemblyConfiguration, mojoParameters, log);
final Map<File, AssemblyFiles.Entry> files = assemblyFiles.getUpdatedEntriesAndRefresh().stream().collect(Collectors.toMap(AssemblyFiles.Entry::getDestFile, Function.identity(), (oldV, newV) -> newV));
JibServiceUtil.copyToContainer(containerBuilder, buildDirs.getOutputDirectory(), buildDirs.getOutputDirectory().getAbsolutePath(), files);
}
JibServiceUtil.buildContainer(containerBuilder, TarImage.at(dockerTarArchive.toPath()).named(imageConfig.getName()), log);
log.info(" %s successfully built", dockerTarArchive.getAbsolutePath());
} catch (Exception ex) {
throw new MojoExecutionException("Error when building JIB image", ex);
}
}
use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.
the class DockerAssemblyManager method createDockerTarArchive.
/**
* Create an docker tar archive from the given configuration which can be send to the Docker host for
* creating the image.
*
* @param imageName Name of the image to create (used for creating build directories)
* @param params Mojos parameters (used for finding the directories)
* @param buildConfig configuration for how to build the image
* @param log Logger used to display warning if permissions are to be normalized
* @param finalCustomizer finalCustomizer to be applied to the tar archive
* @return file holding the path to the created assembly tar file
* @throws MojoExecutionException
*/
public File createDockerTarArchive(String imageName, final MojoParameters params, final BuildImageConfiguration buildConfig, Logger log, ArchiverCustomizer finalCustomizer) throws MojoExecutionException {
final BuildDirs buildDirs = createBuildDirs(imageName, params);
final List<AssemblyConfiguration> assemblyConfigurations = buildConfig.getAssemblyConfigurations();
final List<ArchiverCustomizer> archiveCustomizers = new ArrayList<>();
// Build up assembly. In dockerfile mode this must be added explicitly in the Dockerfile with an ADD
if (hasAssemblyConfiguration(assemblyConfigurations)) {
createAssemblyArchives(assemblyConfigurations, params, buildDirs);
}
try {
if (buildConfig.isDockerFileMode()) {
// Use specified docker directory which must include a Dockerfile.
final File dockerFile = buildConfig.getAbsoluteDockerFilePath(params);
if (!dockerFile.exists()) {
throw new MojoExecutionException("Configured Dockerfile \"" + buildConfig.getDockerFile() + "\" (resolved to \"" + dockerFile + "\") doesn't exist");
}
FixedStringSearchInterpolator interpolator = DockerFileUtil.createInterpolator(params, buildConfig.getFilter());
verifyGivenDockerfile(dockerFile, buildConfig, interpolator, log);
interpolateDockerfile(dockerFile, buildDirs, interpolator);
// User dedicated Dockerfile from extra directory
archiveCustomizers.add(new ArchiverCustomizer() {
@Override
public TarArchiver customize(TarArchiver archiver) throws IOException {
DefaultFileSet fileSet = DefaultFileSet.fileSet(buildConfig.getAbsoluteContextDirPath(params));
addDockerIncludesExcludesIfPresent(fileSet, params);
// Exclude non-interpolated dockerfile from source tree
// Interpolated Dockerfile is already added as it was created into the output directory when
// using dir dir mode
excludeDockerfile(fileSet, dockerFile);
// directly to docker.tar (as the output builddir is not picked up in archive mode)
if (isArchive(assemblyConfigurations)) {
String name = dockerFile.getName();
archiver.addFile(new File(buildDirs.getOutputDirectory(), name), name);
}
archiver.addFileSet(fileSet);
return archiver;
}
});
} else {
// Create custom docker file in output dir
DockerFileBuilder builder = createDockerFileBuilder(buildConfig, assemblyConfigurations);
builder.write(buildDirs.getOutputDirectory());
// Add own Dockerfile
final File dockerFile = new File(buildDirs.getOutputDirectory(), DOCKERFILE_NAME);
archiveCustomizers.add(new ArchiverCustomizer() {
@Override
public TarArchiver customize(TarArchiver archiver) throws IOException {
archiver.addFile(dockerFile, DOCKERFILE_NAME);
return archiver;
}
});
}
// If required make all files in the assembly executable
if (assemblyConfigurations.stream().anyMatch(assemblyConfig -> assemblyConfig.getPermissions() == AssemblyConfiguration.PermissionMode.exec || assemblyConfig.getPermissions() == AssemblyConfiguration.PermissionMode.auto && EnvUtil.isWindows())) {
archiveCustomizers.add(new AllFilesExecCustomizer(log));
}
if (finalCustomizer != null) {
archiveCustomizers.add(finalCustomizer);
}
return createBuildTarBall(buildDirs, archiveCustomizers, assemblyConfigurations, buildConfig.getCompression());
} catch (IOException e) {
throw new MojoExecutionException(String.format("Cannot create %s in %s", DOCKERFILE_NAME, buildDirs.getOutputDirectory()), e);
}
}
use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.
the class DockerAssemblyConfigurationSourceTest method testOutputDirHasImage.
@Test
public void testOutputDirHasImage() {
String image = "image";
MojoParameters params = buildParameters(".", "src/docker", "output/docker");
DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(params, new BuildDirs(image, params), assemblyConfig);
assertTrue(containsDir(image, source.getOutputDirectory()));
assertTrue(containsDir(image, source.getWorkingDirectory()));
assertTrue(containsDir(image, source.getTemporaryRootDirectory()));
}
use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.
the class DockerAssemblyManager method getAssemblyFiles.
/**
* Extract all files with a tracking archiver. These can be used to track changes in the filesystem and triggering
* a rebuild of the image if needed ('docker:watch')
*/
public AssemblyFiles getAssemblyFiles(String name, BuildImageConfiguration buildConfig, MojoParameters mojoParams, Logger log) throws InvalidAssemblerConfigurationException, ArchiveCreationException, AssemblyFormattingException, MojoExecutionException {
BuildDirs buildDirs = createBuildDirs(name, mojoParams);
AssemblyConfiguration assemblyConfig = buildConfig.getAssemblyConfiguration();
String assemblyName = assemblyConfig.getName();
DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(mojoParams, buildDirs, assemblyConfig);
Assembly assembly = getAssemblyConfig(assemblyConfig, source);
synchronized (trackArchiver) {
MappingTrackArchiver ta = (MappingTrackArchiver) trackArchiver;
ta.init(log, assemblyName);
assembly.setId("tracker");
assemblyArchiver.createArchive(assembly, assemblyName, "track", source, false);
return ta.getAssemblyFiles(mojoParams.getSession());
}
}
use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.
the class DockerAssemblyManager method createAssemblyArchive.
private void createAssemblyArchive(AssemblyConfiguration assemblyConfig, MojoParameters params, BuildDirs buildDirs) throws MojoExecutionException {
DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(params, buildDirs, assemblyConfig);
Assembly assembly = getAssemblyConfig(assemblyConfig, source);
AssemblyMode buildMode = assemblyConfig.getMode();
File originalArtifactFile = null;
try {
originalArtifactFile = ensureThatArtifactFileIsSet(params.getProject());
assembly.setId("docker");
assemblyArchiver.createArchive(assembly, assemblyConfig.getName(), buildMode.getExtension(), source, false, null);
} catch (ArchiveCreationException | AssemblyFormattingException e) {
String error = "Failed to create assembly for docker image " + " (with mode '" + buildMode + "'): " + e.getMessage() + ".";
if (params.getProject().getArtifact().getFile() == null) {
error += " If you include the build artifact please ensure that you have " + "built the artifact before with 'mvn package' (should be available in the target/ dir). " + "Please see the documentation (section \"Assembly\") for more information.";
}
throw new MojoExecutionException(error, e);
} catch (InvalidAssemblerConfigurationException e) {
throw new MojoExecutionException(assembly, "Assembly is incorrectly configured: " + assembly.getId(), "Assembly: " + assembly.getId() + " is not configured correctly: " + e.getMessage());
} finally {
setArtifactFile(params.getProject(), originalArtifactFile);
}
}
Aggregations