Category Archives: Maven

Skipping tests when doing “mvn install”

First things first: you should NOT be skipping tests when building your projects. 9 out of 10 cases, you shouldn’t. But when you must / have to / want to do it: after spiking what it looked like a quick solution to a little problem and it turned out to be a massive refactoring during which you broke so many tests it would not be worthy to fix them unless the original problem is really flushed away… possibly the most typical scenario where you would want to do something like this. Well, in that case, you can do:

mvn -DskipTests=true install

or the more lazy version:

mvn -DskipTests install

Or you can do:

mvn -Dmaven.skip.test=true install

or the more lazy version:

mvn -Dmaven.skip.test install

Difference between the first and the second is that the first one is less horrible. Let me explain… both versions will not run any of your tests classes, but at least the first one won’t succeed if they don’t compile. So, if you’ve broken your tests but at least you changed your source code well enough to maintain compilation errors away on your test classes, you can get away with the first command. But if you incurred in compilation errors on your test classes while changing your code, you may want to go for the second command, which not only it will ignore your tests but it won’t even attempt to compile the test classes.

If having to run the first command is already a sign that you’ve not been doing the important things (TDD, for instance) right, running the second means you can’t even use your IDE correctly (refactoring, anyone?).

[Learnt at work, from a colleague]

[Reference]

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.

How to skip Maven unit test

By default, when you build your project with Maven, it will run the entire unit tests of your project.

$ mvn install

Or:

$ mvn package

However, if any of unit test is failed, it will force Maven to abort the building process. In real life, you may still need to build your project even unit test is failed.

To skip it, use -Dmaven.test.skip=true option to tell Maven that you want to skip running the entire unit test. For example,

$ mvn install -Dmaven.test.skip=true
$ mvn package -Dmaven.test.skip=true

Now, when you build your project build, all unit tests will be ignored.

[Seen: Mkyong]

By default, when you build your project with maven, it will run the entire unit tests of your project.

$ mvn install

Or:

$ mvn package

However, if any of unit test is failed, it will force Maven to abort the building process. In real life, you may “STILL” need to build your project even unit test is failed.

To skip it, use “-Dmaven.test.skip=true” option to tell Maven that you want to skip running the entire unit test. For example,

$ mvn install -Dmaven.test.skip=true
$ mvn package -Dmaven.test.skip=true

Now, when you build your project build, all unit tests will be ignored.

[Seen: Mkyong]