Mateusz Stefaniak Mateusz Stefaniak - 3 months ago 27
Java Question

Gradle error with lack of mainClassName property in gradle build

I have graddle configuration with two subprojects and when I want do build the project the following error is thrown:

Executing external task 'build'...
:core:compileJava
:core:processResources UP-TO-DATE
:core:classes
:core:jar
:core:startScripts FAILED

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':core:startScripts'.
> No value has been specified for property 'mainClassName'.


My configuration:
ROOT - build.gradle:

subprojects {

apply plugin: 'java'
apply plugin: 'application'

group = 'pl.morecraft.dev.morepianer'

repositories {
mavenLocal()
mavenCentral()
}

run {
main = project.getProperty('mainClassName')
}

jar {
manifest {
attributes 'Implementation-Title': project.getProperty('name'),
'Implementation-Version': project.getProperty('version'),
'Main-Class': project.getProperty('mainClassName')
}
}

}

task copyJars(type: Copy, dependsOn: subprojects.jar) {
from(subprojects.jar)
into project.file('/jars')
}


ROOT - setting.gradle:

include 'app'
include 'core'


APP PROJECT - build.gradle:

EMPTY


CORE PROJECT - build.gradle:

dependencies {
compile 'com.google.dagger:dagger:2.4'
compile 'com.google.dagger:dagger-compiler:2.4'
}


AND BOTH SUBPROJECTS (SIMILAR) - gradle.properties:

version = 0.1-SNAPSHOT
name = MorePianer Core Lib
mainClassName = pl.morecraft.dev.morepianer.core.Core


I've tried to add the mainClassName property in many places, but it does not work. This is even stranger, that It worked a couple days ago as it is. I'm using gradle for first time and I'm thinking about switching back to maven.

Answer

The application plugin needs to know the main class for when it bundles the application.

In your case, you apply the application plugin for each subproject without specifying the main class for each of those subprojects.

I had the same problem and fixed it by specifying "mainClassName" at the same level as apply plugin: 'application' :

apply plugin: 'application'
mainClassName = 'com.something.MyMainClass'

If you want to specify it in the gradle.properties file you might have to write it as : projectName.mainClassName = ..