abkvandrd
abkvandrd

Reputation: 203

Maven "import" scope

I have module and tests for that module. Tests need resteasy-client, but module doesn't. I don't want mix module dependencies and dependencies for tests, how can i do this? I trying to use maven 'import' scope. pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>ru.my.project</groupId>
        <artifactId>my-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>my-module-1</artifactId>
    <packaging>jar</packaging>
    <name>This is one of my modules</name>

    <dependencies>
        <dependency>
            <groupId>ru.my.project</groupId>
            <artifactId>my-module-2</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

<!--I want to import dependencies for test from this pom:-->
        <dependency>
            <groupId>ru.my.project.test</groupId>
            <artifactId>my-module-1-test</artifactId>
            <type>pom</type>
            <version>1.0</version>
            <scope>import</scope>
        </dependency>
    </dependencies>

</project>

and my-module-1.pom in local nexus repository:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ru.my.project.test</groupId>
    <artifactId>my-module-1-test</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <name>Dependencies for testing module-1</name>

    <dependencyManagement>
        <dependencies>    
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-client</artifactId>
                <version>3.0.11.Final</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>3.0.11.Final</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>     
</project>

I seems that mvn sees this file, but compilation fails:

[ERROR] /home/xxx.java:[5,38] error: package org.jboss.resteasy.client.jaxrs does not exist [ERROR] /home/xxx.java:[6,38] error: package org.jboss.resteasy.client.jaxrs does not exist [ERROR] /home/xxx.java:[7,38] error: package org.jboss.resteasy.client.jaxrs does not exist

But when I copy-paste those dependensies in module-1`s pom.xml everything is fine! But I don't want to mix, it becomes unreadable fast.

Upvotes: 5

Views: 14729

Answers (2)

cahen
cahen

Reputation: 16636

Use the Bill of Materials pattern described by Apache.

(open the link and search for "BOM")

The root of the project is the BOM pom. It defines the versions of all the artifacts that will be created in the library. Other projects that wish to use the library should import this pom into the dependencyManagement section of their pom.

Upvotes: 1

Michał Kalinowski
Michał Kalinowski

Reputation: 17933

This is because the import scope does work only within <dependencyManagement>. (Under the cover it basically just paste the imported content.)

So, it can be used for managing (setting up) versions of dependencies you will potentially use, but the actual depending on them needs separate declaration (within <dependencies>).

The best way I found so far for achieving your goal (as I get it) is to create module like my-module-1-testing-support which depends on JUnit, Mockito, etc. using compile scope. Then you can depend on my-module-1-testing-support using test scope and get its dependencies (transitively) using test scope as well, as described at Introduction to the Dependency Mechanism.

Upvotes: 11

Related Questions