Skip to content

Commit

Permalink
Fix class cast exception in gradle plugin main class detection logic (#…
Browse files Browse the repository at this point in the history
…3435)

* Fix class cast exception in gradle plugin main class detection logic

The main class detection logic in the gradle plugin always assumed that
the `Main-Class` attribute, set in the main jar task, is a string. This
is not always true, as it sometimes makes sense to set it to a provider
of String in order to link multiple configurations together.

The logic now tests the class of the value returned for the `Main-Class`
attribute and in case of an `Provider<?>` value, it will call the `get`
method. If the provider is not set, this will cause the task to fail.
The resulting object is then transformed to a String.

fixes #3396

* Update jib-gradle-plugin/CHANGELOG.md

Co-authored-by: Chanseok Oh <[email protected]>
  • Loading branch information
patrickpichler and chanseokoh authored Aug 18, 2021
1 parent 93d56f8 commit 0050ee4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
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

0 comments on commit 0050ee4

Please sign in to comment.