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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add containerizingMode config
chanseokoh committed May 15, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 526a1bab8fc7fe1e4813c581e04894680b3e92ac
Original file line number Diff line number Diff line change
@@ -369,7 +369,8 @@ private JibContainer buildImage(
Jib.from(baseImage)
.setEntrypoint(
JavaEntrypointConstructor.makeEntrypoint(
JavaEntrypointConstructor.defaultClasspath(AbsoluteUnixPath.get("/app")),
JavaEntrypointConstructor.defaultClasspath(
AbsoluteUnixPath.get("/app"), "exploded"),
Collections.emptyList(),
"HelloWorld"))
.setProgramArguments(Collections.singletonList("An argument."))
Original file line number Diff line number Diff line change
@@ -173,7 +173,8 @@ public static JavaContainerBuilder from(RegistryImage registryImage) {
JavaEntrypointConstructor.DEFAULT_RELATIVE_RESOURCES_PATH_ON_IMAGE;
private RelativeUnixPath dependenciesDestination =
JavaEntrypointConstructor.DEFAULT_RELATIVE_DEPENDENCIES_PATH_ON_IMAGE;
private RelativeUnixPath othersDestination = RelativeUnixPath.get("classpath");
private RelativeUnixPath othersDestination =
JavaEntrypointConstructor.DEFAULT_RELATIVE_OTHERS_PATH_ON_IMAGE;
@Nullable private String mainClass;

private JavaContainerBuilder(JibContainerBuilder jibContainerBuilder) {
Original file line number Diff line number Diff line change
@@ -31,14 +31,22 @@ public class JavaEntrypointConstructor {
RelativeUnixPath.get("classes");
public static final RelativeUnixPath DEFAULT_RELATIVE_DEPENDENCIES_PATH_ON_IMAGE =
RelativeUnixPath.get("libs");
public static final RelativeUnixPath DEFAULT_RELATIVE_OTHERS_PATH_ON_IMAGE =
RelativeUnixPath.get("classpath");

public static List<String> defaultClasspath(AbsoluteUnixPath appRoot) {
public static List<String> defaultClasspath(AbsoluteUnixPath appRoot, String containerizingMode) {
return Arrays.asList(
appRoot.resolve(DEFAULT_RELATIVE_RESOURCES_PATH_ON_IMAGE).toString(),
appRoot.resolve(DEFAULT_RELATIVE_CLASSES_PATH_ON_IMAGE).toString(),
appRoot.resolve(DEFAULT_RELATIVE_DEPENDENCIES_PATH_ON_IMAGE).resolve("*").toString());
}

public static List<String> defaultPackagedClasspath(AbsoluteUnixPath appRoot) {
return Arrays.asList(
appRoot.resolve(DEFAULT_RELATIVE_OTHERS_PATH_ON_IMAGE).resolve("*").toString(),
appRoot.resolve(DEFAULT_RELATIVE_DEPENDENCIES_PATH_ON_IMAGE).resolve("*").toString());
}

/**
* Constructs the container entrypoint.
*
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public class JavaEntrypointConstructorTest {
@Test
public void testDefaultClasspath() {
List<String> classpath =
JavaEntrypointConstructor.defaultClasspath(AbsoluteUnixPath.get("/dir"));
JavaEntrypointConstructor.defaultClasspath(AbsoluteUnixPath.get("/dir"), "exploded");
Assert.assertEquals(
ImmutableList.of("/dir/resources", "/dir/classes", "/dir/libs/*"), classpath);
}
Original file line number Diff line number Diff line change
@@ -27,6 +27,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;
@@ -154,6 +155,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: "
Original file line number Diff line number Diff line change
@@ -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;
@@ -134,6 +135,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: "
Original file line number Diff line number Diff line change
@@ -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;
@@ -156,6 +157,10 @@ public void buildTar()
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: "
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.event.events.LogEvent;
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;
@@ -153,7 +154,8 @@ private static boolean isProgressFooterEnabled(Project project) {
}

@Override
public JibContainerBuilder createContainerBuilder(RegistryImage baseImage) {
public JibContainerBuilder createContainerBuilder(
RegistryImage baseImage, ContainerizingMode containerizingMode) {
try {
if (isWarProject()) {
logger.info("WAR project identified, creating WAR image: " + project.getDisplayName());
Original file line number Diff line number Diff line change
@@ -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
@@ -70,6 +70,7 @@ public class JibExtension {
private final ContainerParameters container;
private final ExtraDirectoriesParameters extraDirectories;
private final Property<Boolean> allowInsecureRegistries;
private final Property<String> containerizingMode;

@Deprecated boolean extraDirectoryConfigured;
@Deprecated boolean extraDirectoriesConfigured;
@@ -83,9 +84,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("exploded");
}

public void from(Action<? super BaseImageParameters> action) {
@@ -161,4 +164,10 @@ boolean getAllowInsecureRegistries() {
}
return allowInsecureRegistries.get();
}

@Input
@Optional
public String getContainerizingMode() {
return containerizingMode.get();
}
}
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.configuration.LayerConfiguration;
import com.google.cloud.tools.jib.image.LayerEntry;
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;
@@ -361,7 +362,8 @@ public void testCreateContainerBuilder_noClassesFiles() throws InvalidImageRefer
Path nonexistentFile = Paths.get("/nonexistent/file");
Mockito.when(mockMainSourceSetOutput.getClassesDirs())
.thenReturn(new TestFileCollection(ImmutableSet.of(nonexistentFile)));
gradleProjectProperties.createContainerBuilder(RegistryImage.named("base"));
gradleProjectProperties.createContainerBuilder(
RegistryImage.named("base"), ContainerizingMode.EXPLODED);
Mockito.verify(mockLogger).warn("No classes files were found - did you compile your project?");
}

@@ -552,7 +554,7 @@ private BuildConfiguration setupBuildConfiguration(String appRoot)
throws InvalidImageReferenceException, IOException, CacheDirectoryCreationException {
JibContainerBuilder jibContainerBuilder =
new GradleProjectProperties(mockProject, mockLogger, AbsoluteUnixPath.get(appRoot))
.createContainerBuilder(RegistryImage.named("base"));
.createContainerBuilder(RegistryImage.named("base"), ContainerizingMode.EXPLODED);
return JibContainerBuilderTestHelper.toBuildConfiguration(
jibContainerBuilder,
Containerizer.to(RegistryImage.named("to"))
Original file line number Diff line number Diff line change
@@ -29,6 +29,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;
@@ -155,6 +156,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: "
Original file line number Diff line number Diff line change
@@ -28,6 +28,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;
@@ -149,6 +150,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: "
Original file line number Diff line number Diff line change
@@ -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;
@@ -127,6 +128,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: "
Original file line number Diff line number Diff line change
@@ -250,6 +250,9 @@ public List<File> getPaths() {
property = PropertyNames.ALLOW_INSECURE_REGISTRIES)
private boolean allowInsecureRegistries;

@Parameter(defaultValue = "exploded", property = PropertyNames.CONTAINERIZING_MODE)
private String containerizingMode;

@Parameter(defaultValue = "false", property = PropertyNames.SKIP)
private boolean skip;

@@ -623,6 +626,10 @@ boolean getAllowInsecureRegistries() {
return allowInsecureRegistries;
}

public String getContainerizingMode() {
return containerizingMode;
}

boolean isSkipped() {
return skip;
}
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.event.events.LogEvent;
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;
@@ -200,14 +201,16 @@ static int getVersionFromString(String versionString) {
}

@Override
public JibContainerBuilder createContainerBuilder(RegistryImage baseImage) throws IOException {
boolean putRunnableArtifact = true;
public JibContainerBuilder createContainerBuilder(
RegistryImage baseImage, ContainerizingMode containerizingMode) throws IOException {
try {
if (isWarProject()) {
Path explodedWarPath =
Paths.get(project.getBuild().getDirectory()).resolve(project.getBuild().getFinalName());
return JavaContainerBuilderHelper.fromExplodedWar(baseImage, explodedWarPath, appRoot);
} else if (putRunnableArtifact) {
}

if (containerizingMode == ContainerizingMode.PACKAGED) {
Path artifact =
Paths.get(project.getBuild().getDirectory())
.resolve(project.getBuild().getFinalName() + "." + project.getPackaging());
Original file line number Diff line number Diff line change
@@ -164,4 +164,9 @@ public List<Path> getExtraDirectories() {
public Map<AbsoluteUnixPath, FilePermissions> getExtraDirectoryPermissions() {
return MojoCommon.convertPermissionsList(jibPluginConfiguration.getExtraDirectoryPermissions());
}

@Override
public String getContainerizingMode() {
return jibPluginConfiguration.getContainerizingMode();
}
}
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.configuration.LayerConfiguration;
import com.google.cloud.tools.jib.image.LayerEntry;
import com.google.cloud.tools.jib.plugins.common.ContainerizingMode;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
@@ -547,7 +548,7 @@ private BuildConfiguration setupBuildConfiguration(AbsoluteUnixPath appRoot)
throws InvalidImageReferenceException, IOException, CacheDirectoryCreationException {
JibContainerBuilder JibContainerBuilder =
new MavenProjectProperties(mockMavenProject, mockMavenSession, mockLog, appRoot)
.createContainerBuilder(RegistryImage.named("base"));
.createContainerBuilder(RegistryImage.named("base"), ContainerizingMode.EXPLODED);
return JibContainerBuilderTestHelper.toBuildConfiguration(
JibContainerBuilder,
Containerizer.to(RegistryImage.named("to"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.plugins.common;

/**
* Containerizing mode.
*
* <ul>
* <li>{@code EXPLODED} puts individual application files without packaging.
* <li>{@code PACKAGED} puts a single packaged artifact for an application.
* </ul>
*/
public enum ContainerizingMode {
EXPLODED,
PACKAGED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2018 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.plugins.common;

/** Indicates that the {@code containerizingMode} config value is invalid. */
public class InvalidContainerizingModeException extends Exception {

private final String invalidContainerizingMode;

public InvalidContainerizingModeException(String message, String invalidContainerizingMode) {
super(message);
this.invalidContainerizingMode = invalidContainerizingMode;
}

public String getInvalidContainerizingMode() {
return invalidContainerizingMode;
}
}
Loading