use of com.google.cloud.tools.jib.plugins.common.ContainerizingMode in project jib by GoogleContainerTools.
the class GradleProjectProperties method createJibContainerBuilder.
@Override
public JibContainerBuilder createJibContainerBuilder(JavaContainerBuilder javaContainerBuilder, ContainerizingMode containerizingMode) {
try {
FileCollection projectDependencies = project.files(project.getConfigurations().getByName(configurationName).getResolvedConfiguration().getResolvedArtifacts().stream().filter(artifact -> artifact.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier).map(ResolvedArtifact::getFile).collect(Collectors.toList()));
if (isWarProject()) {
String warFilePath = getWarFilePath();
log(LogEvent.info("WAR project identified, creating WAR image from: " + warFilePath));
Path explodedWarPath = tempDirectoryProvider.newDirectory();
ZipUtil.unzip(Paths.get(warFilePath), explodedWarPath);
return JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, explodedWarPath, projectDependencies.getFiles().stream().map(File::getName).collect(Collectors.toSet()));
}
SourceSet mainSourceSet = getMainSourceSet();
FileCollection classesOutputDirectories = mainSourceSet.getOutput().getClassesDirs().filter(File::exists);
Path resourcesOutputDirectory = mainSourceSet.getOutput().getResourcesDir().toPath();
FileCollection allFiles = project.getConfigurations().getByName(configurationName).filter(File::exists);
FileCollection nonProjectDependencies = allFiles.minus(classesOutputDirectories).minus(projectDependencies).filter(file -> !file.toPath().equals(resourcesOutputDirectory));
FileCollection snapshotDependencies = nonProjectDependencies.filter(file -> file.getName().contains("SNAPSHOT"));
FileCollection dependencies = nonProjectDependencies.minus(snapshotDependencies);
// Adds dependency files
javaContainerBuilder.addDependencies(dependencies.getFiles().stream().map(File::toPath).collect(Collectors.toList())).addSnapshotDependencies(snapshotDependencies.getFiles().stream().map(File::toPath).collect(Collectors.toList())).addProjectDependencies(projectDependencies.getFiles().stream().map(File::toPath).collect(Collectors.toList()));
switch(containerizingMode) {
case EXPLODED:
// Adds resource files
if (Files.exists(resourcesOutputDirectory)) {
javaContainerBuilder.addResources(resourcesOutputDirectory);
}
// Adds class files
for (File classesOutputDirectory : classesOutputDirectories) {
javaContainerBuilder.addClasses(classesOutputDirectory.toPath());
}
if (classesOutputDirectories.isEmpty()) {
log(LogEvent.warn("No classes files were found - did you compile your project?"));
}
break;
case PACKAGED:
// Add a JAR
Jar jarTask = (Jar) project.getTasks().findByName("jar");
Path jarPath = jarTask.getArchiveFile().get().getAsFile().toPath();
log(LogEvent.debug("Using JAR: " + jarPath));
javaContainerBuilder.addToClasspath(jarPath);
break;
default:
throw new IllegalStateException("unknown containerizing mode: " + containerizingMode);
}
return javaContainerBuilder.toContainerBuilder();
} catch (IOException ex) {
throw new GradleException("Obtaining project build output files failed", ex);
}
}
use of com.google.cloud.tools.jib.plugins.common.ContainerizingMode in project jib by google.
the class MavenProjectProperties method createJibContainerBuilder.
@Override
public JibContainerBuilder createJibContainerBuilder(JavaContainerBuilder javaContainerBuilder, ContainerizingMode containerizingMode) throws IOException {
try {
if (isWarProject()) {
Path war = getWarArtifact();
Path explodedWarPath = tempDirectoryProvider.newDirectory();
ZipUtil.unzip(war, explodedWarPath);
return JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, explodedWarPath, getProjectDependencies().stream().map(Artifact::getFile).map(File::getName).collect(Collectors.toSet()));
}
switch(containerizingMode) {
case EXPLODED:
// Add resources, and classes
Path classesOutputDirectory = Paths.get(project.getBuild().getOutputDirectory());
// Don't use Path.endsWith(), since Path works on path elements.
Predicate<Path> isClassFile = path -> path.getFileName().toString().endsWith(".class");
javaContainerBuilder.addResources(classesOutputDirectory, isClassFile.negate()).addClasses(classesOutputDirectory, isClassFile);
break;
case PACKAGED:
// Add a JAR
javaContainerBuilder.addToClasspath(getJarArtifact());
break;
default:
throw new IllegalStateException("unknown containerizing mode: " + containerizingMode);
}
// Classify and add dependencies
Map<LayerType, List<Path>> classifiedDependencies = classifyDependencies(project.getArtifacts(), getProjectDependencies());
javaContainerBuilder.addDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.DEPENDENCIES)));
javaContainerBuilder.addSnapshotDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES)));
javaContainerBuilder.addProjectDependencies(Preconditions.checkNotNull(classifiedDependencies.get(LayerType.PROJECT_DEPENDENCIES)));
return javaContainerBuilder.toContainerBuilder();
} catch (IOException ex) {
throw new IOException("Obtaining project build output files failed; make sure you have " + (isPackageErrorMessage(containerizingMode) ? "packaged" : "compiled") + " your project " + "before trying to build the image. (Did you accidentally run \"mvn clean " + "jib:build\" instead of \"mvn clean " + (isPackageErrorMessage(containerizingMode) ? "package" : "compile") + " jib:build\"?)", ex);
}
}
use of com.google.cloud.tools.jib.plugins.common.ContainerizingMode in project jib by google.
the class GradleProjectProperties method createJibContainerBuilder.
@Override
public JibContainerBuilder createJibContainerBuilder(JavaContainerBuilder javaContainerBuilder, ContainerizingMode containerizingMode) {
try {
FileCollection projectDependencies = project.files(project.getConfigurations().getByName(configurationName).getResolvedConfiguration().getResolvedArtifacts().stream().filter(artifact -> artifact.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier).map(ResolvedArtifact::getFile).collect(Collectors.toList()));
if (isWarProject()) {
String warFilePath = getWarFilePath();
log(LogEvent.info("WAR project identified, creating WAR image from: " + warFilePath));
Path explodedWarPath = tempDirectoryProvider.newDirectory();
ZipUtil.unzip(Paths.get(warFilePath), explodedWarPath);
return JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, explodedWarPath, projectDependencies.getFiles().stream().map(File::getName).collect(Collectors.toSet()));
}
SourceSet mainSourceSet = getMainSourceSet();
FileCollection classesOutputDirectories = mainSourceSet.getOutput().getClassesDirs().filter(File::exists);
Path resourcesOutputDirectory = mainSourceSet.getOutput().getResourcesDir().toPath();
FileCollection allFiles = project.getConfigurations().getByName(configurationName).filter(File::exists);
FileCollection nonProjectDependencies = allFiles.minus(classesOutputDirectories).minus(projectDependencies).filter(file -> !file.toPath().equals(resourcesOutputDirectory));
FileCollection snapshotDependencies = nonProjectDependencies.filter(file -> file.getName().contains("SNAPSHOT"));
FileCollection dependencies = nonProjectDependencies.minus(snapshotDependencies);
// Adds dependency files
javaContainerBuilder.addDependencies(dependencies.getFiles().stream().map(File::toPath).collect(Collectors.toList())).addSnapshotDependencies(snapshotDependencies.getFiles().stream().map(File::toPath).collect(Collectors.toList())).addProjectDependencies(projectDependencies.getFiles().stream().map(File::toPath).collect(Collectors.toList()));
switch(containerizingMode) {
case EXPLODED:
// Adds resource files
if (Files.exists(resourcesOutputDirectory)) {
javaContainerBuilder.addResources(resourcesOutputDirectory);
}
// Adds class files
for (File classesOutputDirectory : classesOutputDirectories) {
javaContainerBuilder.addClasses(classesOutputDirectory.toPath());
}
if (classesOutputDirectories.isEmpty()) {
log(LogEvent.warn("No classes files were found - did you compile your project?"));
}
break;
case PACKAGED:
// Add a JAR
Jar jarTask = (Jar) project.getTasks().findByName("jar");
Path jarPath = jarTask.getArchiveFile().get().getAsFile().toPath();
log(LogEvent.debug("Using JAR: " + jarPath));
javaContainerBuilder.addToClasspath(jarPath);
break;
default:
throw new IllegalStateException("unknown containerizing mode: " + containerizingMode);
}
return javaContainerBuilder.toContainerBuilder();
} catch (IOException ex) {
throw new GradleException("Obtaining project build output files failed", ex);
}
}
use of com.google.cloud.tools.jib.plugins.common.ContainerizingMode in project jib by google.
the class MavenProjectPropertiesTest method testCreateContainerBuilder_exceptionMessageHasPackageSuggestionIfProjectIsWar.
@Test
public void testCreateContainerBuilder_exceptionMessageHasPackageSuggestionIfProjectIsWar() throws IOException {
String expectedMessage = "Obtaining project build output files failed; make sure you have " + "packaged your project before trying to build the image. (Did you accidentally run \"mvn clean " + "jib:build\" instead of \"mvn clean package jib:build\"?)";
when(mockMavenProject.getPackaging()).thenReturn("war");
when(mockTempDirectoryProvider.newDirectory()).thenThrow(IOException.class);
for (ContainerizingMode containerizingMode : ContainerizingMode.values()) {
IOException thrownException = assertThrows(IOException.class, () -> mavenProjectProperties.createJibContainerBuilder(mockJavaContainerBuilder, containerizingMode));
assertThat(thrownException).hasMessageThat().isEqualTo(expectedMessage);
}
}
use of com.google.cloud.tools.jib.plugins.common.ContainerizingMode in project jib by GoogleContainerTools.
the class MavenProjectPropertiesTest method testCreateContainerBuilder_exceptionMessageHasPackageSuggestionIfProjectIsWar.
@Test
public void testCreateContainerBuilder_exceptionMessageHasPackageSuggestionIfProjectIsWar() throws IOException {
String expectedMessage = "Obtaining project build output files failed; make sure you have " + "packaged your project before trying to build the image. (Did you accidentally run \"mvn clean " + "jib:build\" instead of \"mvn clean package jib:build\"?)";
when(mockMavenProject.getPackaging()).thenReturn("war");
when(mockTempDirectoryProvider.newDirectory()).thenThrow(IOException.class);
for (ContainerizingMode containerizingMode : ContainerizingMode.values()) {
IOException thrownException = assertThrows(IOException.class, () -> mavenProjectProperties.createJibContainerBuilder(mockJavaContainerBuilder, containerizingMode));
assertThat(thrownException).hasMessageThat().isEqualTo(expectedMessage);
}
}
Aggregations