Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven project design - sharing a common project

Tags:

java

maven

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?

like image 987
RonK Avatar asked Jun 22 '12 17:06

RonK


1 Answers

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)

UML
(source: yuml.me)

like image 69
Tomasz Nurkiewicz Avatar answered Sep 28 '22 10:09

Tomasz Nurkiewicz