没有主清单属性错误在使用Gradle的多模块应用程序中。

huangapple go评论70阅读模式
英文:

no main manifest attribute error in multimodule app with gradle

问题

我有一个使用Gradle的多模块Java/Kotlin应用程序。我想要创建一个.jar文件,以便在终端中启动我的应用程序,就像这样:java -jar myApp.jar

如何在多模块应用程序中正确构建.jar文件?
我的应用程序通过IDEA生成的.jar文件在终端中运行时出现错误,错误消息如下:

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

项目结构如下:

  • :ApplicationName
    • :bot-app
      • src/main/java/main
        Main.java // psvm
      • src/main/resources
        • META-INF
          MANIFEST.MF
          build.gradle // 模块的构建
    • :data
    • :utils
      build.gradle // 应用程序(根)构建

没有主清单属性错误在使用Gradle的多模块应用程序中。

因此,在我的多模块项目中,主类位于:bot-app模块中。
每个模块都有自己的build.gradle文件,而在根项目中,我有应用程序的build.gradle文件。

模块的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"
}

这是我的根 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"
    }
}

正如您所看到的,我添加了以下部分:

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

但对我来说没有生效。在单模块情况下,它可以工作,但在重构为多模块后,就不行了。

如何在多模块应用程序中构建.jar文件?

----
UPD:

如果我删除根项目中的.gradle文件夹,然后尝试通过IDEA运行应用程序,它可以正常工作。但是,当我通过IDEA构建工件时,会创建.jar文件,但无法运行,出现以下错误:

no main manifest attribute

而且,每次在IDEA中进行构建时,都会失败,并显示以下错误:

`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.`

如果我再次删除.gradle文件夹,IDEA中的构建将正常工作。

英文:

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

没有主清单属性错误在使用Gradle的多模块应用程序中。

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.

答案1

得分: 0

需要删除的部分:

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

这段代码对我来说是多余的。

然后,在项目的根目录创建 MANIFEST.MF 文件。

注意:你必须通过 文件->项目结构->构件->+ 来创建它,并在 META-INF/MANIFEST.MF 目录字段中设置根文件夹位置(不是模块文件夹,而是根目录)。

移除 .gradle,清理并构建构件。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月18日 08:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定