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

Fix class cast exception in gradle plugin main class detection logic #3435

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file.

### Fixed

- Fixed `ClassCastException` when using non-`String` value (for example, [`Provider`](https://docs.gradle.org/current/javadoc/org/gradle/api/provider/Provider.html)) for `Main-Class` manifest attribute of the `jar` task. ([#3396](https://github.com/GoogleContainerTools/jib/issues/3396))

## 3.1.4

### Changed
Expand Down Expand Up @@ -62,8 +64,8 @@ All notable changes to this project will be documented in this file.
jib {
configurationName = 'myconfig'
}
```
```

### Changed

- [Switched the default base images](https://github.com/GoogleContainerTools/jib/blob/master/docs/default_base_image.md) from Distroless to [`adoptopenjdk:{8,11}-jre`](https://hub.docker.com/_/adoptopenjdk) and [`jetty`](https://hub.docker.com/_/jetty) (for WAR). ([#3124](https://github.com/GoogleContainerTools/jib/pull/3124))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.WarPlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.jvm.tasks.Jar;
Expand Down Expand Up @@ -370,7 +371,22 @@ public String getMainClassFromJarPlugin() {
if (jarTask == null) {
return null;
}
return (String) jarTask.getManifest().getAttributes().get("Main-Class");

Object value = jarTask.getManifest().getAttributes().get("Main-Class");

if (value instanceof Provider) {
value = ((Provider<?>) value).getOrNull();
}

if (value instanceof String) {
return (String) value;
}

if (value == null) {
return null;
}

return String.valueOf(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.bundling.War;
import org.gradle.jvm.tasks.Jar;
import org.gradle.testfixtures.ProjectBuilder;
Expand Down Expand Up @@ -180,6 +181,27 @@ public void testGetMainClassFromJar_missing() {
assertThat(gradleProjectProperties.getMainClassFromJarPlugin()).isNull();
}

@Test
public void testGetMainClassFromJarAsProperty_success() {
Property<String> mainClass =
project.getObjects().property(String.class).value("some.main.class");

Jar jar = project.getTasks().withType(Jar.class).getByName("jar");
jar.setManifest(new DefaultManifest(null).attributes(ImmutableMap.of("Main-Class", mainClass)));

assertThat(gradleProjectProperties.getMainClassFromJarPlugin()).isEqualTo("some.main.class");
}

@Test
public void testGetMainClassFromJarAsPropertyWithValueNull_missing() {
Property<String> mainClass = project.getObjects().property(String.class).value((String) null);

Jar jar = project.getTasks().withType(Jar.class).getByName("jar");
jar.setManifest(new DefaultManifest(null).attributes(ImmutableMap.of("Main-Class", mainClass)));

assertThat(gradleProjectProperties.getMainClassFromJarPlugin()).isNull();
}

@Test
public void testIsWarProject() {
project.getPlugins().apply("war");
Expand Down