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

Support lazy configuration for mainClass and jvmFlags properties of ContainerParameters #3936

Merged
merged 5 commits into from Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions jib-gradle-plugin/README.md
Expand Up @@ -255,9 +255,9 @@ Property | Type | Default | Description
`expandClasspathDependencies` | `boolean` | `false` | <ul><li>Java 8 *or* Jib < 3.1: When set to true, does not use a wildcard (for example, `/app/lib/*`) for dependency JARs in the default Java runtime classpath but instead enumerates the JARs. Has the effect of preserving the classpath loading order as defined by the Gradle project.</li><li>Java >= 9 *and* Jib >= 3.1: The option has no effect. Jib *always* enumerates the dependency JARs. This is achieved by [creating and using an argument file](#custom-container-entrypoint) for the `--class-path` JVM argument.</li></ul>
`filesModificationTime` | `String` | `EPOCH_PLUS_SECOND` | Sets the modification time (last modified time) of files in the image put by Jib. (Note that this does not set the image creation time, which can be set using `jib.container.creationTime`.) The value should either be `EPOCH_PLUS_SECOND` to set the timestamps to Epoch + 1 second (default behavior), or an ISO 8601 date-time parsable with [`DateTimeFormatter.ISO_DATE_TIME`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html#ISO_DATE_TIME) such as `2019-07-15T10:15:30+09:00` or `2011-12-03T22:42:05Z`. The value can also be initialized [lazily](https://docs.gradle.org/current/userguide/lazy_configuration.html) with a provider.
`format` | `String` | `Docker` | Use `OCI` to build an [OCI container image](https://www.opencontainers.org/).
`jvmFlags` | `List<String>` | *None* | Additional flags to pass into the JVM when running your application.
`jvmFlags` | `List<String>` | *None* | Additional flags to pass into the JVM when running your application. The value can also be initialized [lazily](https://docs.gradle.org/current/userguide/lazy_configuration.html) with a provider.
`labels` | `Map<String, String>` | *None* | Key-value pairs for applying image metadata (similar to Docker's [LABEL](https://docs.docker.com/engine/reference/builder/#label) instruction).
`mainClass` | `String` | *Inferred*\*\* | The main class to launch your application from.
`mainClass` | `String` | *Inferred*\*\* | The main class to launch your application from. The value can also be initialized [lazily](https://docs.gradle.org/current/userguide/lazy_configuration.html) with a provider.
`ports` | `List<String>` | *None* | Ports that the container exposes at runtime (similar to Docker's [EXPOSE](https://docs.docker.com/engine/reference/builder/#expose) instruction).
`user` | `String` | *None* | The user and group to run the container as. The value can be a username or UID along with an optional groupname or GID. The following are all valid: `user`, `uid`, `user:group`, `uid:gid`, `uid:group`, `user:gid`.
`volumes` | `List<String>` | *None* | Specifies a list of mount points on the container.
Expand Down
Expand Up @@ -26,8 +26,10 @@
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

Expand All @@ -37,12 +39,12 @@
*/
public class ContainerParameters {

private List<String> jvmFlags = Collections.emptyList();
private final ListProperty<String> jvmFlags;
private Map<String, String> environment = Collections.emptyMap();
@Nullable private List<String> entrypoint;
private List<String> extraClasspath = Collections.emptyList();
private boolean expandClasspathDependencies;
@Nullable private String mainClass;
private final Property<String> mainClass;
@Nullable private List<String> args;
private ImageFormat format = ImageFormat.Docker;
private List<String> ports = Collections.emptyList();
Expand All @@ -59,6 +61,8 @@ public ContainerParameters(ObjectFactory objectFactory) {
labels = objectFactory.mapProperty(String.class, String.class).empty();
filesModificationTime = objectFactory.property(String.class).convention("EPOCH_PLUS_SECOND");
creationTime = objectFactory.property(String.class).convention("EPOCH");
mainClass = objectFactory.property(String.class);
jvmFlags = objectFactory.listProperty(String.class);
}

@Input
Expand All @@ -83,15 +87,19 @@ public void setEntrypoint(String entrypoint) {
@Input
@Optional
public List<String> getJvmFlags() {
if (System.getProperty(PropertyNames.CONTAINER_JVM_FLAGS) != null) {
return ConfigurationPropertyValidator.parseListProperty(
System.getProperty(PropertyNames.CONTAINER_JVM_FLAGS));
String jvmFlagsSystemProperty = System.getProperty(PropertyNames.CONTAINER_JVM_FLAGS);
if (jvmFlagsSystemProperty != null) {
return ConfigurationPropertyValidator.parseListProperty(jvmFlagsSystemProperty);
}
return jvmFlags;
return jvmFlags.getOrElse(Collections.emptyList());
}

public void setJvmFlags(List<String> jvmFlags) {
this.jvmFlags = jvmFlags;
this.jvmFlags.set(jvmFlags);
}

public void setJvmFlags(Provider<List<String>> jvmFlags) {
this.jvmFlags.set(jvmFlags);
}

@Input
Expand Down Expand Up @@ -138,14 +146,19 @@ public void setExpandClasspathDependencies(boolean expand) {
@Nullable
emmileaf marked this conversation as resolved.
Show resolved Hide resolved
@Optional
public String getMainClass() {
if (System.getProperty(PropertyNames.CONTAINER_MAIN_CLASS) != null) {
return System.getProperty(PropertyNames.CONTAINER_MAIN_CLASS);
String mainClassProperty = System.getProperty(PropertyNames.CONTAINER_MAIN_CLASS);
if (mainClassProperty != null) {
return mainClassProperty;
}
return mainClass;
return mainClass.getOrNull();
}

public void setMainClass(String mainClass) {
this.mainClass = mainClass;
this.mainClass.set(mainClass);
}

public void setMainClass(Provider<String> mainClass) {
this.mainClass.set(mainClass);
}

@Input
Expand Down
Expand Up @@ -407,6 +407,18 @@ public void testLazyEvalForContainerCreationAndFileModificationTimes() {
assertThat(output).contains("filesModificationTime=2022-07-19T11:23:42Z");
}

@Test
public void testLazyEvalForMainClass() {
BuildResult showLabels = testProject.build("showMainClass");
assertThat(showLabels.getOutput()).contains("mainClass value updated");
}

@Test
public void testLazyEvalForJvmFlags() {
BuildResult showLabels = testProject.build("showJvmFlags");
assertThat(showLabels.getOutput()).contains("jvmFlags value [updated]");
}

private Project createProject(String... plugins) {
Project project =
ProjectBuilder.builder().withProjectDir(testProjectRoot.getRoot()).withName("root").build();
Expand Down
Expand Up @@ -39,6 +39,8 @@ jib {
}
creationTime = project.provider { project.ext.jibCreationTime }
filesModificationTime = project.provider { project.ext.jibFilesModificationTime }
mainClass = project.provider { project.ext.value }
jvmFlags = project.provider { [project.ext.value] }
}
extraDirectories {
paths = project.provider { ['src/main/' + project.ext.value + '-custom-extra-dir'] }
Expand All @@ -64,3 +66,13 @@ tasks.register('showtimes') {
prop = project.extensions.jib.container.filesModificationTime.get()
println('filesModificationTime=' + prop)
}

tasks.register('showMainClass') {
String prop = project.extensions.jib.container.mainClass
println('mainClass value ' + prop)
}

tasks.register('showJvmFlags') {
List<String> prop = project.extensions.jib.container.jvmFlags
println('jvmFlags value ' + prop)
}