Java – Maven project design – share a common project

I'm new to maven, I'm trying to convert some projects into Maven work, and I'm not sure what's the right way to build them - that's what I have:

I have a common module called common and two different applications that do not depend on the fact that common We call them a and B

The dependency between a - > common and B - > common applies to both runtime and test - which means that a's test class needs common's test class

I tried all kinds of combinations I could think of - but they didn't produce what I wanted Strangely, my code compiled, but junits failed because the common test class could not be found in the classpath

Should I add 2 configuration files to common to create 2 artifacts and add 2 dependencies for these artifacts in a and B? Is that possible Is there the right way to do what I want? Should I refactor my code to fit Maven?

Solution

This is a common Maven trap When you depend on the artifact, the test class in the artifact is not available This is actually reasonable: when you rely on common, you rely on production classes (JAR files) Test classes are only used to run tests and are not even included in the final jar

Suppose your common test class contains all common tests, some practical methods required for a and B tests. Here is a suggested structure:

>Common test – in / SRC / main / Java (!) Contains generic utility test classes (not test cases!) > Common depends on common test with < scope > test < / scope > > A and B depend on common (default range) and common test (with test range)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>