Maven: use the special test classes of project A in project B

I have projects A and B, where B requires A. Inside the project AI there is a UC of class UC, which should be available only for JUnit tests and, therefore, is in src/test/java project A. As long as I I write tests in AI have access to UC. However, if I run Maven and want it to run tests in B, I get compiler errors because UC is not available in B.

Obviously, Eclipse includes all classes in all source folders when it compiles something (that is, it knows about UC when I write tests in B), and Maven removes all test classes in the final version of A.

My question is: what do I need to do to make UC available in B when I run its tests with Maven?

Please understand that I am new to Maven and I think similar questions have been asked. However, I cannot convert what is written there and correct it.

I hope this is clear what I'm trying to do ...

+8
maven junit dependencies project-structure
source share
3 answers

After looking a little more, I finally found a solution:

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

I sometimes saw this pattern on other issues, so I think I just didn't get it ... Oh, fine. * Eyeroll *

1 This original link has stopped working. I found it again on archive.org (not against an awkward layout).

+7
source share

The maven-jar-plugin page - http://maven.apache.org/plugins/maven-jar-plugin/usage.html - mentions two paths. A simple approach is to create an artifact of a test banner, and then a link to it. (fragments clearly copied from the official page)

 <project> ... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> ... </project> 

and then refer to it with the type of test container and scope of verification in projects that need it:

 <project> ... <dependencies> <dependency> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <type>test-jar</type> <version>version</version> <scope>test</scope> </dependency> </dependencies> ... </project> 

If you need to do this a lot, you should probably consider porting test code to individual projects.

+2
source share

I always found uncomfortable test banks. They are peculiar because they are used only after they have been recently deployed to the repository. Otherwise, other projects do not see changes. This is why I recommend creating only regular projects that you can use to enter your test assistants, and then refer to them using the scope of validation.

0
source share

All Articles