Question

The idea is to have a self-containing project (well, at least for development) without any need to configure external tomcat - but the problem with using tomcat7-maven-plugin it that, correct me if I'm wrong, the embedded version of tomcat does not contain "manager" application - and I just don't want to rerun whole server each time.

So the question is, what is the best way to incorporate manager webapp to embedded tomcat?

Was it helpful?

Solution

I got the Manager working by doing as @olamy suggests in his solution. Since it was still quite some work to get it running and my solution somewhat differs from his and since information about this is rare on the internet, I am going to show how I did it in some detail.

First, this is the project structure of my WAR:

Image of WAR structure

I downloaded the manager.war file from the link @olamy provided and placed it inside WEB-INF under tomcat/manager. I left the WAR file there for convenience, but you can just delete it after extracting its content as shown in the screen shot above. You are actually free to place the folder anywhere you want, just make sure you update the document base attribute for it in your server.xml.

Inside the pom.xml I configured the tomcat7-maven-plugin like this:

<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
    <port>9090</port>
    <path>/webapp</path>
    <serverXml>./src/main/tomcat/conf/server.xml</serverXml>
    <contextFile>./src/main/tomcat/conf/context.xml</contextFile>
    <tomcatUsers>./src/main/tomcat/conf/tomcat-users.xml</tomcatUsers>
    <tomcatLoggingFile>./src/main/tomcat/conf/logging.properties</tomcatLoggingFile>
    <additionalConfigFilesDir>./src/main/tomcat/conf</additionalConfigFilesDir>
</configuration>

Notice that although additionalConfigFilesDir tells the plug-in where to look for the configuration file and makes sure the files are copied to the right folder before the server starts, I was not able to get this to work correctly without explicitly mentioning the separate configuration files in their corresponding tags. logging.properties and context.xml can be omitted, but server.xml and tomcat-users.xml have to be mentioned in their corresponding tags for the Manager to work.

So, here is the content of these two files:

server.xml

<?xml version="1.0" encoding="UTF-8"?>
<Server port="9090" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

    <GlobalNamingResources>
        <!-- Used by Manager -->
        <Resource name="UserDatabase" auth="Container"
                  type="org.apache.catalina.UserDatabase"
                  description="User database that can be updated and saved"
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                  pathname="conf/tomcat-users.xml" readonly="true"/>
    </GlobalNamingResources>

    <Service name="Catalina">
        <Connector port="9090" keepAliveTimeout="1800000" maxKeepAliveRequests="30000" maxThreads="300"/>
        <Engine name="Catalina" defaultHost="localhost">
            <Valve className="org.apache.catalina.valves.AccessLogValve" resolveHosts="false" buffered="false"
                   pattern="%t-ip:%a-protocol::%H-status:%s-localPort:%p-path:%U-time:%D ms"/>
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
            <Host name="localhost" appBase="webapps" autoDeploy="true" unpackWARs="true" deployXML="false">
                <Context path="/manager" docBase="../../<your WAR root folder name>/WEB-INF/tomcat/manager" privileged="true"/>
            </Host>
        </Engine>
    </Service>
</Server>

tomcat-users.xml

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users>
    <role rolename="manager"/>
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <user username="admin" password="password" roles="manager, manager-gui, manager-script"/>
</tomcat-users>

After starting Tomcat 7 with mvn tomcat7:run-war I can log into the Manager via the URL http://localhost:9090/manager and the credentials admin / password.

OTHER TIPS

You need to include manager.war you will find here: http://svn.apache.org/repos/asf/tomcat/maven-plugin/trunk/tomcat7-maven-plugin/src/test/manager.war

It's not actually distributed in the central repo (so feel free to have it in your Maven repository manager)

Then check the content of this server.xml to see how to enable it: http://svn.apache.org/repos/asf/tomcat/maven-plugin/trunk/tomcat7-maven-plugin/src/test/resources/deploy-war-project/src/main/tomcatconf/server.xml

The trick is to use privileged="true"

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top