Gradle 设置自定义 jar 的存档文件
Gradle set archiveFile of a custom jar
给定一个简单的结构:
.
├── 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
。这没有帮助。
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'
}
给定一个简单的结构:
.
├── 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
。这没有帮助。
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'
}