use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException 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.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.
the class JibOwnershipExtensionTest method testExtendContainerBuildPlan_noGlobGiven.
@Test
public void testExtendContainerBuildPlan_noGlobGiven() {
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
Configuration.Rule rule = mockRule("", "doesn't matter");
when(config.getRules()).thenReturn(Arrays.asList(rule));
try {
new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
fail();
} catch (JibPluginExtensionException ex) {
assertEquals(JibOwnershipExtension.class, ex.getExtensionClass());
assertEquals("glob pattern not given in ownership configuration", ex.getMessage());
}
}
use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.
the class JibQuarkusExtension method extendContainerBuildPlan.
@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, GradleData gradleData, ExtensionLogger logger) throws JibPluginExtensionException {
try {
logger.log(LogLevel.LIFECYCLE, "Running Quarkus Jib extension");
readJibConfigurations(gradleData.getProject());
Project project = gradleData.getProject();
Path buildDir = project.getBuildDir().toPath();
Jar jarTask = (Jar) project.getTasks().findByName("jar");
String jarName = jarTask.getArchiveFile().get().getAsFile().getName();
Path jar = buildDir.resolve(jarName.replaceAll("\\.jar$", "-runner.jar"));
if (!Files.isRegularFile(jar)) {
throw new JibPluginExtensionException(getClass(), jar + " doesn't exist; did you run the Qaurkus Gradle plugin ('quarkusBuild' task)?");
}
ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
planBuilder.setLayers(Collections.emptyList());
// dependency layers
addDependencyLayers(project, planBuilder, buildDir.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);
}
}
use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.
the class JibNativeImageExtensionTest method testNoExecutableNameDetected.
@Test
public void testNoExecutableNameDetected() {
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
try {
new JibNativeImageExtension().extendContainerBuildPlan(buildPlan, Collections.emptyMap(), Optional.empty(), mavenData, logger);
fail();
} catch (JibPluginExtensionException ex) {
assertEquals("cannot auto-detect native-image executable name; consider setting 'imageName' property", ex.getMessage());
}
}
use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.
the class JibOwnershipExtensionTest method testExtendContainerBuildPlan_noGlobGiven.
@Test
public void testExtendContainerBuildPlan_noGlobGiven() {
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
Configuration.Rule rule = mockRule("", "doesn't matter");
when(config.getRules()).thenReturn(Arrays.asList(rule));
try {
new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
fail();
} catch (JibPluginExtensionException ex) {
assertEquals(JibOwnershipExtension.class, ex.getExtensionClass());
assertEquals("glob pattern not given in ownership configuration", ex.getMessage());
}
}
Aggregations