Skip to content

BootRepackage requires mainClassName propery even if mainClass option is explicitly specified #5956

@Infeligo

Description

@Infeligo

I was trying to create a fat jar with integration tests, but ran into this problem. The important part of Gradle configuration is:

task integTestBoot(type: BootRepackage, dependsOn: integTestJar) {
    withJarTask = integTestJar
    mainClass = 'org.springframework.boot.issues.gh1370.IntegrationTestSuite'
    customConfiguration = "integTestRuntime"
}

Here I specified the main class using mainClass option. The main class of the application itself is determined by Spring, meaning that do not explicitly specify any main class elsewhere and everything works. Running gradle integTestBoot task gives me the following error:

Execution failed for task ':integTestBoot'.
> Cannot get property 'mainClassName' on extra properties extension as it does not exist

After digging into the source, I found the cause in RepackageTask class:

        ...
        private void setMainClass(Repackager repackager) {
            String mainClassName = getMainClassNameProperty();
            if (RepackageTask.this.mainClass != null) {
                mainClassName = RepackageTask.this.mainClass;
            }
            ...
        }
        ...
        private String getMainClassNameProperty() {
            if (getProject().hasProperty("mainClassName")) {
                return (String) getProject().property("mainClassName");
            }
            ExtraPropertiesExtension extraProperties = (ExtraPropertiesExtension) getProject()
                    .getExtensions().getByName("ext");
            // My comment: This will throw exception if property not found
            return (String) extraProperties.get("mainClassName");
        }

When determining the main class, RepackageTask unconditionally looks for mainClassName property, including in the extraProperties object. Unfortunately, calling get for non-existent property throws an exception. If would suggest using has method to check if property exists beforehand.

The workaround at the moment is to explicitly specify ext.mainClassName for integration tests package and also re-override mainClass for the application. This works, but means a lot of boilerplate code.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions