如何使maven从/ src/test/resources而不是/ src/main/resources获取依赖项目的资源?

Mat*_*roz 13 resources dependencies jar maven

我搜索了论坛,找到了我的问题的答案,但我无法找到它.我的问题是:

我有两个项目:ProjectA和ProjectB.ProjectB使用ProjectA.在ProjectA中,我有两个文件夹:/ src/main/resources和/ src/test/resources.在ProjectB中我运行:mvn clean install.我想,在测试阶段,ProjectB中的类使用/ src/test/resources中的资源而不是/ src/main/resources.

这就是我尝试过的:http: //www.waltercedric.com/java-j2ee-mainmenu-53/361-maven-build-system/1349-maven-reusing-test-classes-across-multi-modules-projects. HTML

它类似于我的问题,但是在为ProjectA配置test-jar目标之后,ProjectB仍然运行测试,ProjectA中的类使用/ src/main/resources中的属性而不是/ src/test/resources.

我在ProjectA中的pom.xml看起来像:

<project ...>
    <parent>
        ...
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>ProjectA</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        ...
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

在ProjectB中,我的pom.xml看起来像:

<project ...>
    <parent>
        ...
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>ProjectB</artifactId>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.sensano</groupId>
            <artifactId>ProjectA</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>ProjectA</groupId>
            <artifactId>ProjectA</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>test</scope>
            <type>test-jar</type>
        </dependency>
    </dependencies>

</project>
Run Code Online (Sandbox Code Playgroud)

有没有方法任何帮助将不胜感激!

此致,
Mateusz Moroz

Cha*_*suk 10

src/main/resources被打包到名为jar文件ProjectA.jar为以下结构

ProjectA.jar
|`-com
|  `-sensano
|    `-foo
`-[the resource form src/main/resources]
Run Code Online (Sandbox Code Playgroud)

遗憾的是,src/test/resources它们也被打包到名为ProjectA-tests.jar以下结构的jar文件中.

ProjectA-tests.jar
|`-com
|  `-sensano
|    `-foo
`-[the resource form src/test/resources]
Run Code Online (Sandbox Code Playgroud)

如果您需要的资源名称与from src/main/resources和/的名称相同src/test/resources.可能会有一些类加载器问题.恕我直言,最近的胜利.

既然你把它放在ProjectA之前ProjectA-tests,那么可能是ProjectB将使用src/main/resourcesfrom的最根本原因ProjectA.

请尝试通过将交换ProjectA-testsProjectA,如下: -

<dependencies>
    <dependency>
        <groupId>com.sensano</groupId>
        <artifactId>ProjectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>test</scope>
        <type>test-jar</type>
    </dependency>
    <dependency>
        <groupId>com.sensano</groupId>
        <artifactId>ProjectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

最近的将是ProjectA-tests,而ProjectB应该使用src/test/resources相反.

我希望这可能有所帮助.