use of com.google.cloud.tools.jib.maven.extension.MavenData in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtension method extendContainerBuildPlan.
@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Configuration> config, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
logger.log(LogLevel.LIFECYCLE, "Running Jib Layer Filter Extension");
if (!config.isPresent()) {
logger.log(LogLevel.WARN, "Nothing configured for Jib Layer Filter Extension");
return buildPlan;
}
preparePathMatchersAndLayerBuilders(buildPlan, config.get());
ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder();
newPlanBuilder.setLayers(Collections.emptyList());
@SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = (List<FileEntriesLayer>) buildPlan.getLayers();
// Start filtering original layers.
for (FileEntriesLayer layer : originalLayers) {
List<FileEntry> filesToKeep = new ArrayList<>();
for (FileEntry entry : layer.getEntries()) {
Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName());
// Either keep, move, or delete this FileEntry.
if (finalLayerName.isPresent()) {
if (finalLayerName.get().equals(layer.getName())) {
filesToKeep.add(entry);
} else {
FileEntriesLayer.Builder targetLayerBuilder = Verify.verifyNotNull(newToLayers.get(finalLayerName.get()));
targetLayerBuilder.addEntry(entry);
}
}
}
if (!filesToKeep.isEmpty()) {
newPlanBuilder.addLayer(layer.toBuilder().setEntries(filesToKeep).build());
}
}
// Add newly created non-empty to-layers (if any).
newToLayers.values().stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).forEach(newPlanBuilder::addLayer);
ContainerBuildPlan newPlan = newPlanBuilder.build();
return config.get().isCreateParentDependencyLayers() ? moveParentDepsToNewLayers(newPlan, mavenData, logger) : newPlan;
}
use of com.google.cloud.tools.jib.maven.extension.MavenData in project jib-extensions by GoogleContainerTools.
the class JibNativeImageExtension method extendContainerBuildPlan.
@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
logger.log(LogLevel.LIFECYCLE, "Running Jib Native Image extension");
MavenProject project = mavenData.getMavenProject();
Optional<String> executableName = getExecutableName(project, properties);
if (!executableName.isPresent()) {
throw new JibPluginExtensionException(getClass(), "cannot auto-detect native-image executable name; consider setting 'imageName' property");
}
String outputDirectory = project.getBuild().getDirectory();
Path localExecutable = Paths.get(outputDirectory, executableName.get());
if (!Files.isRegularFile(localExecutable)) {
throw new JibPluginExtensionException(getClass(), "Native-image executable does not exist or not a file: " + localExecutable + "\nDid you run the 'native-image:native-image' goal?");
}
// TODO: also check system and Maven properties (e.g., -Djib.container.appRoot).
String appRoot = getPluginConfigValue(project, JIB_APP_ROOT).orElse("/app");
AbsoluteUnixPath targetExecutable = AbsoluteUnixPath.get(appRoot).resolve(executableName.get());
ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
FileEntriesLayer nativeImageLayer = FileEntriesLayer.builder().setName("native image").addEntry(localExecutable, targetExecutable, FilePermissions.fromOctalString("755")).build();
planBuilder.setLayers(Collections.singletonList(nativeImageLayer));
// Preserve extra directories layers.
String extraFilesLayerName = JavaContainerBuilder.LayerType.EXTRA_FILES.getName();
buildPlan.getLayers().stream().filter(layer -> layer.getName().startsWith(extraFilesLayerName)).forEach(planBuilder::addLayer);
// TODO: also check system and Maven properties (e.g., -Djib.container.entrypoint).
if (!getPluginConfigValue(project, JIB_ENTRYPOINT).isPresent()) {
planBuilder.setEntrypoint(Collections.singletonList(targetExecutable.toString()));
}
return planBuilder.build();
}
use of com.google.cloud.tools.jib.maven.extension.MavenData in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtension method moveParentDepsToNewLayers.
private ContainerBuildPlan moveParentDepsToNewLayers(ContainerBuildPlan buildPlan, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
logger.log(LogLevel.INFO, "Moving parent dependencies to new layers.");
// The key is the source file path for the parent dependency. We only consider artifacts that
// have been resolved.
Map<Path, Artifact> parentDependencies = getParentDependencies(mavenData).stream().map(Dependency::getArtifact).filter(artifact -> artifact.getFile() != null).collect(Collectors.toMap(artifact -> artifact.getFile().toPath(), artifact -> artifact));
// Parent dependencies that have not been found in any layer (due to different version or
// filtering). Only needed for logging.
Map<Path, Artifact> parentDependenciesNotFound = new HashMap<>(parentDependencies);
List<FileEntriesLayer.Builder> newLayerBuilders = new ArrayList<>();
@SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = ((List<FileEntriesLayer>) buildPlan.getLayers());
for (FileEntriesLayer originalLayer : originalLayers) {
String parentLayerName = originalLayer.getName() + "-parent";
FileEntriesLayer.Builder parentLayerBuilder = FileEntriesLayer.builder().setName(parentLayerName);
FileEntriesLayer.Builder layerBuilder = originalLayer.toBuilder().setEntries(Collections.emptyList());
newLayerBuilders.add(parentLayerBuilder);
newLayerBuilders.add(layerBuilder);
for (FileEntry entry : originalLayer.getEntries()) {
Path sourceFilePath = entry.getSourceFile();
if (parentDependencies.containsKey(sourceFilePath)) {
// move to parent layer
logger.log(LogLevel.DEBUG, "Moving " + sourceFilePath + " to " + parentLayerName + ".");
parentLayerBuilder.addEntry(entry);
// mark parent dep as found
parentDependenciesNotFound.remove(sourceFilePath);
} else {
// keep in original layer
logger.log(LogLevel.DEBUG, "Keeping " + sourceFilePath + " in " + originalLayer.getName() + ".");
layerBuilder.addEntry(entry);
}
}
}
logMissingParentDependencies(logger, parentDependenciesNotFound, originalLayers);
List<FileEntriesLayer> newLayers = newLayerBuilders.stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).collect(Collectors.toList());
return buildPlan.toBuilder().setLayers(newLayers).build();
}
use of com.google.cloud.tools.jib.maven.extension.MavenData in project jib-extensions by GoogleContainerTools.
the class JibQuarkusExtension method extendContainerBuildPlan.
@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
try {
logger.log(LogLevel.LIFECYCLE, "Running Quarkus Jib extension");
readJibConfigurations(mavenData.getMavenProject());
Build build = mavenData.getMavenProject().getBuild();
Path outputDirectory = Paths.get(build.getDirectory());
Path jar = outputDirectory.resolve(build.getFinalName() + "-runner.jar");
if (!Files.isRegularFile(jar)) {
throw new JibPluginExtensionException(getClass(), jar + " doesn't exist; did you run the Qaurkus Maven plugin " + "('compile' and 'quarkus:build' Maven goals)?");
}
ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
planBuilder.setLayers(Collections.emptyList());
// dependency layers
addDependencyLayers(mavenData.getMavenSession(), planBuilder, outputDirectory.resolve("lib"));
// Quarkus runner JAR layer
AbsoluteUnixPath appRootJar = appRoot.resolve("app.jar");
FileEntriesLayer jarLayer = FileEntriesLayer.builder().setName("quarkus jar").addEntry(jar, appRootJar).build();
planBuilder.addLayer(jarLayer);
// Preserve extra directories layers.
String extraFilesLayerName = JavaContainerBuilder.LayerType.EXTRA_FILES.getName();
buildPlan.getLayers().stream().filter(layer -> layer.getName().startsWith(extraFilesLayerName)).forEach(planBuilder::addLayer);
// set entrypoint
List<String> entrypoint = new ArrayList<>();
entrypoint.add("java");
entrypoint.addAll(jvmFlags);
entrypoint.add("-jar");
entrypoint.add(appRootJar.toString());
planBuilder.setEntrypoint(entrypoint);
return planBuilder.build();
} catch (IOException ex) {
throw new JibPluginExtensionException(getClass(), Verify.verifyNotNull(ex.getMessage()), ex);
}
}
Aggregations