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

时间:2016-10-28 06:50:26

标签: xml 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>

写完db信息之后,它应该是这样的。

新文件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>

我会感谢任何建议,有用的链接。请帮忙。

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

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

为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>