I'm new to Maven and I'm trying to convert a few projects to work on Maven and I'm not sure what is the correct way to structure them - here is what I have:
I have a common module - named Common
and two different applications that have nothing in common a part from the fact they both depend on Common
. Let's call them A
and B
.
The dependencies between A
->Common
and B
->Common
are both for runtime and for tests - meaning that A
's test classes require Common
's test classes.
I tried various combinations I could think of - but non of them produced what I want.
The strange thing is that my code compiles, but JUnits fail since the test classes from Common
are not found in the classpath.
Should I add 2 profiles to the Common
to create 2 artifacts and add 2 dependencies in A
and B
to both artifacts? (Is that possible?)
Is there a correct way to do what I wanted? Should I restructure my code to fit Maven?
This is a common maven pitfall. Test classes from an artifact aren't available when you depend on that artifact. It is actually reasonable: when you depend on Common
, you depend on production classes (JAR file). Test classes are needed only for running tests and aren't even included in the final JAR.
Assuming your Common
test classes contain some utility method needed for all Common
tests, A
and B
tests, here is a suggested structure:
Common-test
- containing common utility test classes (not test cases!) in /src/main/java
(!)Common
depends on Common-test
with <scope>test</scope>
A
and B
depend on both Common
(with default scope) and Common-test
(with test
scope)
(source: yuml.me)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With