类路径和maven概要文件

类路径和maven概要文件,maven,classpath,maven-profiles,Maven,Classpath,Maven Profiles,我有3个罐子,分别是1罐,2罐,3罐 在jar1中,有一些代码从类路径读取文件。但是,该文件不会出现在jar1的类路径中。相反,jar2将作为jar1的依赖项插入,jar2类路径将包含该文件 我有jar3,它的类路径中也将包含相同的文件,但是我将在jar1的pom.xml中为jar3声明范围为test 现在,当测试用例被执行时,我们如何告诉maven总是从jar3(作用域为测试)类路径获取文件,尽管jar2是作为主要依赖项(作用域不是测试)给出的 这是否可以通过maven配置文件实现?如果是,我

我有3个罐子,分别是1罐,2罐,3罐

在jar1中,有一些代码从类路径读取文件。但是,该文件不会出现在jar1的类路径中。相反,jar2将作为jar1的依赖项插入,jar2类路径将包含该文件

我有jar3,它的类路径中也将包含相同的文件,但是我将在jar1的
pom.xml
中为jar3声明范围为
test

现在,当测试用例被执行时,我们如何告诉maven总是从jar3(作用域为测试)类路径获取文件,尽管jar2是作为主要依赖项(作用域不是测试)给出的

这是否可以通过maven配置文件实现?如果是,我们如何指定? 或者我们可以使用maven资源插件在测试范围内复制文件吗

如果相同的文件存在于作为依赖项提供的多个JAR中,那么类路径将如何设置

示例jar1 pom.xml

<dependencies>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>jar2</artifactId>
            <version>0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>jar3</artifactId>
            <version>0.1-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
</dependencies>

com.test
罐子2
0.1-1快照
com.test
jar3
0.1-1快照
测试
jar2和jar3都有资源文件


问题:将选择哪一项以及为什么选择?

如果您的依赖项在依赖项树中处于同一级别,则您的pom中的声明顺序将获胜,根据文档:

请注意,如果两个依赖项版本在依赖项树中处于相同的深度,则在Maven 2.0.8之前,没有定义哪一个版本将获胜,但由于Maven 2.0.9,声明中的顺序才是最重要的:第一个声明获胜

因此,在您的案例中,这两个依赖关系在依赖关系树中处于同一级别(第一级,在POM中声明),并且无论何时
test
scope都会出现
compile
scope,因此
jar2
将获胜,因为您在POM中首先声明了它(根据您发布的剪报)

如果您希望始终从
jar3
加载文件,但仅在测试期间加载,只需在依赖项中首先声明
jar3
。它不会影响最终的可交付成果(在
test
范围内),但它将定义类路径的测试顺序,从而为您提供预期的场景。你不需要Maven的个人资料


验证它的简单测试用例:

让我们在Maven项目的
src\main\resources
中定义一个属性
file.properties
。对于项目
资源提供程序
(artifactId),该文件如下所示:

对于项目
资源提供者2
(artifactId),如下所示:

注意:同一个文件名分为两个内容不同的项目

然后在消费者项目(
资源消费者
)中,我们可以有以下JUnit测试用例示例:

public class MainTest {

    @Test
    public void checkClassPath() {
        InputStream is = MainTest.class.getResourceAsStream("/file.properties");
        Scanner s = new Scanner(is);
        System.out.println(s.nextLine());
    }

}
对于
资源使用者中的以下依赖项

<dependencies>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>
因此,声明的第一个依赖项,
resource-provider2
获胜(注意范围,
test

将依赖项顺序更改为:

<dependencies>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>

注意:这次
资源提供者
获胜,因为它是首先声明的,而且
编译
范围也是
测试
范围的一部分,而事实并非如此。

@svsteja不客气。旁注:当使用这种方法(基于订购)时,我建议您在pom中添加注释,以帮助其他开发人员或您自己进行维护和故障排除:)
<dependencies>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>
-------------------------------------------------------  
 T E S T S  
-------------------------------------------------------  
Running com.sample.MainTest   
property=from-resource-provider2  
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec  
<dependencies>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>resource-provider2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>
-------------------------------------------------------   
 T E S T S   
-------------------------------------------------------   
Running com.sample.MainTest   
property=from-resource-provider   
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.07 sec