![](/img/trans.png)
[英]How can I change mainClassName in build.gradle during runtime?
[英]Gradle error with lack of mainClassName property in gradle build
我有兩個子項目的 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'.
我的配置: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')
}
根 - setting.gradle:
include 'app'
include 'core'
APP 項目 - build.gradle:
EMPTY
核心項目 - build.gradle:
dependencies {
compile 'com.google.dagger:dagger:2.4'
compile 'com.google.dagger:dagger-compiler:2.4'
}
和兩個子項目(類似) - gradle.properties:
version = 0.1-SNAPSHOT
name = MorePianer Core Lib
mainClassName = pl.morecraft.dev.morepianer.core.Core
我嘗試在很多地方添加 mainClassName 屬性,但它不起作用。 更奇怪的是,它在幾天前有效。 我第一次使用 gradle,我正在考慮切換回 maven。
application
插件在捆綁應用程序時需要知道主類。
在您的情況下,您為每個子項目application
插件,而無需為每個子項目指定主類。
我遇到了同樣的問題並通過在與apply plugin: 'application'
相同的級別指定“mainClassName”來修復它:
apply plugin: 'application'
mainClassName = 'com.something.MyMainClass'
如果您想在gradle.properties
文件中指定它,您可能必須將其寫為: projectName.mainClassName = ..
而不是設置 mainClassName 嘗試創建
task run(type: JavaExec, dependsOn: classes) {
main = 'com.something.MyMainClass'
classpath = sourceSets.main.runtimeClasspath
}
每當我們將 gradle 腳本與應用程序插件綁定時,Gradle 都希望我們指出應用程序的起點。 這是必要的,因為 Gradle 將使用給定的位置作為入口點開始捆綁您的應用程序(jar/zip)。
拋出此錯誤的原因很簡單,因為Gradle知道您想要捆綁應用程序,但不知道從哪里開始捆綁過程。
可以將mainClassName
指定為項目擴展屬性:
ext {
mainClassName = 'com.something.MyMainClass'
}
我在我的項目中遇到了同樣的問題,並通過從 gradle.properties 中排除來解決它:
#ogr.gradle.configurationdemand = true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.