1

The problem I encountered is pretty standard: I need to have some common code base between micro-services despite the fact that this approach is discouraged as I've heard. At the moment I have common code as separate module that's built into jar and deployed to local Nexus. Then it's used as standard maven compile dependency. We use CI to build all modules. So when I want to change common code and use it in micro-service outright I can't do it. I must go through "two-stage process" when I first push common module changes, wait until module gets built and receives new version; and only then I can push micro-service code that uses that new version. That's at least awkward, in fact it's very annoying.

Can I use common module as some kind of "source dependency" in maven? What's the proper way to implement this?

Aleksandr Kravets
  • 5,750
  • 7
  • 53
  • 72

1 Answers1

2

The important question is: Is it ok for the different microservices to use different versions of the common jar?

If yes: You do not need to push changes. You update your common jar and test it separately (as far as possible). While updating a microservice, you can update the dependency (if you want). The microservices then run (possibly) different versions of your common jar.

If no: You can put your jar together with all microservices into one multi-module project, but this means that they all have to be built together every time. You cannot change and build a single one any more. But if they rely on common code in the very same version, they are tightly coupled, so that building only one is probably not a good idea anyway.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Usually it's normal to use different versions of common module in different micro-services. But it's not correct that I don't have to push changes. I need to ship a feature that depends on common functionality. Therefore even if I need to change 1 micro-service I still have to update common jar. – Aleksandr Kravets Nov 07 '18 at 10:03
  • Don't get me wrong. Nothing will break if I continue following "if yes" approach. The problem is that I ship features. Basically there's a task in Jira that says to implement functionality and If I don't do it right away(in one push) and start updating services one by one the task will take too much time and potentially create massive technical debt. – Aleksandr Kravets Nov 07 '18 at 10:09
  • But this cannot be completely automated because how should Maven know which microservice needs the new version of the jar, and which not? You can write a script, of course, or better, configure build triggers in your build server (Jenkins?) that starts a build if another one has finished, but this depends on your tool chain. – J Fabian Meier Nov 07 '18 at 10:11
  • I also don't agree your tight coupling thesis. Dependency I have on common component is no different of any external dependency. Obviously it's not true that micro-services are tightly coupled if they are based on same micro-service framework like DropWizard or Spring Boot. Same here – Aleksandr Kravets Nov 07 '18 at 10:12
  • I hoped you can point me a way to configure maven to use common code not as jar but as another source folder. – Aleksandr Kravets Nov 07 '18 at 10:24
  • I never heard of shared source folders, and I would consider them to be dangerous because you sneak in changes through the back door into an existing project. Even though it might sound tempting, I would advise you to not construct anything like that. – J Fabian Meier Nov 07 '18 at 10:43
  • I see no perfect solution for your problem. The "tight coupling" was only meant for the case that build everything (microservices and common jar) together in a multi-module project. – J Fabian Meier Nov 07 '18 at 10:54
  • Well you see, there's actually Go language which entirely exists in similar paradigm regarding sources. It indeed could be dangerous if someone violates library's Git repository. But that's the way it was initially designed and works so far for Go. I hope I could adapt this approach for my project – Aleksandr Kravets Nov 07 '18 at 11:00
  • I would not use shared source code in Java/Maven. It is not common and probably something will break. If you want to automate build steps, either go for multi-module or use a build server like Jenkins. – J Fabian Meier Nov 07 '18 at 11:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183238/discussion-between-aleksandr-kravets-and-jf-meier). – Aleksandr Kravets Nov 07 '18 at 12:30