如何使用 ant 脚本写入 xml 文件?

How to write into xml file using ant script?

我是 ant 脚本的新手。我不知道,如何使用 ant 脚本写入 xml 文件。

我想将数据库信息写入Tomcat context.xml 文件。 我已经编写了 ant 脚本来从用户那里读取数据库信息。 我已将所有必要信息存储到这些属性中

{jdbc.myDatabase.driver}
{jdbc.myDatabase.url}
{jdbc.myDatabase.user}
{jdbc.myDatabase.password}

现在,如何将以上信息写入context.xml文件。 Context.xml 文件看起来像这样。

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
</Context>

写入数据库信息后,它应该如下所示。

新文件 context.xml 应该如下所示。

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <Resource name="jdbc/monitords"
              auth="Container"
              type="javax.sql.DataSource"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@192.168.15.194:1521:orcl"
              username="tomcat_dbrds"
              password="cmxinfa"
              jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
            />  
</Context>

如果有任何有用的建议,我将不胜感激link。请帮忙。

更新 1: 添加了 bild.xml 文件

<project name="TestWebApp" default="collect-user-inputs" basedir="." xmlns:contrib="antlib:net.sf.antcontrib">
 <!-- set global properties for this build -->
 <property name="resourcedir" location="resource"/>
 <property name ="tomcatServer" location ="apache-tomcat-server"/>
 <property name ="context_xml" location ="${tomcatServer}/conf"/>
 <property name="build" location="build"/>
 <property name="temp" location="_tmp"/>
 <property name="lib" location ="lib"/>
 <property name="installdir" location="install"/>
 <property name="outputdir" location="output"/>
 <property name="build.props" value="build.properties"/>
 <property name="ui.log4j.props" value="log4j.properties"/>
 <property name="dependencyfinder.home" value="."/>
 <property name="log.path" value=""/>
 

 
 <!-- Collects all the required inputs from the User -->
 <target name="collect-user-inputs"  unless="${build.props.exists}">
  <property name="jdbc.WebApp.driver" value="oracle.jdbc.driver.OracleDriver"/>
      <input message="Enter hostname/IP for   TestWebApp database" addproperty="jdbc.WebApp.url.host" defaultvalue="localhost"/>
      <input message="Enter the port number for database server where   TestWebApp schema was created" addproperty="jdbc.WebApp.url.port" defaultvalue="1521"/>
    <input message="Select Connection type for   TestWebApp schema (1) SID, (2) Service Name" addproperty="jdbc.WebApp.conn.type" defaultvalue="SID" validargs="1,2"/>
    <contrib:if>
   <contrib:equals arg1="${jdbc.WebApp.conn.type}" arg2="1"/>
   <contrib:then>
    <property name="jdbc.WebApp.conn.type.name" value="SID"/>
   </contrib:then>   
   <contrib:elseif>
    <contrib:equals arg1="${jdbc.WebApp.conn.type}" arg2="2"/>
     <contrib:then>
      <property name="jdbc.WebApp.conn.type.name" value="Service Name"/>
     </contrib:then>
   </contrib:elseif>   
  </contrib:if>
    
      <input message="Enter ${jdbc.WebApp.conn.type.name} for database server where   TestWebApp schema was created" addproperty="jdbc.WebApp.url.sid" defaultvalue="orcl"/>
         
    <contrib:if>
   <contrib:equals arg1="${jdbc.WebApp.conn.type}" arg2="1"/>
   <contrib:then>
    <contrib:var name="jdbc.WebApp.url" value="jdbc:oracle:thin:@${jdbc.WebApp.url.host}:${jdbc.WebApp.url.port}:${jdbc.WebApp.url.sid}"/>
    <echo message="WebApp jdbc.WebApp.url : ${jdbc.WebApp.url}" />
   </contrib:then>   
   <contrib:elseif>
    <contrib:equals arg1="${jdbc.WebApp.conn.type}" arg2="2"/>
     <contrib:then>
      <contrib:var name="jdbc.WebApp.url" value="jdbc:oracle:thin:@${jdbc.WebApp.url.host}:${jdbc.WebApp.url.port}/${jdbc.WebApp.url.sid}"/>
      <echo message="WebApp jdbc.WebApp.url : ${jdbc.WebApp.url}" />
     </contrib:then>
   </contrib:elseif>   
  </contrib:if>   

  <input message="Enter  TestWebApp database username" addproperty="jdbc.WebApp.user" defaultvalue=" _Monitor"/>
  <input message="Enter password for ${jdbc.WebApp.user}${line.separator}" addproperty="jdbc.WebApp.password">
   <handler classname="org.apache.tools.ant.input.SecureInputHandler"/>  
  </input>

  <echo message="Getting ORS name from ${jdbc.WebApp.user}@${jdbc.WebApp.url.host}:${jdbc.WebApp.url.port}/${jdbc.WebApp.url.sid}..."/>
  <tempfile property="temp.properties" prefix="temp_" suffix=".properties"/>

  <property file="${temp.properties}"/>
  <delete file="${temp.properties}"/>
  <condition property="schema_user_set" else="false">
   <isset property="${jdbc.hub.user}"/>
  </condition>
  
  
   <condition property="isORSSchemaSet">
            <not>
                <equals arg1="${jdbc.hub.user}" arg2="" trim="true"/>
            </not>
        </condition>
  <antcall target="failed_getting_ors_schema"/> 
  
  
  <property name="jdbc.hub.driver" value="oracle.jdbc.driver.OracleDriver"/>
      <input message="Enter hostname/IP ${jdbc.hub.user}" addproperty="jdbc.hub.url.host" defaultvalue="localhost"/>
      <input message="Enter the port number of Database server  ${jdbc.hub.user} is created " addproperty="jdbc.hub.url.port" defaultvalue="1521"/>
   <input message="Select Connection type for Database (1) SID (2) Service Name" addproperty="jdbc.hub.conn.type" defaultvalue="SID" validargs="1,2"/>
   <contrib:if>
   <contrib:equals arg1="${jdbc.hub.conn.type}" arg2="1"/>
   <contrib:then>
    <property name="jdbc.hub.conn.type.name" value="SID"/>
   </contrib:then>   
   <contrib:elseif>
    <contrib:equals arg1="${jdbc.hub.conn.type}" arg2="2"/>
     <contrib:then>
      <property name="jdbc.hub.conn.type.name" value="Service Name"/>
     </contrib:then>
   </contrib:elseif>   
  </contrib:if>
      <input message="Enter the ${jdbc.hub.conn.type.name} of the database where ORS ${jdbc.hub.user} is created" addproperty="jdbc.hub.url.sid" defaultvalue="orcl"/>
   
   <contrib:if>
   <contrib:equals arg1="${jdbc.hub.conn.type}" arg2="1"/>
   <contrib:then>
    <contrib:var name="jdbc.hub.url" value="jdbc:oracle:thin:@${jdbc.hub.url.host}:${jdbc.hub.url.port}:${jdbc.hub.url.sid}"/>
   </contrib:then>   
   <contrib:elseif>
    <contrib:equals arg1="${jdbc.hub.conn.type}" arg2="2"/>
     <contrib:then>
      <contrib:var name="jdbc.hub.url" value="jdbc:oracle:thin:@${jdbc.hub.url.host}:${jdbc.hub.url.port}/${jdbc.hub.url.sid}"/>
     </contrib:then>
   </contrib:elseif>   
  </contrib:if>      
  <!--<input message="Enter password for ${jdbc.hub.user}${line.separator}" addproperty="jdbc.hub.password">
   <handler classname="org.apache.tools.ant.input.SecureInputHandler"/>  
  </input> -->
  
  <property name="jdbc.hub.url" value="jdbc:oracle:thin:@${jdbc.hub.url.host}:${jdbc.hub.url.port}/${jdbc.hub.url.sid}"/>
  <input message="Enter License Key" addproperty="WebApp.license"/>

  
  <antcall target="tomcat-server-xml"/>
  
 </target>
 

  <target name="tomcat-server-xml">
 
  <echo message ="tomcat-server-xml is called"/> 
  <xmltask source="${context_xml}/context.xml" dest="${context_xml}/context.xml">
   <replace path="//*[local-name()='url']/text()" withText="${jdbc.WebApp.url}"/>
   <replace path="//*[local-name()='username']/text()" withText="${jdbc.WebApp.user}"/>
   <replace path="//*[local-name()='password']/text()" withText="${jdbc.WebApp.password}"/>
  </xmltask>
 </target>
 
 
 
 
</project>

为您的 context.xml 创建一个模板:

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <Resource name="jdbc/monitords"
              auth="Container"
              type="javax.sql.DataSource"
                  factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              driverClassName="${jdbc.myDatabase.driver}"
              url="${jdbc.myDatabase.url}"
              username="${jdbc.myDatabase.user}"
              password="${jdbc.myDatabase.password}"
              jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionSta    te;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apach    e.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
            />  
</Context>

然后使用下面的目标生成一个真实的context.xml,基于它:

<target name="generate.context">

    <!-- Properties (might be defined elsewhere) -->
    <property name="jdbc.myDatabase.driver" value="oracle.jdbc.OracleDriver"/>
    <property name="jdbc.myDatabase.url" value="jdbc:oracle:thin:@192.168.15.194:1521:orcl"/>
    <property name="jdbc.myDatabase.user" value="tomcat_dbrds"/>
    <property name="jdbc.myDatabase.password" value="cmxinfa"/>

     <!-- Copy context.xml.template to context.xml, expanding the properties in the process -->
    <copy file="context.xml.template" tofile="context.xml">
        <filterchain>
            <filterreader classname="org.apache.tools.ant.filters.ExpandProperties"/>
        </filterchain>
    </copy>

</target>