【问题标题】:Custom property handling with ant使用 ant 处理自定义属性
【发布时间】:2013-05-28 12:02:13
【问题描述】:

我们有一个包含数据库访问凭据的属性文件(例如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 实例的一些魔法,还有什么?

提前感谢您的提示。

【问题讨论】:

    标签: java ant


    【解决方案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(可能很危险)

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

    【讨论】:

    • 谢谢!所有这些下标都是自给自足的单位,仅依赖于设置一些环境变量的目标。他们测试与数据库的连接、导入、导出等。我目前更喜欢我自己的自定义任务,我将把它放入环境目标中。此任务将解密密码,并使用 PropertyHelper 为其他目标提供解密版本。
    【解决方案2】:

    我更喜欢的解决方案是使用我自己的自定义任务来处理问题。这需要最少的更改。在我们的 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);
            ...                 
        }
    }
    

    是的 - 它似乎工作;-)

    【讨论】:

    猜你喜欢
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多