开发者

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?


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜