本文介绍了使用 ant 处理自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含数据库访问凭据的属性文件(例如 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>

现在客户希望通过使用 .jar 文件中提供的加密库对 db.properties 中的密码进行加密,例如:

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 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 处理自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:56