3

I am working on a Java maven built project, which consists of several modules. I (as many before me) face this issue that I have classes that are used in multiple of modules. I wish to find an elegant solution to the issue of sharing classes across all modules.

I am aware that this is possible to be accomplished by making another module called for example common where I would put all shared classes. After this module can be compiled into separate jar and can be used as dependency in other modules.

However I do not find this solution elegant enough and am looking for a more direct sharing. This essentially means that I would like to have those classes as separate module common, but this module would not be compiled as separate jar, instead those classes would be directly included into compilation/packaging of all depending modules.

Is this possible to achieve using maven?

UPD: To add an example why I do not find mentioned above way as acceptable - when writing code and mid way realize that some changes should be done to the common classes, all IDEs would require after those changes to run install goal on common module in order to have it as compiled jar in classpath (so that those changes will be visible in other modules). This is just one of the examples why I find this way inconvenient and am looking for more elegant solution.

Alexey Kamenskiy
  • 2,888
  • 5
  • 36
  • 56
  • Not an exact duplicate but very similar (and containing the answer): http://stackoverflow.com/questions/586202/best-practices-for-copying-files-with-maven – Gimby Feb 02 '15 at 12:07
  • 2
    Why is creating a shared jar not elegant? – Stefan Feb 02 '15 at 12:08
  • I must share Stefan's wonderment. Its how Java works, and thus what Maven conventions assume you will do. If you go iceskating uphill by not finding the standard way of doing things "elegant", you make your life difficult. – Gimby Feb 02 '15 at 12:09
  • This is because the application I am working on is in fact 3-piece app each of which is a standalone running jar. In this case I would have to for each of standalone parts do "build-with-dependencies" configuration, which for this case quite annoying to me. As in my question - yes, I know I can do it this way, but I find direct inclusion of shared classes way more elegant for this situation. – Alexey Kamenskiy Feb 02 '15 at 12:14
  • @Gimby To extend my point - `Its how Java works, and thus what Maven conventions assume you will do` this doesn't always mean convenient for the developer. Maven used here purely as build system and we do not even have any external dependency in our project. I also could easily switch from Maven to something else that could actually do this easier, but the question would still exist - is it possible or not. The question itself is less about conventions, but rather about achieving goal. – Alexey Kamenskiy Feb 02 '15 at 12:20
  • Hm..if you already have multiple modules the questions is why is a problem and not convenient to have a common/shared module which contains the classes which are shared over different modules. This is a kind of separation of concerns which you already did cause you separated you build in different modules so the consquence is to have a shared module as well. So now you stating to go a complete different way with shared classes? Hm..sounds strange to me...But if you don't like the way Maven works just leave and use something different. – khmarbaise Feb 02 '15 at 12:52
  • Would this be a solution? : http://stackoverflow.com/questions/1895537/how-to-merge-module-jars-to-a-single-jar-in-maven2 else you might be able to use aggregation. Something like this: https://rombertw.wordpress.com/2010/05/14/maven-recipe-building-an-aggregate-jar/ – Martin Frey Feb 02 '15 at 20:38
  • @AlexKey Have to disagree with you on this one. Its not necessary that every developer may find the conventions to be convenient. But you still have to follow conventions to at least make things work the way they are meant to. – Saif Asif Feb 03 '15 at 05:54

0 Answers0