1

I have a Java webapp for tomcat 7 that uses an H2 database for the data as well as for the user authentication. When I deploy the webapp on a standalone Tomcat everything works fine.

Now I want to run integration tests with maven. I use the tomcat7-maven-plugin version 2.1 When I run the integration tests the tomcat (embedded) it uses the tomcat-users.xml only. I have put my server.xml into src/main/tomcatconf and it will be copied correctly into the target/tomcat/conf.

my realm definition in the server.xml

<Realm className="org.apache.catalina.realm.JDBCRealm"
        driverName="org.h2.Driver"
        connectionURL="jdbc:h2:DB/db;USER=user;PASSWORD=password;AUTO_SERVER=TRUE;"
        userTable="userAuth" userNameCol="username" userCredCol="userpass"
        userRoleTable="userAuth" roleNameCol="groupname" digest="SHA-512"/>

my pom.xml

     <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <argLine>${failsafe.argLine}</argLine>
                <fork>true</fork>
                <path>/</path>

                <contextFile>${project.build.directory}/ROOT/META-INF/context.xml</contextFile>

                <systemProperties>
                    <JAVA_OPTS>-Xms128m -Xmx256m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled</JAVA_OPTS>

                </systemProperties>

            </configuration>
            <executions>
                <execution>
                    <id>tomcat-run</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run-war-only</goal>
                    </goals>
                    <configuration>

                        <path>/</path>
                        <serverXml>src/main/tomcatconf/server.xml</serverXml>

                        <port>8080</port>
                    </configuration>
                </execution>

                <execution>
                    <id>tomcat-shutdown</id>

                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>shutdown</goal>
                    </goals>

                </execution>


            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <version>1.3.173</version>
                </dependency>
            </dependencies>



        </plugin>

I tried different approaches I've on the Internet (and also on the Apache site) but nothing worked. When I add a tomcat-users.xml that contains my test users the tests work.

UPDATE

here the log

Running war on http://localhost:8080/
Creating Tomcat server configuration at D:\project\target\tomcat
Coping additional tomcat config files
 copy server.xml
 copy _tomcat-users.xml
setting SystemProperties:
 JAVA_OPTS=-Xms128m -Xmx256m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled
create webapp with contextPath: 
Feb 10, 2014 2:44:40 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 10, 2014 2:44:40 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Feb 10, 2014 2:44:40 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Feb 10, 2014 2:44:42 PM org.jboss.weld.bootstrap.WeldBootstrap <clinit>
INFO: WELD-000900 2.0.3 (Final)
Feb 10, 2014 2:44:42 PM org.jboss.weld.bootstrap.WeldBootstrap startContainer
INFO: WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Feb 10, 2014 2:44:43 PM org.jboss.weld.environment.tomcat7.Tomcat7Container initialize
INFO: Tomcat 7 detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported
Feb 10, 2014 2:44:43 PM org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
WARNING: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
Feb 10, 2014 2:44:43 PM org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
WARNING: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
Feb 10, 2014 2:44:43 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]

--- jacoco-maven-plugin:0.6.3.201306030806:prepare-agent (pre-integration-test) @ ILS_webapp 

--- exec-maven-plugin:1.2.1:exec (gen-mvnopts-bat) @ ILS_webapp ---
--- maven-resources-plugin:2.6:copy-resources (copy-resources) @ ILS_webapp ---
Using 'UTF-8' encoding to copy filtered resources.
Copying 6 resources

I renamed tomcat-users.xml th _tomcat-users.xml to avoid that this file is used as realm. The webapps deploys well. Only the authentication doesn't work.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <listener>
        <listener-class>
            org.jboss.weld.environment.servlet.Listener
        </listener-class>
    </listener>
    <resource-env-ref>
        <resource-env-ref-name>BeanManager</resource-env-ref-name>
        <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
    </resource-env-ref>
    <security-constraint>
        <web-resource-collection>
            <url-pattern>/error/*</url-pattern>
<!--            <url-pattern>/css/*</url-pattern>-->
            <url-pattern>/js/*</url-pattern>
            <url-pattern>/img/*</url-pattern>
            <url-pattern>/api/*</url-pattern>
            <url-pattern>/resources/*</url-pattern>
            <url-pattern>/favicon.ico</url-pattern>
        </web-resource-collection>
    </security-constraint>
    <security-constraint>
        <display-name>secure</display-name>
        <web-resource-collection>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>ADMIN</role-name>
            <role-name>USER</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>myRealm</realm-name>
        <form-login-config>
            <form-login-page>/index.html</form-login-page>
            <form-error-page>/index_failed.html</form-error-page>
        </form-login-config>
    </login-config>


    <security-role>
        <role-name>ADMIN</role-name>
    </security-role>
    <security-role>
        <role-name>USER</role-name>
    </security-role>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
  • is there any error comes in your console.. – Kanhu Feb 10 '14 at 13:28
  • probably you miss some in your web.xml file , check this link http://stackoverflow.com/questions/15818038/how-to-configure-tomcat-users-xml-to-secure-a-page-in-tomcat?answertab=active#tab-top – Kanhu Feb 10 '14 at 13:57
  • I added my web.xml. As I mentioned. The webapp works well on a standalone system. – Gerald Ortner Feb 10 '14 at 14:09
  • 1
    I got it working! The answer of Krishna K in http://stackoverflow.com/questions/13604377/tomcat7-maven-plugin-custom-server-xml solved my problem. I put my Realm in a context.xml and added the context.xml to the tomcat-maven-plugin configuration. – Gerald Ortner Mar 19 '14 at 07:56

0 Answers0