gradle-在每种构建的一部分之前 /作为将其包含在jar中之前 /作为创建.properties文件
我希望gradle在构建项目时,将版本。
我有这样的任务
task createVersionPropertiesFile() {
def outputFile = new File(projectDir, "src/main/java/org/example/lpimport/version.properties")
outputs.file outputFile
doLast {
outputFile.text =
"""# generated by Gradle
version=$version
revision=${getSvnRevision()}
buildtime=${new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())}
"""
}
outputs.upToDateWhen { false }
}
,我将其包括在内:
build.dependsOn createVersionPropertiesFile
当我在eclipse中运行gradle build
任务时,将按预期创建文件! ✅
如果我在Eclipse中运行应用程序,则可以通过我的应用程序加载生成的版本
文件。 ✅
props.load( Main.class.getResourceAsStream( "version.properties" ) );
但是version.properties
文件将不是是myProject-1.0中构建
myProject-1.0.0.0.0.0.7.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. .0.zip
分发,尽管它位于普通类路径软件包中。 ❌
如何使gradle包括罐子内的即时生成的version.properties
?
I want Gradle to write a version.properties
file into my Java classpath whenever I build the project.
I have a task like this
task createVersionPropertiesFile() {
def outputFile = new File(projectDir, "src/main/java/org/example/lpimport/version.properties")
outputs.file outputFile
doLast {
outputFile.text =
"""# generated by Gradle
version=$version
revision=${getSvnRevision()}
buildtime=${new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())}
"""
}
outputs.upToDateWhen { false }
}
And I include it like this:
build.dependsOn createVersionPropertiesFile
When I run the Gradle build
task in Eclipse, the file is created as expected! ✅
If I run my application in Eclipse, the generated version.properties
file can be loaded by my application. ✅
props.load( Main.class.getResourceAsStream( "version.properties" ) );
But the version.properties
file will not be part of the built myproject-1.0.0.jar
inside the myproject-1.0.0.zip
distribution, although it resides in a normal class path package. ❌
How can I make Gradle include the on-the-fly generated version.properties
inside the JAR?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过进行两个更改解决了问题:
将
.properties
文件写入src/main/resources
而不是src/main/java
>:制作
compilejava
任务取决于我的自定义任务:
.properties
gradle忽略了文件(通过其JAR任务??),如果它位于src之下/main/java
。现在它有效,我的构建和包装应用程序可以在启动上显示其版本属性。
I solved the problem by making two changes:
Write the
.properties
file tosrc/main/resources
instead ofsrc/main/java
:Make the
compileJava
task depend on my custom task:It seems the
.properties
file is ignored by Gradle (by its jar task??) if it resides belowsrc/main/java
.Now it works and my built and packaged application can display its version properties on startup.
编辑
当然,一旦我写下这样的答案,我就会学到更好的方法。 Gradle具有
原始答案在下面保存下来
我已经完成了几次;可以在这里找到一个很好的(公开)示例: https://github.com/microsoft/thrifty/blob/master/thrifty-gradle-plugin/build.gradle#l45
链接的片段在下面的完整中复制,并提供额外的评论,解释了为什么这样的方法:
EDIT
Of course, as soon as I write a SO answer, I learn a better way. Gradle has a WriteProperties task that is built in. The original example can be simplified:
Original answer preserved below
I've done this a few times; a good (public) example can be found here: https://github.com/microsoft/thrifty/blob/master/thrifty-gradle-plugin/build.gradle#L45
The linked snippet is reproduced in full below, with extra comments explaining why it's this way: