Monthly Archives: November 2012

Using the Tomcat Maven Plugin to run your application

If you really want to be proficient using the Tomcat Maven Plugin you should go here and have a proper read of the plugin goals and usage. I take it for granted that your Maven knowledge is already enough to understand them. If it’s not, then close this tab and start here.

Ok, if you are still here, the quickest way to run your project into an embedded tomcat 7 instance is adding this into your pom’s plugin section:

<plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.0</version>
</plugin>

Now, just do…

mvn tomcat7:run

… in your preferred Maven installation (command-line, Eclipse plugin, Netbeans plugin…) and go to http://localhost:8080/${project.artifactId}/ to check that the project is running fine. Obviously you need replace ${project.artifactId} for your project’s Maven artifactId.

If you want to use a different value for the url, instead of the ${project.artifactId}, then you just have to add a new configuration section to the plugin:

<plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <path>/mycontext</path>
        </configuration>
</plugin>

The plugin uses automatically the last version of Tomcat 7 out there. If, for whatever reason, you want to use a specific version that does not match this one, you can do it, but you’ll have to specify it in the dependencies of the plugin. A bit tedious but easy:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
	  <dependencies>
             <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-util</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-coyote</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jdbc</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-dbcp</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-servlet-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jsp-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jasper</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jasper-el</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-el-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-catalina</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-tribes</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-catalina-ha</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-annotations-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	       </dependencies>
</plugin>

I took the trick to specify the Tomcat version from http://nurkiewicz.blogspot.co.uk/2012/11/standalone-web-application-with.html.

varchar or nvarchar

If you got here is because you had the same problem as me… you are probably trying to define the data type for a table column in your preferred database and you saw these two possibilities for variable length text columns. I will save you time, picking the best answer out there (IMHO) from Stack Overflow:

varchar: Variable-length, non-Unicode character data. The database collation determines which code page the data is stored using.

nvarchar: Variable-length Unicode character data. Dependent on the database collation for comparisons.

Armed with this knowledge, use whichever one matches your input data (ASCII v. Unicode).

There is plenty of information on the topic, but this (in my case) convinced me of using nvarchar.

Where to start with Lucene 4?

If you are new to Lucene, like me, and you are struggling to find documentation, or, better, you’re finding too much documentation… this is what I did to find out what this is about…

1.- Read this.
2.- Do this example. I have mavenized the project.
3.- Read this.
4.- Create your first project searching files from a local directory with the classic IndexFiles and SearchFiles classes from Lucene, as advised in the previous point reading.
5.- Create your first project searching text from a local database tables content. You may want to read this before.
6.- Address any possible performance issues.
7.- Put Luke in your Lucene-life. This tool will let you query in your indexes a very friendly manner, among other things.
8.- To present results almost real time in an impressive fashion, give this a read.

For a more technical FAQ.

As any other framework, though, as much as you work with it, more confident you will feel. I am just a beginner, so I suppose that this will be only useful for you if you’re starting too. Feel free to point out the next steps that I (or others to whom this post is useful) should be doing (resources, etc…) if you are an expert.

Thanks!