Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make maven to take resources from /src/test/resources instead of /src/main/resources for a dependent project?

I've searched the forum to find the answer to my problem, but I was unable to find it. My problem is, that:

I have two projects: ProjectA and ProjectB. ProjectB uses ProjectA. In ProjectA i have two folders: /src/main/resources and /src/test/resources. In ProjectB i run: mvn clean install. I want, that in the test phase, classes in ProjectB use resources from /src/test/resources instead of /src/main/resources.

This is what I tried: http://www.waltercedric.com/java-j2ee-mainmenu-53/361-maven-build-system/1349-maven-reusing-test-classes-across-multi-modules-projects.html

It is similar to my problem, but after I configured the test-jar goal for ProjectA, ProjectB still runs the tests in the way, that classes in ProjectA use properties from /src/main/resources instead of /src/test/resources.

My pom.xml in ProjectA looks like:

<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>

In ProjectB my pom.xml looks like:

<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>

Is there a method Any help would be appreciated!

Sincerely,
Mateusz Moroz

like image 694
Mateusz Moroz Avatar asked May 16 '13 15:05

Mateusz Moroz


People also ask

How do I add resources to src test?

Right click on maven project --->Click on Build Path ----->Click on New Source Folder. New source folder window will open, give the name to your folder example - src/test/source. click on Finish.

What is src main Java in Maven?

The src/main Directory As the name indicates, src/main is the most important directory of a Maven project. Anything that is supposed to be part of an artifact, be it a jar or war, should be present here. Its subdirectories are: src/main/java – Java source code for the artifact.

Where do you put resources in POM?

Via the resources area in the pom you can filter files from their way src/main/resources to the target/classes folder. The lifecycle of Maven is not influenced by this. I have added resources successfully in . Jar file.


1 Answers

The src/main/resources are packed to the jar file named ProjectA.jar as the following structure

ProjectA.jar
|`-com
|  `-sensano
|    `-foo
`-[the resource form src/main/resources]

Sadly the src/test/resources are also packed to the jar file named ProjectA-tests.jar as the following structure as well.

ProjectA-tests.jar
|`-com
|  `-sensano
|    `-foo
`-[the resource form src/test/resources]

If the resource name that you require are the same name for both from src/main/resources and src/test/resources. There may be some class loader trouble. IMHO, the nearest wins.

Since you put the ProjectA before the ProjectA-tests, then may be the root cause that the ProjectB will use the src/main/resources from ProjectA since it is nearest.

Please try to swap by putting the ProjectA-tests before the ProjectA as the following: -

<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>

The nearest will be the ProjectA-tests, and ProjectB should use the src/test/resources instead.

I hope this may help.

like image 126
Charlee Chitsuk Avatar answered Oct 12 '22 10:10

Charlee Chitsuk