Java gradle 构建中缺少 mainClassName 属性的 Gradle 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36917530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle error with lack of mainClassName property in gradle build
提问by Mateusz Stefaniak
I have graddle configuration with two subprojects and when I want do buildthe project the following error is thrown:
我有两个子项目的 graddle 配置,当我想构建项目时,会抛出以下错误:
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:
我的配置: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:
根 - setting.gradle:
include 'app'
include 'core'
APP PROJECT - build.gradle:
APP 项目 - build.gradle:
EMPTY
EMPTY
CORE PROJECT - build.gradle:
核心项目 - build.gradle:
dependencies {
compile 'com.google.dagger:dagger:2.4'
compile 'com.google.dagger:dagger-compiler:2.4'
}
AND BOTH SUBPROJECTS (SIMILAR) - gradle.properties:
和两个子项目(类似) - 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.
我尝试在很多地方添加 mainClassName 属性,但它不起作用。更奇怪的是,它在几天前有效。我第一次使用 gradle,我正在考虑切换回 maven。
采纳答案by zlandorf
The application
plugin needs to know the main class for when it bundles the application.
该application
插件需要知道当它捆绑的应用程序的主类。
In your case, you apply the application
plugin for each subproject without specifying the main class for each of those subprojects.
在您的情况下,您application
为每个子项目应用插件,而无需为每个子项目指定主类。
I had the same problem and fixed it by specifying "mainClassName" at the same level as apply plugin: 'application'
:
我遇到了同样的问题,并通过在与以下相同级别指定“mainClassName”来修复它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 = ..
如果要在gradle.properties
文件中指定它,则可能必须将其写为:projectName.mainClassName = ..
回答by Mika16
Instead of setting up mainClassName try to create
而不是设置 mainClassName 尝试创建
task run(type: JavaExec, dependsOn: classes) {
main = 'com.something.MyMainClass'
classpath = sourceSets.main.runtimeClasspath
}
Please look at Gradle fails when executes run task for scala
回答by AlexPes
I faced the same issue in my project and solved it by excluding from gradle.properties:
我在我的项目中遇到了同样的问题,并通过从 gradle.properties 中排除来解决它:
#ogr.gradle.configurationdemand = true
回答by swayamraina
Whenever we bind a gradle script with the applicationplugin, Gradle expects us to point out the starting point of the application. This is neccessary because, Gradle will start bundling your application (jar/zip) using the given location as the entry point.
This error is thrown simply because Gradleknows that you want to bundle your application but is clueless about where to start the bundling process.
每当我们将 gradle 脚本与应用程序插件绑定时,Gradle 都希望我们指出应用程序的起点。这是必要的,因为 Gradle 将使用给定的位置作为入口点开始捆绑您的应用程序(jar/zip)。
抛出此错误的原因很简单,因为Gradle知道您想要捆绑应用程序,但不知道从哪里开始捆绑过程。