如何使用 Buildr 有条件地将两个文件之一包含在 .war 包中?

发布于 2024-10-08 09:12:39 字数 1372 浏览 3 评论 0原文

我们使用 Buildr 将应用程序打包为 .war 文件。为了进一步简化自动化部署过程,我想有条件地选择这两个文件之一(从目录 src/main/resources 中)...

  • database-hsql.properties
  • database-postgres.properties

...并将其包含在生成的 .war 中,名称为 database.properties。 (在 WAR 中的同一位置,即 WEB-INF/classes/,来自 src/main/resources 的文件现在结束。)

任何简单的方法都可以做到这一点?任何方法都可以 - 例如参数化“package”(以某种方式)或定义两个不同的任务/目标(我不确定术语),例如“package-hsql”和“package-hsql”。 “package-pgsql”——只要它有效并且足够简单。

buildfile 的相关部分:

load 'dependencies' 
require 'buildr/hibernate'

desc "..."
define "foo" do

  project.version = VERSION_NUMBER
  project.group = GROUP
  manifest["Implementation-Vendor"] = COPYRIGHT
  compile.with WICKET,GUAVA,GSON, ... [many more libs]
  test.with MOCKITO
  resources

  test.compile.with SERVLET,_('src/main/webapp'),_('src/main/resources')
  package(:war).with(:libs=> [WICKET,GUAVA,GSON, ... ])

end

额外问题:如何执行任意 shell 命令(我会使用涉及例如 >> 或 < code>sed) 在这两种情况下对另一个配置文件进行更改? (我还想从另一个文件将一些数据库设置“注入”到 applicationContext.xml 中;这比在 VCS 中保留该文件的两个副本且内容基本相同更好。)

抱歉,如果这样太基础了;我是 Buildr 的新手,并且不太了解 Ruby。 (是的,使用当前团队成员都不精通的工具并不是最佳情况......)如果有任何需要澄清的地方,请指出!

We're using Buildr to package an application as a .war file. To streamline the automated deployment process further, I'd like to conditionally select one of these two files (from the directory src/main/resources)...

  • database-hsql.properties
  • database-postgres.properties

...and have it included in the resulting .war with the name database.properties. (In the same place inside the WAR, namely WEB-INF/classes/, where files from src/main/resources now end up.)

Any simple way to do this? Any approach would be fine — e.g. parameterising "package" (somehow) or defining two different tasks/targets (I'm not sure of the terminology) such as "package-hsql" & "package-pgsql" — as long as it works and is simple enough.

Relevant bits of buildfile:

load 'dependencies' 
require 'buildr/hibernate'

desc "..."
define "foo" do

  project.version = VERSION_NUMBER
  project.group = GROUP
  manifest["Implementation-Vendor"] = COPYRIGHT
  compile.with WICKET,GUAVA,GSON, ... [many more libs]
  test.with MOCKITO
  resources

  test.compile.with SERVLET,_('src/main/webapp'),_('src/main/resources')
  package(:war).with(:libs=> [WICKET,GUAVA,GSON, ... ])

end

Bonus question: how to execute an arbitrary shell command (I'd use something involving e.g. >> or sed) in each of the two cases to make changes to another config file? (I'd also want to "inject" some db settings to applicationContext.xml from another file; this would be preferable to keeping two copies of that file in VCS with mostly identical content.)

Sorry if this is too basic; I'm a total newbie with Buildr, and don't really know Ruby. (And yes, it's not an optimal situation to be using a tool that no current team member is proficient with...) If anything needs clarifying, please point it out!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2024-10-15 09:12:39

参数化构建器构建的常用方法是使用环境变量。因此,例如,您可能会将构建设计为像这样执行:

$ buildr package DATABASE=postgres

在您的构建文件中,您可能会:

ENV['DATABASE'] ||= 'hsql' # this line makes the default 'hsql'

define "foo" do
  # the rest of the definition

  resources.enhance do
    cp _(:source, :main, :resources, "database-#{ENV['DATABASE']}.properties"), _(:target, :resources, "database.properties")
  end
end

这是最简单的解决方案;您可能会认为一个缺点是,除了您实际使用的 database.properties 文件之外,它还会复制两个 database-*.properties 文件。如果这是一个问题,可以通过增加复杂性的代价来解决。

奖励答案:您可以使用system执行任意shell命令。例如,以子壳为代价,我可以将上述实现为:

resources.enhance do
  system("cp '#{_(:source, :main, :resources, "database-#{ENV['DATABASE']}.properties")}' '#{_(:target, :resources, "database.properties")}'")
end

如果您想将属性注入到配置文件中,您可能需要查看 Buildr 的 过滤机制。

The usual way to parameterize a buildr build is with environment variables. So for instance you might design your build to be executed like so:

$ buildr package DATABASE=postgres

In your buildfile you might then have:

ENV['DATABASE'] ||= 'hsql' # this line makes the default 'hsql'

define "foo" do
  # the rest of the definition

  resources.enhance do
    cp _(:source, :main, :resources, "database-#{ENV['DATABASE']}.properties"), _(:target, :resources, "database.properties")
  end
end

This is the simplest solution; one thing you might consider a drawback is that it will copy the two database-*.properties files in addition to the database.properties file that you'll actually use. If that's a problem it could be worked around at the cost of some more complexity.

Bonus answer: You can execute an arbitrary shell command using system. E.g., at the cost of subshelling I could have implemented the above as:

resources.enhance do
  system("cp '#{_(:source, :main, :resources, "database-#{ENV['DATABASE']}.properties")}' '#{_(:target, :resources, "database.properties")}'")
end

If you are wanting to inject properties into a configuration file, you might want to look at Buildr's filter mechanism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文