use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib by GoogleContainerTools.
the class MavenProjectPropertiesExtensionTest method testRunPluginExtensions_exceptionFromExtension.
@Test
public void testRunPluginExtensions_exceptionFromExtension() {
FileNotFoundException fakeException = new FileNotFoundException();
FooExtension extension = new FooExtension((buildPlan, properties, extraConfig, mavenData, logger) -> {
throw new JibPluginExtensionException(FooExtension.class, "exception from extension", fakeException);
});
loadedExtensions = Arrays.asList(extension);
try {
mavenProjectProperties.runPluginExtensions(Arrays.asList(new FooExtensionConfig()), containerBuilder);
Assert.fail();
} catch (JibPluginExtensionException ex) {
Assert.assertEquals("exception from extension", ex.getMessage());
Assert.assertSame(fakeException, ex.getCause());
}
}
use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib by GoogleContainerTools.
the class BuildImageTask method buildImage.
/**
* Task Action, builds an image to remote registry.
*
* @throws IOException if an error occurs creating the jib runner
* @throws BuildStepsExecutionException if an error occurs while executing build steps
* @throws CacheDirectoryCreationException if a new cache directory could not be created
* @throws MainClassInferenceException if a main class could not be found
* @throws InvalidGlobalConfigException if the global config file is invalid
*/
@TaskAction
public void buildImage() throws IOException, BuildStepsExecutionException, CacheDirectoryCreationException, MainClassInferenceException, InvalidGlobalConfigException {
// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
TaskCommon.disableHttpLogging();
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
GradleProjectProperties projectProperties = GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider, jibExtension.getConfigurationName().get());
GlobalConfig globalConfig = GlobalConfig.readConfig();
Future<Optional<String>> updateCheckFuture = TaskCommon.newUpdateChecker(projectProperties, globalConfig, getLogger());
try {
if (Strings.isNullOrEmpty(jibExtension.getTo().getImage())) {
throw new GradleException(HelpfulSuggestions.forToNotConfigured("Missing target image parameter", "'jib.to.image'", "build.gradle", "gradle jib --image <your image name>"));
}
PluginConfigurationProcessor.createJibBuildRunnerForRegistryImage(new GradleRawConfiguration(jibExtension), ignored -> Optional.empty(), projectProperties, globalConfig, new GradleHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
} catch (InvalidAppRootException ex) {
throw new GradleException("container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidContainerizingModeException ex) {
throw new GradleException("invalid value for containerizingMode: " + ex.getInvalidContainerizingMode(), ex);
} catch (InvalidWorkingDirectoryException ex) {
throw new GradleException("container.workingDirectory is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidPlatformException ex) {
throw new GradleException("from.platforms contains a platform configuration that is missing required values or has invalid values: " + ex.getMessage() + ": " + ex.getInvalidPlatform(), ex);
} catch (InvalidContainerVolumeException ex) {
throw new GradleException("container.volumes is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidFilesModificationTimeException ex) {
throw new GradleException("container.filesModificationTime should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or special keyword \"EPOCH_PLUS_SECOND\": " + ex.getInvalidFilesModificationTime(), ex);
} catch (InvalidCreationTimeException ex) {
throw new GradleException("container.creationTime should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH\", " + "\"USE_CURRENT_TIMESTAMP\"): " + ex.getInvalidCreationTime(), ex);
} catch (JibPluginExtensionException ex) {
String extensionName = ex.getExtensionClass().getName();
throw new GradleException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new GradleException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForGradle(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
} catch (InvalidImageReferenceException ex) {
throw new GradleException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
} catch (ExtraDirectoryNotFoundException ex) {
throw new GradleException("extraDirectories.paths contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
} finally {
tempDirectoryProvider.close();
TaskCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
projectProperties.waitForLoggingThread();
}
}
use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib by GoogleContainerTools.
the class BuildTarTask method buildTar.
/**
* Task Action, builds an image to tar file.
*
* @throws IOException if an error occurs creating the jib runner
* @throws BuildStepsExecutionException if an error occurs while executing build steps
* @throws CacheDirectoryCreationException if a new cache directory could not be created
* @throws MainClassInferenceException if a main class could not be found
* @throws InvalidGlobalConfigException if the global config file is invalid
*/
@TaskAction
public void buildTar() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException, MainClassInferenceException, InvalidGlobalConfigException {
// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
TaskCommon.disableHttpLogging();
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
GradleProjectProperties projectProperties = GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider, jibExtension.getConfigurationName().get());
GlobalConfig globalConfig = GlobalConfig.readConfig();
Future<Optional<String>> updateCheckFuture = TaskCommon.newUpdateChecker(projectProperties, globalConfig, getLogger());
try {
PluginConfigurationProcessor.createJibBuildRunnerForTarImage(new GradleRawConfiguration(jibExtension), ignored -> Optional.empty(), projectProperties, globalConfig, new GradleHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
} catch (InvalidAppRootException ex) {
throw new GradleException("container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidContainerizingModeException ex) {
throw new GradleException("invalid value for containerizingMode: " + ex.getInvalidContainerizingMode(), ex);
} catch (InvalidWorkingDirectoryException ex) {
throw new GradleException("container.workingDirectory is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidPlatformException ex) {
throw new GradleException("from.platforms contains a platform configuration that is missing required values or has invalid values: " + ex.getMessage() + ": " + ex.getInvalidPlatform(), ex);
} catch (InvalidContainerVolumeException ex) {
throw new GradleException("container.volumes is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidFilesModificationTimeException ex) {
throw new GradleException("container.filesModificationTime should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or special keyword \"EPOCH_PLUS_SECOND\": " + ex.getInvalidFilesModificationTime(), ex);
} catch (InvalidCreationTimeException ex) {
throw new GradleException("container.creationTime should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH\", " + "\"USE_CURRENT_TIMESTAMP\"): " + ex.getInvalidCreationTime(), ex);
} catch (JibPluginExtensionException ex) {
String extensionName = ex.getExtensionClass().getName();
throw new GradleException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new GradleException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForGradle(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
} catch (InvalidImageReferenceException ex) {
throw new GradleException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
} catch (ExtraDirectoryNotFoundException ex) {
throw new GradleException("extraDirectories.paths contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
} finally {
tempDirectoryProvider.close();
TaskCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
projectProperties.waitForLoggingThread();
}
}
use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib by GoogleContainerTools.
the class GradleProjectProperties method runPluginExtensions.
@Override
public JibContainerBuilder runPluginExtensions(List<? extends ExtensionConfiguration> extensionConfigs, JibContainerBuilder jibContainerBuilder) throws JibPluginExtensionException {
if (extensionConfigs.isEmpty()) {
log(LogEvent.debug("No Jib plugin extensions configured to load"));
return jibContainerBuilder;
}
List<JibGradlePluginExtension<?>> loadedExtensions = extensionLoader.get();
JibGradlePluginExtension<?> extension = null;
ContainerBuildPlan buildPlan = jibContainerBuilder.toContainerBuildPlan();
try {
for (ExtensionConfiguration config : extensionConfigs) {
extension = findConfiguredExtension(loadedExtensions, config);
log(LogEvent.lifecycle("Running extension: " + config.getExtensionClass()));
buildPlan = runPluginExtension(extension.getExtraConfigType(), extension, config, buildPlan);
// to validate image reference
ImageReference.parse(buildPlan.getBaseImage());
}
return jibContainerBuilder.applyContainerBuildPlan(buildPlan);
} catch (InvalidImageReferenceException ex) {
throw new JibPluginExtensionException(Verify.verifyNotNull(extension).getClass(), "invalid base image reference: " + buildPlan.getBaseImage(), ex);
}
}
use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException 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();
}
Aggregations