Maven"导入"范围

abk*_*drd 2 maven

我有该模块的模块和测试.测试需要resteasy-client,但模块不行.我不会混合模块依赖项和依赖项进行测试,我该怎么做?我试图使用maven'import'范围. 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>
Run Code Online (Sandbox Code Playgroud)

my-module-1.pom在本地nexus存储库中:

<?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>
Run Code Online (Sandbox Code Playgroud)

我似乎mvn看到这个文件,但编译失败:

[错误] /home/xxx.java:[5,38]错误:包org.jboss.resteasy.client.jaxrs不存在[错误] /home/xxx.java:[6,38]错误:包org. jboss.resteasy.client.jaxrs不存在[错误] /home/xxx.java:[7,38]错误:包org.jboss.resteasy.client.jaxrs不存在

但是当我将这些dependensies复制粘贴到module-1的pom.xml中时,一切都很好!但我不想混合,快速变得难以理解.

Mic*_*ski 5

这是因为import范围仅在内部有效<dependencyManagement>.(在封面下它基本上只是粘贴导入的内容.)

因此,它可以用于管理(设置)您可能使用的依赖项版本,但实际依赖于它们需要单独声明(内部<dependencies>).

到目前为止,我发现实现目标的最好方法(就像我得到的那样)是my-module-1-testing-support使用compile范围创建依赖于JUnit,Mockito等的模块.然后,您可以依赖于my-module-1-testing-support使用test范围并使用范围来获取其依赖性(传递性)test,如依赖性机制简介中所述.