Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Maven support for containerizing packaged JAR #1746

Merged
merged 34 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
af2e106
wip
chanseokoh May 10, 2019
bc98dac
wip
chanseokoh May 10, 2019
759a8b7
Merge branch 'master' into jar-experiment
chanseokoh May 15, 2019
526a1ba
Add containerizingMode config
chanseokoh May 15, 2019
17d5dd0
Refactor code
chanseokoh May 15, 2019
e63bc7e
fix
chanseokoh May 17, 2019
4ec572d
Update example
chanseokoh May 17, 2019
56562cc
fix example
chanseokoh May 17, 2019
a7728fa
Merge branch 'master' into jar-experiment
chanseokoh May 29, 2019
4d8e81c
Remove debug output
chanseokoh May 29, 2019
bee3268
Fix existing tests
chanseokoh May 29, 2019
c8af0ff
wip
chanseokoh May 29, 2019
13db40c
Add tests
chanseokoh May 30, 2019
17d5ebc
Merge remote-tracking branch 'origin/master' into jar-experiment
chanseokoh May 30, 2019
c0d2c0b
Merge remote-tracking branch 'origin/master' into jar-experiment
chanseokoh May 30, 2019
9a33978
Set appRoot / update error message
chanseokoh May 30, 2019
8135c97
Add tests
chanseokoh May 30, 2019
24ee519
Refactor code
chanseokoh May 30, 2019
8fd5d18
Merge branch 'master' into jar-experiment
chanseokoh May 31, 2019
f0af7af
Merge remote-tracking branch 'origin/master' into jar-experiment
chanseokoh May 31, 2019
4c715c2
Add tests
chanseokoh May 31, 2019
c73eac3
Add integration test
chanseokoh May 31, 2019
185ed13
Define constant for containerizingMode
chanseokoh May 31, 2019
6ff148f
Specialize error message on mode / refactor code
chanseokoh May 31, 2019
2c34f80
Remove unused method introduced briefly
chanseokoh Jun 3, 2019
9365ae7
Get generated JAR in various ways
chanseokoh Jun 4, 2019
c6e5f7c
mainArtifact should be JAR
chanseokoh Jun 4, 2019
dc83f69
Target maven-jar-plugin's JAR path
chanseokoh Jun 4, 2019
279cb1e
Define constant for default mode (exploded)
chanseokoh Jun 12, 2019
32edbba
Error on "packaged" mode with WAR
chanseokoh Jun 12, 2019
19c41e7
Merge remote-tracking branch 'origin/master' into jar-experiment
chanseokoh Jun 12, 2019
ea170e1
Fix test
chanseokoh Jun 12, 2019
bbfff1c
Do not use context classloader
chanseokoh Jun 12, 2019
be7610b
Merge remote-tracking branch 'origin/master' into jar-experiment
chanseokoh Jun 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions examples/helloworld/src/main/java/example/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@

package example;

import com.google.common.io.Resources;
import com.google.common.io.CharStreams;

import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class HelloWorld {

public static void main(String[] args) throws URISyntaxException, IOException {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code did not work inside a JAR, as it was complaining that there is no "file system" inside it. The new code works both inside and outside a JAR.

Path worldFile = Paths.get(Resources.getResource("world").toURI());
String world = new String(Files.readAllBytes(worldFile), StandardCharsets.UTF_8);

System.out.println("Hello " + world);
try (Reader reader = new InputStreamReader(
HelloWorld.class.getResourceAsStream("/world"), StandardCharsets.UTF_8)) {
String world = CharStreams.toString(reader);
System.out.println("Hello " + world);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
Expand Down Expand Up @@ -149,6 +150,10 @@ public void buildDocker()
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: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
Expand Down Expand Up @@ -129,6 +130,10 @@ public void buildImage()
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: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
Expand Down Expand Up @@ -147,6 +148,10 @@ public void buildTar()
throw new GradleException(
"container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved

} 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: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.tools.jib.event.events.TimerEvent;
import com.google.cloud.tools.jib.event.progress.ProgressEventHandler;
import com.google.cloud.tools.jib.filesystem.DirectoryWalker;
import com.google.cloud.tools.jib.plugins.common.ContainerizingMode;
import com.google.cloud.tools.jib.plugins.common.JavaContainerBuilderHelper;
import com.google.cloud.tools.jib.plugins.common.ProjectProperties;
import com.google.cloud.tools.jib.plugins.common.PropertyNames;
Expand Down Expand Up @@ -127,12 +128,15 @@ private static boolean isProgressFooterEnabled(Project project) {

@Override
public JibContainerBuilder createContainerBuilder(
RegistryImage baseImage, AbsoluteUnixPath appRoot) {
RegistryImage baseImage, AbsoluteUnixPath appRoot, ContainerizingMode containerizingMode) {
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
JavaContainerBuilder javaContainerBuilder =
JavaContainerBuilder.from(baseImage).setAppRoot(appRoot);

try {
if (isWarProject()) {
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
logger.info("WAR project identified, creating WAR image: " + project.getDisplayName());
Path explodedWarPath = GradleProjectProperties.getExplodedWarDirectory(project);
return JavaContainerBuilderHelper.fromExplodedWar(baseImage, explodedWarPath, appRoot);
return JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, explodedWarPath);
}

JavaPluginConvention javaPluginConvention =
Expand All @@ -143,7 +147,7 @@ public JibContainerBuilder createContainerBuilder(
FileCollection classesOutputDirectories =
mainSourceSet.getOutput().getClassesDirs().filter(File::exists);
Path resourcesOutputDirectory = mainSourceSet.getOutput().getResourcesDir().toPath();
FileCollection allFiles = mainSourceSet.getRuntimeClasspath();
FileCollection allFiles = mainSourceSet.getRuntimeClasspath().filter(File::exists);

FileCollection allDependencyFiles =
allFiles
Expand All @@ -154,9 +158,6 @@ public JibContainerBuilder createContainerBuilder(
allDependencyFiles.filter(file -> file.getName().contains("SNAPSHOT"));
FileCollection dependencyFiles = allDependencyFiles.minus(snapshotDependencyFiles);

JavaContainerBuilder javaContainerBuilder =
JavaContainerBuilder.from(baseImage).setAppRoot(appRoot);

// Adds resource files
if (Files.exists(resourcesOutputDirectory)) {
javaContainerBuilder.addResources(resourcesOutputDirectory);
Expand All @@ -171,23 +172,16 @@ public JibContainerBuilder createContainerBuilder(
}

// Adds dependency files
javaContainerBuilder.addDependencies(
dependencyFiles
.getFiles()
.stream()
.filter(File::exists)
.map(File::toPath)
.collect(Collectors.toList()));

javaContainerBuilder.addSnapshotDependencies(
snapshotDependencyFiles
.getFiles()
.stream()
.filter(File::exists)
.map(File::toPath)
.collect(Collectors.toList()));

return javaContainerBuilder.toContainerBuilder();
return javaContainerBuilder
.addDependencies(
dependencyFiles.getFiles().stream().map(File::toPath).collect(Collectors.toList()))
.addSnapshotDependencies(
snapshotDependencyFiles
.getFiles()
.stream()
.map(File::toPath)
.collect(Collectors.toList()))
.toContainerBuilder();

} catch (IOException ex) {
throw new GradleException("Obtaining project build output files failed", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,9 @@ public List<Path> getExtraDirectories() {
public Map<AbsoluteUnixPath, FilePermissions> getExtraDirectoryPermissions() {
return TaskCommon.convertPermissionsMap(jibExtension.getExtraDirectories().getPermissions());
}

@Override
public String getContainerizingMode() {
return jibExtension.getContainerizingMode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ public class JibExtension {

// Defines default configuration values.
private static final boolean DEFAULT_ALLOW_INSECURE_REGISTIRIES = false;
private static final String DEFAULT_CONTAINERIZING_MODE = "exploded";

private final BaseImageParameters from;
private final TargetImageParameters to;
private final ContainerParameters container;
private final ExtraDirectoriesParameters extraDirectories;
private final Property<Boolean> allowInsecureRegistries;
private final Property<String> containerizingMode;

@Deprecated boolean extraDirectoryConfigured;
@Deprecated boolean extraDirectoriesConfigured;
Expand All @@ -83,9 +85,11 @@ public JibExtension(Project project) {
extraDirectories = objectFactory.newInstance(ExtraDirectoriesParameters.class, project, this);

allowInsecureRegistries = objectFactory.property(Boolean.class);
containerizingMode = objectFactory.property(String.class);

// Sets defaults.
allowInsecureRegistries.set(DEFAULT_ALLOW_INSECURE_REGISTIRIES);
containerizingMode.set(DEFAULT_CONTAINERIZING_MODE);
}

public void from(Action<? super BaseImageParameters> action) {
Expand Down Expand Up @@ -115,7 +119,7 @@ public void extraDirectories(Action<? super ExtraDirectoriesParameters> action)
// for the deprecated "jib.extraDirectory" config parameter
public void setExtraDirectory(File extraDirectory) {
extraDirectoryConfigured = true;
this.extraDirectories.setPath(extraDirectory);
extraDirectories.setPath(extraDirectory);
}

public void setAllowInsecureRegistries(boolean allowInsecureRegistries) {
Expand Down Expand Up @@ -161,4 +165,11 @@ boolean getAllowInsecureRegistries() {
}
return allowInsecureRegistries.get();
}

@Input
@Optional
public String getContainerizingMode() {
String property = System.getProperty(PropertyNames.CONTAINERIZING_MODE);
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
return property != null ? property : containerizingMode.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.cloud.tools.jib.api.LayerEntry;
import com.google.cloud.tools.jib.api.RegistryImage;
import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.plugins.common.ContainerizingMode;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -84,6 +85,8 @@
@RunWith(MockitoJUnitRunner.class)
public class GradleProjectPropertiesTest {

private static final ContainerizingMode DEFAULT_CONTAINERIZING_MODE = ContainerizingMode.EXPLODED;

/** Implementation of {@link FileCollection} that just holds a set of {@link File}s. */
private static class TestFileCollection extends AbstractFileCollection {

Expand Down Expand Up @@ -361,7 +364,9 @@ public void testCreateContainerBuilder_noClassesFiles() throws InvalidImageRefer
Mockito.when(mockMainSourceSetOutput.getClassesDirs())
.thenReturn(new TestFileCollection(ImmutableSet.of(nonexistentFile)));
gradleProjectProperties.createContainerBuilder(
RegistryImage.named("base"), AbsoluteUnixPath.get("/anything"));
RegistryImage.named("base"),
AbsoluteUnixPath.get("/anything"),
DEFAULT_CONTAINERIZING_MODE);
Mockito.verify(mockLogger).warn("No classes files were found - did you compile your project?");
}

Expand Down Expand Up @@ -552,7 +557,10 @@ private BuildConfiguration setupBuildConfiguration(String appRoot)
throws InvalidImageReferenceException, IOException, CacheDirectoryCreationException {
JibContainerBuilder jibContainerBuilder =
new GradleProjectProperties(mockProject, mockLogger)
.createContainerBuilder(RegistryImage.named("base"), AbsoluteUnixPath.get(appRoot));
.createContainerBuilder(
RegistryImage.named("base"),
AbsoluteUnixPath.get(appRoot),
DEFAULT_CONTAINERIZING_MODE);
return JibContainerBuilderTestHelper.toBuildConfiguration(
jibContainerBuilder,
Containerizer.to(RegistryImage.named("to"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private static void clearProperties() {
System.clearProperty("jib.container.ports");
System.clearProperty("jib.container.useCurrentTimestamp");
System.clearProperty("jib.container.user");
System.clearProperty("jib.containerizingMode");
System.clearProperty("jib.extraDirectory.path");
System.clearProperty("jib.extraDirectory.permissions");
System.clearProperty("jib.extraDirectories.paths");
Expand Down Expand Up @@ -152,6 +153,11 @@ public void testContainer() {
Assert.assertEquals("some invalid appRoot value", container.getAppRoot());
}

@Test
public void testContainerizingMode() {
Assert.assertEquals("exploded", testJibExtension.getContainerizingMode());
}

@Test
public void testExtraDirectories_default() {
Assert.assertEquals(
Expand Down Expand Up @@ -288,6 +294,8 @@ public void testProperties() {
Assert.assertTrue(testJibExtension.getContainer().getUseCurrentTimestamp());
System.setProperty("jib.container.user", "myUser");
Assert.assertEquals("myUser", testJibExtension.getContainer().getUser());
System.setProperty("jib.containerizingMode", "packaged");
Assert.assertEquals("packaged", testJibExtension.getContainerizingMode());
System.setProperty("jib.extraDirectories.paths", "/foo,/bar/baz");
Assert.assertEquals(
Arrays.asList(Paths.get("/foo"), Paths.get("/bar/baz")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
Expand Down Expand Up @@ -146,6 +147,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue(),
ex);

} catch (InvalidContainerizingModeException ex) {
throw new MojoExecutionException(
"invalid value for <containerizingMode>: " + ex.getInvalidContainerizingMode(), ex);

} catch (InvalidWorkingDirectoryException ex) {
throw new MojoExecutionException(
"<container><workingDirectory> is not an absolute Unix-style path: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
Expand Down Expand Up @@ -140,6 +141,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue(),
ex);

} catch (InvalidContainerizingModeException ex) {
throw new MojoExecutionException(
"invalid value for <containerizingMode>: " + ex.getInvalidContainerizingMode(), ex);

} catch (InvalidWorkingDirectoryException ex) {
throw new MojoExecutionException(
"<container><workingDirectory> is not an absolute Unix-style path: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
Expand Down Expand Up @@ -118,6 +119,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue(),
ex);

} catch (InvalidContainerizingModeException ex) {
throw new MojoExecutionException(
"invalid value for <containerizingMode>: " + ex.getInvalidContainerizingMode(), ex);

} catch (InvalidWorkingDirectoryException ex) {
throw new MojoExecutionException(
"<container><workingDirectory> is not an absolute Unix-style path: "
Expand Down
Loading