Search code examples
javakotlingradlejar

no main manifest attribute error in multimodule app with gradle


I have multimodule java/kotlin app with Gradle. I wanna make .jar to launch my app in a terminal: like java -jar mayApp.jar

How correct build .jar in multimodule app? My .jar generated by IDEA is not runs when I trying in terminal due to error:

no main manifest attribute, in /Users/me/IdeaProjects/MyProject/out/artifacts/MyProject_jar/MyProject.jar

project structure:

- :ApplicationName
    - :bot-app
        - src/main/java/main
            Main.java // psvm
        - src/main/resources
             - META-INF
                 MANIFEST.MF
        build.gradle // module's build 
        
    - :data
    - :utils
    build.gradle // application (root) build

app-module organization

So, in my multimodule project the main class is located in the :bot-app module. Each module has its own build.gradle and in the root project I have build.gradle of the app;

Module build.gradle


buildscript {

    repositories {
        ...
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
    }
}

apply plugin: "org.jetbrains.kotlin.jvm"
apply plugin: 'kotlin-kapt'

group 'org.my_project'
version '2.4.0'

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib"
}

And it is my root build.gradle


buildscript {

    repositories {
        ...
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
    }
}

apply plugin: 'java'

tasks.withType(Jar) {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes["Main-Class"] = "main.Main"
    }
}

As u see, I added

tasks.withType(Jar) {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes["Main-Class"] = "main.Main"
    }
}

but it is not works for me. With 1 module it worked, but after refactoring to multimodule, no.

How build .jar in multimodule app?

---- UPD:

if I delete the .gradle folder in the root project and then try to run the app via IDEA it works well. But when I build artifacts via IDEA, jar is created but not works with the error: no main manifest attribute

And each next build in IDEA is failed with error: `Execution failed for task ':bot-app:jar'.

Entry META-INF/bot-app.kotlin_module is a duplicate but no duplicate handling strategy has been set.`

If I delete .gradle again, build in IDEA works well.


Solution

  • Need to delete:

    tasks.withType(Jar) {
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        manifest {
            attributes["Main-Class"] = "main.Main"
        }
    }
    

    This code is extra for me.

    Then create MANIFEST.MF at the root of the project.

    Note: you must create it via file->project structure->artifact->+ and in the field Directory for META-INF/MANIFEST.MF: u must set root folder location (NOT module folder, but root)

    Remove .gradle, clean, and build artifacts.