where to define test maven dependencies

Viewed 986

I want to supply test dependencies like spring, mockito and junit in every project to make it as easy as possible to write tests. Should I use dependency management for this dependencies or define them in the parent pom?

3 Answers

Yes , You don't need to define the dependency in every project. Just define the dependency in parent pom. Child project will automatically inherits its parent pom. Also If you want to use different version of Mockito or anything. Just override the parent dependency in child one.

All the common dependencies can be mentioned in the parent pom file. There mainly 4 types of dependencies that can be mentioned in a pom file.

  1. Library Dependencies created by ourselves
  2. Module Dependencies from our own modules
  3. 3rd Party library Dependencies
  4. Dependencies for tests

Example

<dependencies>
<!-- Library Dependencies created by ourselves -->
    <dependency>
        <groupId>it.myapp</groupId>
        <artifactId>MyAppBootstrap</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>it.myapp.libs</groupId>
        <artifactId>b2b_connecttion</artifactId>
    </dependency>

    <!-- Module Dependencies from our own modules-->
    <dependency>
        <groupId>it.myapp.mymodules</groupId>
        <artifactId>RevenueManager</artifactId>
        <version>${myapp.module.version}</version>
        <classifier>classes</classifier>
    </dependency>

    <!-- 3rd Party Dependency -->
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-vfs2</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Dependencies for tests -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

there are two ways to achieve it -

  1. you declare the dependencies in the parent pom in the <dependencies /> node, and each child will benefit from the dependency.

  2. Add the dependencies in the parent pom under the <dependencyManagement /> node and in each child that requires it, add the dependency in the node. You can choose not to set the version of the dependency.

So for example, if you declare this in the parent pom:

<dependencies>
    <dependency>
        <groupId>org.abc</groupId>
        <artifactId>xyz</artifactId>
        <version>your_version</version>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.abc</groupId>
            <artifactId>xyz</artifactId>
            <version>your_version</version>
            <scope>your_scope</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Related