Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I organise my project's codebase that targets multiple library versions

I'm writing a small library L1 that depends on a third party library L2. L2 has multiple versions that L1 needs to be able to support. Each version of L2 is bound to a given specific API and target JDK. I have no control over L2.

For instance :

  • L2-v1.x -> I need to be able to provide L1-v1.x
  • L2-v2.x -> I need to be able to provide L1-v2.x
  • L2-v3.x -> I need to be able to provide L1-v3.x

What would be the best way to organise code in git (what should I put in master / what branches / should I have multiple projects / multiple modules) knowing that I have to build the project using Maven and I want the build to be as simple as possible ?

Thank you in advance.

Edit: all versions of L2 are in Maven Central, all versions of L1 will have to be in Central.

like image 446
Kraal Avatar asked Nov 09 '22 04:11

Kraal


1 Answers

If those library sources are not in a maven repo, then you can follow "Using Git Submodules for Maven Artifacts Not in Central".
Git submodules is great for linking a prevision version of a repo into another.

Here is a version adapted to your setup:

  1. You setup your Maven project to have a parent pom and your own project L1 as a Maven module of that project.

  2. You import the project you want into your project. For example the L2 project at the right tag.

git submodule add /url/to/L2.git
cd L2
git checkout <L2-vy.x>
cd ..
git add .
git commit -m "Add submodule L2 at  <L2-vy.x>"
git push

The git submodule command will now have cloned L2 repository in a folder called L2.
Git will have added a .gitmodule file which looks like this:

submodule ["L2"] path = L2 url = /url/to/L2.git

You directory structure should look like this

yourParentProject
- pom.xml 
- .git
- .gitmodule
- L1
  \- pom.xml
- L2
  \- pom.xml
  1. In you parent pom.xml you add the L2 folder as a module.
<modules>
  <module>L1</module>
  <module>L2</module>
</modules>

And in your project L1, you add L2 as a dependency:

<groupId>com.github.user.L2</groupId>
<artifactId>L2</artifactId>
<version>L2-vy.x</version>
  1. Now everything is set-up. If you call mvm test on the parent project you will see that it first builds L2 and then your project L1.

Using it

When other developers clone your project, they need to install the module as well using git command:

git submodule update --init
like image 186
VonC Avatar answered Nov 14 '22 22:11

VonC