2013-05-28 41 views
2

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

现在客户想要的是,在db.properties密码是通过使用一个.jar文件中提供了加密的lib,如加密:

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

我们希望是什么通过对大量蚂蚁文件进行最小限度的修改来实现解密。我听说Ant 1.8的房地产处理增强,但我们使用Ant 1.7.1

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

在此先感谢您的提示。

回答

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); 
     ...     
    } 
} 

而且叶氏 - 它似乎工作;-)

+0

请参阅此答案类似的方法:http://stackoverflow.com/questions/7503630/where-do-i-put-my-credentials-when-using-ivy-and-a-private-company-repository/7505364# 7505364 –

1

我认为你想采取的方法是你可以在ant中做的包装方法。

父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具有潜在危险

你想要做的是每个标中拖放到这个父生成文件,并通过周围将每个脚本的未加密字符串作为参数/从属性读入。

+0

谢谢!所有这些下标都是自给自足的单位,它们只依赖设定一些环境变量的目标。他们测试连接到数据库,导入,导出等。我现在更喜欢我自己的定制任务,我将其放入环境目标。此任务将解密密码,并使用PropertyHelper为其他目标提供解密版本。 –