Skip to content

Maven Notes

illyfrancis edited this page Nov 18, 2013 · 8 revisions

Dependency management

Use <dependencyManagement> to define dependencies and use it by refering to it in <dependencies>.

Plugin management

Similarly, use <pluginManagement> to configure plugins.

Multi module

Refer to Instance Apache Maven Starter.

In sub-module, to refer to parent POM configure it as

<parent>
  <groupId>...</groupId>
  <artifactId>...
  <versionId>...
  <relativePath>../../pom.xml</relativePath>
</parent>

Aparently the default value of <relativePath> is ../pom.xml. The above example assumes the project structure of

some-multi-modules-project
  |- pom.xml (POM aggregator)
  `- some-modules
       |- first-module
       |    `- pom.xml
       |- second-module
       |    `- pom.xml
       `- third-module
            `- pom.xml

Surefire:test

The mvn test phase is bound to the sourefire:test goal.

To run only one specific test MyUnitTest.java,

mvn test -Dtest=MyUnitTest

To execute a phase that implicitly invokes surefire:test, e.g. install phase, you can skip tests by

mvn install -DskipTests=true

Using -Dmaven.test.skip=true instead, you'd also skip the test class compilation.

Integration testing

Unlike test it is not bound by default in Maven. But goal failsafe:integration-test of can be used to run all tests with name ends with IT. Surefire plugin will automatically skip these tests.

To configure the plugin so that:

  • failsafe:integration-test goal when the integration-test phase is invoked and
  • failsafe:verify goal (to collect the test's results) when the verify phase is invoked

Configuration:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.14.1</version>
  <executions>
    <execution>
      <id>integration-test</id>
      <phase>integration-test</phase>
      <goals>
        <goal>integration-test</goal>
      </goals>
    </execution>
    <execution>
      <id>verify</id>
      <phase>verify</phase>
      <goals>
        <goal>verify</goal>
      </goals>
    </execution>
  <executions>
<plugin>

Dependency version ranges

  • exclusive : (, )
  • inclusive : [, ]

Example: greater than equal to 3.8 but less than 4.0

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>[3.8,4.0)</version>
  <scope>test</scope>
</dependency>

Example: less than equal to 3.8

<version>[,3.8]</version>

Example: exactly 3.8.2

<version>[3.8.2]</version>

Note, when defined like this <version>3.8.2</version> it is allow anything but prefer 3.8.2.

Clone this wiki locally