Configurating datasources with ant?

Stefan Koch

A glance at the source code of the install task of tomcat tells me that it cannot replace tokens from a template configuration file.

You should use the ant <replace/> task, or <copy/> with a nested <filterchain> containing a nested <filterset/> element.

Example with filterset :

Will work with a template file where the tokens to be replaced are introduced and ended by backquotes.

Tokens are recognized by replacetokens with one beginning and one ending character. So tokens cannot look like an ant property ${some.property}

  <copy (appropriate attributes here)>
    <filterchain>
       <replacetokens>
          <param type="tokenchar" name="begintoken" value="`"/>
          <param type="tokenchar" name="endtoken" value="`"/>
          <param type="token" name="dburl" value="${dburl}"/>
          <param type="token" name="driver" value="${driver}"/>
          <param type="token" name="username" value="${username}"/>
          <param type="token" name="password" value="${password}"/>
       </replacetokens>
    </filterchain>
   </copy>

Additional info:
I can delete, update and deploy applications to Tomcat using the catalina-ant.jar tasks, but can I also create/edit datasource?

I would like my ant script to have the dburl, driver, username and password as propeties and automatically create the datasource and underlying connection pool (using those settings).

For the install task you can specify a config file which contains the context definitions like in the server.xml

 <target name="install" description="Install web application" depends="createwar">
    <install url="${url}" username="${username}" password="${password}"
            path="${path}" war="file://${basedir}/${warfile}"
            config="file://${basedir}/conf/context.xml"/>
  </target>
conf/context.xml

<Context className="org.apache.catalina.core.StandardContext" 
         crossContext="false" reloadable="true"
         mapperClass="org.apache.catalina.core.StandardContextMapper"
         useNaming="true" debug="0" swallowOutput="false" 
         wrapperClass="org.apache.catalina.core.StandardWrapper"
         docBase="/opt/jakarta-tomcat-4.1.24/webapps/home" cookies="true"
         path="/home" cachingAllowed="true" privileged="false"
         charsetMapperClass="org.apache.catalina.util.CharsetMapper">
  <Logger className="org.apache.catalina.logger.FileLogger" debug="0"
          verbosity="1" prefix="homepage" directory="logs"
          timestamp="true" suffix=".log"/>
  <Resource name="jdbc/postgres" scope="Shareable" 
            type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/postgres">
    <parameter>
      <name>url</name>
      <value>jdbc:postgresql://127.0.0.1/webappdb</value>
    </parameter>
    <parameter>
      <name>password</name>
      <value>xxxx</value>
    </parameter>
    <parameter>
      <name>maxActive</name>
      <value>4</value>
    </parameter>
    <parameter>
      <name>maxWait</name>
      <value>5000</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>org.postgresql.Driver</value>
    </parameter>
    <parameter>
      <name>username</name>
      <value>xxx</value>
    </parameter>
    <parameter>
      <name>maxIdle</name>
      <value>2</value>
    </parameter>
  </ResourceParams>
</Context>

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact