问题描述
我们有一个属性文件(例如 db.properties
),其中包含数据库访问的凭据。示例:
We have a properties file (e.g. db.properties
) that contains the credentials for a database access. Example:
db.jdbc.user=johndoe
db.jdbc.password=topsecret
我们有许多ant脚本可以读取此文件并执行各种任务。示例:
We have many ant scripts that read this file and execute various tasks. Example:
<!--Initialize the environment-->
<target name="environment">
<!--Read database connection properties-->
<property file="$../db.properties"/>
...
</target>
<target name="dbping"
description="Test the database connectivity with the current settings."
depends="environment">
...
<sql driver="oracle.jdbc.OracleDriver"
url="${db.jdbc.url}"
userid="${db.jdbc.user}"
password="${db.jdbc.password}"
classpathref="classpath.jdbc"
print="true"
showheaders="false">
SELECT 'JDBC connect: successful!' FROM dual;
</sql>
...
</target>
现在,客户端希望使用其中提供的加密库来加密db.properties中的密码一个.jar文件,例如:
Now the client wants that the password in the db.properties is encrypted by using their encryption lib provided within a .jar file, e.g.:
db.jdbc.user=johndoe
db.jdbc.password.encrypted=true
db.jdbc.password=018Dal0AdnE=|ySHZl0FsnYOvM+114Q1hNA==
我们是什么希望是以最少量的ant文件修改来实现解密。我听说过 Ant 1.8
中的增强属性处理,但我们使用 Ant 1.7.1
。
What we want to is to achieve the decryption with minimum modifications of the tons of ant files. I've heard about the enhanced property handling in Ant 1.8
, but we use Ant 1.7.1
.
对此最佳解决方案是什么 - 自定义任务, PropertyHelper
实例的一些神奇之处还有什么?
What is the best solution for this - custom task, some magic with the PropertyHelper
instance, something else?
提前感谢您的提示。
推荐答案
我更喜欢的解决方案是用我自己的自定义任务处理问题。这需要最小的变化。在我们的ant脚本中,此任务如下所示:
The solution that I preferred is to handle the problem with my own custom task. This required minimum changes. In our ant script this task looks like this:
<!--Initialize the environment-->
<target name="environment">
<!--Read database connection properties-->
<property file="$../db.properties"/>
...
<decryptpwd passwordProperty="db.jdbc.password"/>
</target>
这项任务也很简单。它看起来像这样:
The task is also trivial. It looks like this:
public class DecryptPassword extends Task
{
@Override
public void execute()
{
...
PropertyHelper.getPropertyHelper(getProject()).setProperty(null, passwordProperty, getDecryptedPassword(),
false);
...
}
}
然后 - 它似乎工作; - )
And yeap - it seems to work ;-)
这篇关于使用ant定制属性处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!