4

我们有一个db.properties包含数据库访问凭据的属性文件(例如 )。例子:

db.jdbc.user=johndoe 
db.jdbc.password=topsecret

我们有许多 ant 脚本来读取这个文件并执行各种任务。例子:

<!--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 中的密码进行加密,例如:

db.jdbc.user=johndoe
db.jdbc.password.encrypted=true
db.jdbc.password=018Dal0AdnE=|ySHZl0FsnYOvM+114Q1hNA==

我们想要的是通过对大量 ant 文件的最小修改来实现解密。我听说过增强的属性处理Ant 1.8,但我们使用Ant 1.7.1.

什么是最好的解决方案 - 自定义任务,PropertyHelper实例的一些魔力,还有什么?

提前感谢您的提示。

4

2 回答 2

1

我认为您要采用的方法是您可以在 ant 中执行的包装器方法。

父蚂蚁脚本:

<target name="decrypt">    
  <exec executable="myJar">
    <arg value="encryptedString"/>
  </exec>
</target>  
    <target name="build-foo">
        <subant target="build">
          <fileset dir="${test.home}" includes="Foobuild.xml"/>
        </subant>
    </target>

    <target name="build-bar">
        <subant target="build">
          <fileset dir="${test.home}" includes="Barbuild.xml"/>
        </subant>
    </target>

使用subant
exec潜在危险

您要做的是将每个下标放入此父构建文件中,并将未加密的字符串作为参数传递给每个脚本/从属性中读取。

于 2013-05-28T12:11:36.853 回答
1

我更喜欢的解决方案是用我自己的自定义任务来处理问题。这需要最少的更改。在我们的 ant 脚本中,这个任务看起来像这样:

<!--Initialize the environment-->
<target name="environment">

    <!--Read database connection properties-->
    <property file="$../db.properties"/>
    ...

    <decryptpwd passwordProperty="db.jdbc.password"/>

</target>

任务也是微不足道的。它看起来像这样:

public class DecryptPassword extends Task
{
    @Override
    public void execute()
    {
        ...
        PropertyHelper.getPropertyHelper(getProject()).setProperty(null, passwordProperty, getDecryptedPassword(),
                            false);
        ...                 
    }
}

是的 - 它似乎工作;-)

于 2013-05-30T10:03:26.643 回答