javagradlegradle-kotlin-dslgradlewmulti-project

Not able to run gradle multi-project


I am trying to build a basic multi-project with gradle as suggested at Structuring Projects with Gradle

Project structure is as follows:

enter image description here

Main.java in test subproject is as follows:

package org.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

setting.gradle.kts under root project is as follows:

rootProject.name = "GradleTest"
include("test")

build.gradle.kts looks like this:

plugins {
    id("java")
    id("application")
}

application {
    var mainClass = "org.example.Main"
}

group = "org.example"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
}

tasks.getByName<Test>("test") {
    useJUnitPlatform()
}

When I am trying to run ./gradlew run under root project I am getting below error:

> Task :test:run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test:run'.
> No main class specified and classpath is not an executable jar.

* Try:
> Run with --stacktrace option to get the stack trace.

Can someone suggest what I am missing here ?

Thanks


Solution

  • This issue has been resolved. The discrepancy arose because I was following the documentation for the current version of Gradle (8.7), while my Gradle version was 7.5. It turns out there are significant differences in the way main classes are defined in build.gradle.kts between the two versions.

    So As per documentation for gradle 7.5 mainClass should be defined like this:

    application {
        mainClass.set("org.example.Main")
    }