Dealing with Tomcat

I was modifying a project in Java with maven and Spring. In some point the need of having the application context XML outside Tomcat came out. In my case it was being loaded using the ContextLoaderListener. I tried other ways of loading it, but the issue is the same: the classpath.

pom.xml

<build>
    <resources>
        <resource>
            <directory>path/to/application/context/</directory>
            <includes>
                <include>*</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webXml>path/to/resource/web.xml</webXml>
            </configuration>
        </plugin>
    </plugins>
</build>

I wanted to change path/to/application/context.

Tomcat doesn’t give the access to any file outside where it is running, so all the classpath have to behave this way. It is done this way for security reasons. After struggling a hard time, trying different things and almost give up, I decided to read how Tomcat works. I found this page: http://www.mulesoft.com/tomcat-classpath. It have the answer to my question: add the path to the application context XML in the catalina.properties under the shared loader.

shared.loader=path/to/application/context

Hope this is useful for someone !

Advertisements

Setting up Tomcat 7 with IntelliJ IDEA

I have been doing a java project with IntelliJ IDEA. It is a quite normal IDE, with some supposed advantages over Eclipse. I got used to it in two weeks. 

So i’ll to explain how to run a project with Tomcat 7.

  1. Download the core version of Tomcat 7 here. DO NOT INSTALL IT WITH APT-GET OR ANY PACKAGE MANAGER
  2. Uncompress the folder anywhere you want
  3. Click on Run -> Edit Configurations…
    Search on the left for Tomcat Server and select Local

  4. Click Configure

  5. In the name put Tomcat 7
    Tomcat Home and Tomcat base directory are the folder you created in step 2

  6. Click Ok
    Set Startup page: http://localhost:8081/, HTTP port: 8081
    You can change this values to any you want

  7. Click on Run -> Edit Configurations…
    Click on the + sign 

  8. Select Tomcat Server Local
    Go to Deployment tab

  9. Click on the + sign, select artifact you want to add to the server (in the picture i added one called dsp-api.war)
    Click Ok

  10. That’s all run and test your application 🙂