給定一個簡單的結構:
.
├── build.gradle
└── folder
├── file.txt
└── other.txt
我正在使用最新的 gradle 版本 (7.2)。
我嘗試用檔案夾的內容構建一個 jar。
測驗 1
使用以下build.gradle檔案,定義自定義 jar 任務:
task customJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Main-Class': 'xxx.aaa.Bbb'
}
from 'folder'
}
當我運行時,./gradlew customJar我收到此錯誤:
> Task :customJar FAILED
FAILURE: Build failed with an exception.
* What went wrong:
A problem was found with the configuration of task ':customJar' (type 'Jar').
- Type 'org.gradle.api.tasks.bundling.Jar' property 'archiveFile' doesn't have a configured value.
Reason: This property isn't marked as optional and no value has been configured.
Possible solutions:
1. Assign a value to 'archiveFile'.
2. Mark property 'archiveFile' as optional.
Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#value_not_set for more details about this problem.
測驗 2
當我將build.gradle檔案中的任務更新為:
task customJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Main-Class': 'xxx.aaa.Bbb'
}
archiveFile = 'custom-jar'
from 'folder'
}
我得到這個結果:
FAILURE: Build failed with an exception.
* Where:
Build file '/<path to my project>/build.gradle' line: 101
* What went wrong:
A problem occurred evaluating root project 'tmp'.
> Cannot set the value of read-only property 'archiveFile' for task ':customJar' of type org.gradle.api.tasks.bundling.Jar.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
其他帖子表示設定baseName或archiveBaseName。這沒有幫助。
uj5u.com熱心網友回復:
archiveFile是一個計算值,需要設定destinationDirectory和baseName。這將起作用:
tasks.register("customJar", Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Main-Class': 'xxx.aaa.Bbb'
}
destinationDirectory = layout.buildDirectory.dir("libs")
baseName = 'custom-jar'
from 'folder'
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/355001.html
