Skip to content

Running microservices in embedded Tomcat for testing

Andrew Post edited this page May 8, 2017 · 3 revisions

The following maven profile will setup embedded tomcat to run an Eureka! Clinical service on your own computer with embedded tomcat and an h2 database:

        <profile>
            <id>tomcat</id>
            <properties>
                <cas-mock-version>1.0</cas-mock-version>
                <tomcat.httpsPort>8443</tomcat.httpsPort>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-context.xml</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/tomcat-config</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-server-config</directory>
                                            <filtering>true</filtering>
                                            <includes>
                                                <include>context.xml</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                            <execution>
                                <id>copy-cert</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/tomcat-config</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-server-config</directory>
                                            <filtering>false</filtering>
                                            <includes>
                                                <include>localhost.keystore</include>
                                                <include>localhost.truststore</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                            <execution>
                                <id>copy-logging.properties-file</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/eureka-config</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-eureka-config</directory>
                                            <includes>
                                                <include>logging.properties</include>
                                            </includes>
                                            <filtering>true</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                            <execution>
                                <id>copy-application.properties-file</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/eureka-config/eureka</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-eureka-config/eureka</directory>
                                            <filtering>true</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <dependencies>
                            <dependency>
                                <groupId>org.jasig.cas.client</groupId>
                                <artifactId>cas-client-core</artifactId>
                                <version>${cas-client-version}</version>
                                <exclusions>
                                    <exclusion>
                                        <groupId>javax.servlet</groupId>
                                        <artifactId>servlet-api</artifactId>
                                    </exclusion>
                                </exclusions>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
                            <port>${tomcat.port}</port>
                            <httpsPort>${tomcat.httpsPort}</httpsPort>
                            <keystorePass>changeit</keystorePass>
                            <keystoreFile>conf/localhost.keystore</keystoreFile>
                            <keystoreType>JKS</keystoreType>
                            <additionalConfigFilesDir>${project.build.directory}/tomcat-config</additionalConfigFilesDir>
                            <systemProperties>
                                <org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH>true</org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH>
                                <javax.net.ssl.trustStore>${project.build.directory}/tomcat/conf/localhost.truststore</javax.net.ssl.trustStore>
                                <javax.net.ssl.trustStorePassword>changeit</javax.net.ssl.trustStorePassword>
                                <eureka.config.dir>${project.build.directory}/eureka-config/eureka</eureka.config.dir>
                                <java.util.logging.config.file>${project.build.directory}/eureka-config/logging.properties</java.util.logging.config.file>
                            </systemProperties>
                            <webapps>
                                <webapp>
                                    <groupId>org.eurekaclinical</groupId>
                                    <artifactId>cas-mock</artifactId>
                                    <version>${cas-mock-version}</version>
                                    <type>war</type>
                                    <asWebapp>true</asWebapp>
                                </webapp>
                            </webapps>
                            <addContextWarDependencies>true</addContextWarDependencies>
                            <addWarDependenciesInClassloader>true</addWarDependenciesInClassloader>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.liquibase</groupId>
                        <artifactId>liquibase-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>liquibase-populate-db</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>update</goal>
                                </goals>
                                <configuration>
                                    <changeLogFile>${project.build.directory}/classes/dbmigration/changelog-master.xml</changeLogFile>
                                    <url>jdbc:h2:file:${project.build.directory}/db/db</url>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

This profile assumes that the following directories and files are in your project's /src/main/resources directory:

  • tomcat-eureka-config
    • logging.properties: a java.util.properties style logging configuration file.
  • tomcat-eureka-config/eureka
    • application.properties: typically provides a value for the cas.url property such as https://localhost:8443/cas-mock
  • tomcat-server-config
    • context.xml: contains one or more Resource tags pointing to h2 databases for this project and any services on which this project depends.
    • localhost.keystore: a java keystore containing a self-signed certificate for localhost.
    • localhost.truststore: a java trust store containing the self-signed certificate above.

The following profile will do the same for an Eureka! Clinical webapp (replace references to eurekaclinical-user-agreement-service with the artifact id of the service of interest):

        <profile>
            <id>tomcat</id>
            <properties>
                <cas-mock-version>1.0</cas-mock-version>
                <tomcat.httpsPort>8443</tomcat.httpsPort>
                <eurekaclinical-user-agreement-service-version>2.0-Alpha-1</eurekaclinical-user-agreement-service-version>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-context.xml</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/tomcat-config</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-server-config</directory>
                                            <filtering>true</filtering>
                                            <includes>
                                                <include>context.xml</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                            <execution>
                                <id>copy-cert</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/tomcat-config</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-server-config</directory>
                                            <filtering>false</filtering>
                                            <includes>
                                                <include>localhost.keystore</include>
                                                <include>localhost.truststore</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                            <execution>
                                <id>copy-logging.properties-file</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/eureka-config</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-eureka-config</directory>
                                            <includes>
                                                <include>logging.properties</include>
                                            </includes>
                                            <filtering>true</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                            <execution>
                                <id>copy-application.properties-file</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/eureka-config/eureka</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${basedir}/src/main/resources/tomcat-eureka-config/eureka</directory>
                                            <filtering>true</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <dependencies>
                            <dependency>
                                <groupId>org.jasig.cas.client</groupId>
                                <artifactId>cas-client-core</artifactId>
                                <version>${cas-client-version}</version>
                                <exclusions>
                                    <exclusion>
                                        <groupId>javax.servlet</groupId>
                                        <artifactId>servlet-api</artifactId>
                                    </exclusion>
                                </exclusions>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
                            <port>${tomcat.port}</port>
                            <httpsPort>${tomcat.httpsPort}</httpsPort>
                            <keystorePass>changeit</keystorePass>
                            <keystoreFile>conf/localhost.keystore</keystoreFile>
                            <keystoreType>JKS</keystoreType>
                            <additionalConfigFilesDir>${project.build.directory}/tomcat-config</additionalConfigFilesDir>
                            <systemProperties>
                                <org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH>true</org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH>
                                <javax.net.ssl.trustStore>${project.build.directory}/tomcat/conf/localhost.truststore</javax.net.ssl.trustStore>
                                <javax.net.ssl.trustStorePassword>changeit</javax.net.ssl.trustStorePassword>
                                <eureka.config.dir>${project.build.directory}/eureka-config/eureka</eureka.config.dir>
                                <java.util.logging.config.file>${project.build.directory}/eureka-config/logging.properties</java.util.logging.config.file>
                            </systemProperties>
                            <webapps>
                                <webapp>
                                    <groupId>org.eurekaclinical</groupId>
                                    <artifactId>cas-mock</artifactId>
                                    <version>${cas-mock-version}</version>
                                    <type>war</type>
                                    <asWebapp>true</asWebapp>
                                </webapp>
                                <webapp>
                                    <groupId>org.eurekaclinical</groupId>
                                    <artifactId>eurekaclinical-user-agreement-service</artifactId>
                                    <version>${eurekaclinical-user-agreement-service-version}</version>
                                    <type>war</type>
                                    <asWebapp>true</asWebapp>
                                </webapp>
                            </webapps>
                            <addContextWarDependencies>true</addContextWarDependencies>
                            <addWarDependenciesInClassloader>true</addWarDependenciesInClassloader>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-sql-from-eurekaclinical-user-agreement-service</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>org.eurekaclinical</groupId>
                                            <artifactId>eurekaclinical-user-agreement-service</artifactId>
                                            <version>${eurekaclinical-user-agreement-service-version}</version>
                                            <type>war</type>
                                        </artifactItem>
                                    </artifactItems>
                                    <includes>WEB-INF/classes/dbmigration/changelog-master.xml</includes>
                                    <outputDirectory>${project.build.directory}/eureka-config</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.liquibase</groupId>
                        <artifactId>liquibase-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>liquibase-populate-db</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>update</goal>
                                </goals>
                                <configuration>
                                    <changeLogFile>${project.build.directory}/eureka-config/WEB-INF/classes/dbmigration/changelog-master.xml</changeLogFile>
                                    <url>jdbc:h2:file:${project.build.directory}/db/db</url>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

Notice how it copies the corresponding service's changelog-master.xml file so that an h2 database can be created.

Clone this wiki locally