Launch tomcat from Maven with context.xml and injected password

1k Views Asked by At

I'm trying to set up a Maven pom.xml file such that it will

  • build my war file
  • launch tomcat with a context.xml that has a database resource
  • set the username and password in that context.xml from settings.xml or a properties file

I need the username and password to be external from the project so that the sensitive info isn't stored in version control.

The Maven tomcat plugin is working fine if I hard code the username and password. I put the context.xml fil in src/test/resources/tomcat/context.xml and configured the plugin to pull it from there:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration> 
        <url>http://localhost:8080/manager/text</url>
        <contextFile>
        ${project.basedir}/src/test/resources/tomcat/context.xml
        </contextFile>
    </configuration>
</plugin>

I've seen examples of putting the username and password in the .m2/settings.xml file as follows:

  <servers>
    <server>
      <id>demo</id>
      <username>myUser</username>
      <password>myPassword</password>
    </server>
  <servers>

But I don't know how to "inject" those values into the context.xml. I've putting the following in the appropriate points in my context.xml:

${servers.server.demo.username}

or

${servers.server.username}

but they're not resolving to the actual value.

What's the best practice for this kind of thing?

1

There are 1 best solutions below

2
svujic On

You need to add 'server' tag inside 'configuration' and specify its id as value. In your case this is a 'demo'.

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration> 
        <url>http://localhost:8080/manager/text</url>
        <contextFile>
        ${project.basedir}/src/test/resources/tomcat/context.xml
        </contextFile>
        <server>demo</server>
    </configuration>
</plugin>