java 在 Gradle 应用程序插件中部署其他文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5743036/
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
Deploy additional files in Gradle Application Plugin
提问by James Blewitt
I have a small Java/Gradle project. I'm using the Application pluginto create a zip distribution (using the distZip task). Using the standard configuration I get the following directories in my zip file:
我有一个小型 Java/Gradle 项目。我正在使用应用程序插件来创建 zip 分发(使用 distZip 任务)。使用标准配置,我在 zip 文件中得到以下目录:
/bin- The scripts to start the application go in here
/lib- Contains my project code in a JAR file and all dependency JAR files.
/bin- 启动应用程序的脚本放在此处
/lib- 在 JAR 文件和所有依赖 JAR 文件中包含我的项目代码。
The trouble is that I would like a third directory: /confwhere I can put my configuration files (instead of having them packaged inside my application JAR file.
问题是我想要第三个目录:/conf,我可以在其中放置我的配置文件(而不是将它们打包在我的应用程序 JAR 文件中。
I imagine that this is a pretty common requirement because things like log4j.xml and hibernate.properties would be better placed outside the JAR file. I just can't figure out how I can customise the behavior of the Application plugin to do this however.
我想这是一个非常普遍的要求,因为像 log4j.xml 和 hibernate.properties 这样的东西最好放在 JAR 文件之外。但是,我无法弄清楚如何自定义应用程序插件的行为来执行此操作。
回答by James Blewitt
I revisited this problem several months later and I finally have an elegant solution. The following code should be added to the gradle file:
几个月后我重新审视了这个问题,我终于有了一个优雅的解决方案。应将以下代码添加到 gradle 文件中:
distZip {
into(project.name) {
from '.'
include 'conf/*'
}
}
This adds an additional include to the distZip task. This copies the "conf" directory (including contents) into the Zip distribution.
这为 distZip 任务添加了一个额外的包含。这会将“conf”目录(包括内容)复制到 Zip 发行版中。
The generated zip file contains a single directory which is the same as the project name. This is why the "into" part is required.
生成的 zip 文件包含一个与项目名称相同的目录。这就是为什么需要“进入”部分的原因。
回答by Graham
Actually, create a dist
dir under the src
dir in your project. Anything in this dir is copied by the application plugin (under applicationDistribution
) when installApp or distZip is run.
实际上,在您的项目中的dist
目录下创建一个目录src
。applicationDistribution
当 installApp 或 distZip 运行时,此目录中的任何内容都由应用程序插件(在 下)复制。
Or edit applicationDistribution
to do other things, if a simple copy is not enough.
或者编辑applicationDistribution
做其他事情,如果简单的副本是不够的。
回答by ferdy
For me, a simple
对我来说,一个简单的
applicationDistribution.from("src/main/config/") {
into "config"
}
did the job. Of course you need to have your properties loaded correctly from within code. Especially if you move them from src/main/resources where they have been usable via classpath, into the new location. I circumvented this by adding a command line parameter which points to the configuration file.
完成了工作。当然,您需要从代码中正确加载您的属性。特别是如果您将它们从 src/main/resources 中通过类路径可以使用的位置移动到新位置。我通过添加一个指向配置文件的命令行参数来规避这一点。
回答by c_maker
I am not sure whether you can customize the application plugin, I have never used it. There is however other ways to achieve what you want to achieve.
我不确定您是否可以自定义应用程序插件,我从未使用过它。然而,还有其他方法可以实现您想要实现的目标。
You may create a /conf
directory like this:
你可以创建一个/conf
这样的目录:
confDir = new File("$buildDir/conf")
You can then copy the files you need into this directory like this:
然后,您可以将需要的文件复制到此目录中,如下所示:
task copyConfFiles(type: Copy) {
from _wherever your files reside_
into confDir
include('**/*.properties') // your configuration files
}
You may then hook this copy task into the process like this:
然后,您可以将此复制任务挂接到流程中,如下所示:
distZip.dependsOn copyConfFiles
And last if you do not want your configurations in the final zip, you can do this:
最后,如果您不希望在最终 zip 中配置您的配置,您可以这样做:
distZip {
exclude('**/*.properties') // your configuration files
}
Again, there might be a better way. This is away.
同样,可能有更好的方法。这是一种方式。
回答by Abhijit Sarkar
OP's self-answer may be good for his use case, but there are a few things I'd like to improve on:
OP 的自我回答可能对他的用例有好处,但我想改进以下几点:
- His answer suggests that he has a directory
conf
parallel to thebuild.gradle
. There is no such thing in the Maven Standard Directory Layout. The general consensus is to have asrc/main/conf
as had been hinted to in the docs:
- 他的回答表明他有一个
conf
与build.gradle
. Maven Standard Directory Layout 中没有这样的东西。普遍的共识是src/main/conf
按照文档中的提示进行:
If there are other contributing sources to the artifact build, they would be under other subdirectories: for example src/main/antlr would contain Antlr grammar definition files.
如果工件构建有其他贡献源,它们将位于其他子目录下:例如 src/main/antlr 将包含 Antlr 语法定义文件。
The target directory name is NOT
project.name
as had been pointed out in a comment.If resource filtering is required, and it often is, then having a separate task is desirable. During local development, this task can be run to generate the filtered files. The distribution would merely use the output of this task (and unlike OP's answer, this also makes
conf
available to the tar distribution).def props = new Properties() file("src/main/filters/application.properties") .withInputStream { props.load(it) } import org.apache.tools.ant.filters.ReplaceTokens task copyConf(type: Copy) { from("src/main/conf/") into("$buildDir/conf") filesMatching("**/*.y*ml") { filter(tokens: props, ReplaceTokens) } } distributions { main { contents { from(copyConf) { into("conf") } } } }
目标目录名称
project.name
与评论中指出的不同。如果需要资源过滤,而且经常是这样,那么需要单独的任务。在本地开发期间,可以运行此任务以生成过滤后的文件。该发行版仅使用此任务的输出(与 OP 的答案不同,这也
conf
可用于 tar 发行版)。def props = new Properties() file("src/main/filters/application.properties") .withInputStream { props.load(it) } import org.apache.tools.ant.filters.ReplaceTokens task copyConf(type: Copy) { from("src/main/conf/") into("$buildDir/conf") filesMatching("**/*.y*ml") { filter(tokens: props, ReplaceTokens) } } distributions { main { contents { from(copyConf) { into("conf") } } } }