日期:2014-05-16  浏览次数:20419 次

如何在ant中调用javascript
在工作中, 我遇到了这样一个应用场景. 调用ant时传的属性参数中有连接数据库的密码, 在log中会显示出来, 如果将密码加密, 那么又会报数据库端的验证错误, 说不认这个经过加密的密码. 怎么办, 有两种方法, 一个是用java写自定义的ant task来解密传给ant script的密码. 然后用明文密码来连接数据库. 写java的缺点在于每次都要事先编译好这个ant task. 还有一种方法是在ant script写一个javascript task. 当然javascript可以调用java的解密函数, 这样不用每次运行ant script前都编译task. 具体脚本如下:

<project name="sfamx.cleanupdb" default="cleanup.db.logging">

    <dirname property="sfamx.ddl.dir" file="${ant.file.sfamx.ddl.build}"/>

    <target name="cleanupall" depends="replace.db.password, cleanup.db.logging"/>

    <path id="jdbc.classpath">
        <fileset dir="${db.driver.path}" includes="*.jar"/>
    </path>
    
    <property name="plaintext.password" value="${plainText}"/>

    <target name="replace.db.password">
    <script language="javascript">
    <classpath>
            <!-- for xxx 3.2 -->
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.security.tibcrypt_2.15.0.009.jar"/>
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.tpcl.org.slf4j.log4j12_1.6.200.001/slf4j-log4j12-1.5.2.jar"/>
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.tpcl.org.slf4j.log4j12_1.6.200.001/slf4j-api-1.5.2.jar"/>
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.tpcl.org.apache.log4j_1.2.400.002/log4j-1.2.15.jar"/>
            <!-- for xxx 3.1.5 -->
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.security.tibcrypt_2.14.300.006.jar"/>
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.tpcl.org.slf4j.log4j12_1.5.101.001/slf4j-log4j12-1.5.2.jar"/>
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.tpcl.org.slf4j.log4j12_1.5.101.001/slf4j-api-1.5.2.jar"/>
            <pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.tpcl.org.apache.log4j_1.2.300.004/log4j-1.2.15.jar"/>
    </classpath>
    <![CDATA[
        importPackage(com.tibco.security);
        var encryptedPassword = project.getProperty("admin.db.password");
        var decryptedPassword = ObfuscationEngine.decrypt(encryptedPassword);
        var plainText = new java.lang.String(decryptedPassword);
        project.setProperty("plaintext.password", plainText)
    ]]>
    </script>
    </target>

    <target name="cleanup.db.logging" depends="replace.db.password" 
            description="Run ddl to cleanup commonlogging tables in external database for amx administrator">
       <sql
            driver="${admin.db.driver}"
            url="${admin.db.url}"
            userid="${admin.db.username}"
            password="${plaintext.password}"
            print="yes">
           <classpath refid="jdbc.classpath"/>
           <transaction  src="./${db.ddl.path}/drop_commonlogging.ddl"/>
       </sql>
    </target>
</project>


输出

05/25/12 18:46:55.406 INFO: [ProcessWrapper] Executing: D:\build\fabric\DSEngine\resources\win32\wrappid.exe,D:\build\fabric\DSEngine\work\yangxiaoming-lt-1\antcall.pid,D:/build/fabric/DSEngine/work/yangxiaoming-lt-1/tibcoamx/tibcojre/1.6.0/bin/java,-cp,D:/build/fabric/DSEngine/work/yangxiaoming-lt-1/tibcoamx/tools/lib/antpackage.jar,org.apache.tools.ant.launch.Launcher,-f,D:\build\fabric\DSEngine\work\yangxiaoming-lt-1\scripts\cleanupDDL\cleanupDB.xml,-Dadmin.db.password=#!Panbl6s1FhkBUM70G9T+okR+fv5Nwoq+fYUx+SLzZi8=,-Dadmin.db.url=jdbc:sqlserver://192.168.71.99:1433;databasename=amxdb,-Dadmin.db.username=sa,-Ddb.ddl.path=mssql300,-Ddb.driver.path=D:\build\fabric\DSEngine\work\yangxiaoming-lt-1/fabric/mssql2008_driver,-Dtibco.home=D:/build/fabric/DSEngine/work/ya