Jason
Jason

Reputation: 4130

Maven2 Custom Profile For Testing

I want to create a custom profile for in my Maven2 pom.xml file to isolate test-related dependencies and settings, using the surefire plugin, but am somewhat confused by the documentation. Ultimately, I don't want junit/etc to be in the production deployment package.

Does anyone have an example that can get me started?

Upvotes: 1

Views: 190

Answers (2)

Pascal Thivent
Pascal Thivent

Reputation: 570335

You don't need a profile for this (I mean, if you really want to use a profile, you can, but you don't need to). Maven has a built-in feature that allows to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks. This feature is called Dependency Scope and this is what the documentation writes about the test scope:

This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.

So, if you want to use a dependency during the test phase but don't want to have it packaged in the final artifact, just declare it with a test scope:

<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
      <version>1.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Upvotes: 3

Andreas Dolk
Andreas Dolk

Reputation: 114767

That's pretty simple. Declare the junit dependency like this:

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
</dependency>

The scope will make sure, that the junit library will not appear in the deployment package. And you won't see the test classes in the production packages, if maven found the sources in the src/test/java folder

Upvotes: 3

Related Questions