【问题标题】:Ant -propertyfile resolve from cmdAnt -propertyfile 从 cmd 解析
【发布时间】:2013-08-22 04:22:43
【问题描述】:

我正在尝试为项目创建属性文件。 该项目可以使用不同的数据库(Oracle 或 Mssql,但不能同时使用) 因为我制作了 3 个属性文件:

common.properties
mssql.properties
oracle.properties

我想使用 ant 属性层次结构功能来设置其中的一些属性。 例如,我可以定义 at,common.properties :

db.hostname= localhost
db.port= 1433

然后在 mssql\oracle.proprties 文件上我可以构建

db.connectionString= jdbc:sqlserver://${db.hostname}:${db.port}

在我的 build.xml 上 我写过:

<property file="common.properties"/>    

为了设置我在 CMD 上写的具体数据库:

Ant-1.8.4\bin\ant   -propertyfile mssql.properties

问题是 ant 不使用我在 common.properties 中定义的引用 为了解决:

db.connectionString 

如何使用 cmd 解决这个问题?

【问题讨论】:

  • 能看到build.xml文件吗?

标签: ant


【解决方案1】:

问题在于创建属性的顺序。在执行 ANT 脚本之前,首先加载文件“mssql.properties”。这就解释了为什么属性“db.connectionString”被分配了字符串“${db.hostname}”和“${db.port}”,因为这些属性没有值。它们的值在脚本运行并加载第二个属性文件时设置。

另一种方法是使用属性来指示数据库类型。

示例

├── build.xml
├── common.properties
└── mssql.properties

如下运行

$ ant -Ddb=mssql
Buildfile: /home/mark/tmp/build.xml

echo:
     [echo] db.connectionString=jdbc:sqlserver://localhost:1433

build.xml

<project name="demo" default="echo">

   <property file="common.properties"/>
   <property file="${db}.properties"/>

   <target name="echo">
      <echo message="db.connectionString=${db.connectionString}"/>
   </target>

</project>

额外学分

如果未指定正确的数据库类型,此方法还可以启用错误检查:

<project name="demo" default="echo">

   <property file="common.properties"/>
   <property file="${db}.properties"/>

   <available property="db.prop.file" file="${db.properties}"/>

   <target name="echo">
      <fail message="Missing a property file for a ${db} database" unless="db.prop.file"/>

      <echo message="db.connectionString=${db.connectionString}"/>
   </target>

</project>

【讨论】:

  • 感谢您的宝贵时间。无论如何,我确实知道这种方法...,但我想知道为什么引用没有解决。
猜你喜欢
  • 2011-07-04
  • 2011-03-27
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
相关资源
最近更新 更多