4

Background:

I have multiple projects that utilize the same logic across their business so I decided to split the shared part and reference it in both projects.

Example:

Suppose there is an HR application and an Accounting application that require a shared business in this case let's say count hours which is part of calculations class (let's call it calc).

Both applications (HR & Accounting) have their own repository on gitlab. Also have independent deployment and utilize the calc package which is uploaded on nexus repository.

enter image description here

Question:

I would like to know if there is a way to find projects using calc package through the package name across the repositories.

Hoshani
  • 746
  • 1
  • 10
  • 27
  • 1
    This would need to be a function of either your source version control system, or nexus as this needs metadata that is not collected by default. If you use a build server you may be able to configure it to tell you. – Thorbjørn Ravn Andersen Aug 21 '22 at 17:31
  • In Maven as well as other build tools are using groupId/artifactId/version to identify an artifact (in other words a JAR file) which contains packages. But there is no mechanism to search/find a project by using a package name. – khmarbaise Aug 21 '22 at 19:21
  • 1
    Sites like `mvnrepository.com` and `github.com` show usages/counts of packages as dependencies in other projects. I assume they do this by parsing all pom/gradle/etc build files they have on their system. If there is no existing solution/plugin for that, you would have to write something on your own. – slindenau Aug 24 '22 at 15:11
  • @slindenau surely it does exist somewhere, and surely I am not the first person to come up with such an idea. writing my own solution is the last I want to do, as I would like to know all available solutions first. – Hoshani Aug 25 '22 at 09:57
  • 2
    Just stumbled upon this answer: https://stackoverflow.com/a/34066648/2846138 - it's talking about a tool for building a dependency graph, and although the answer is old, the tool seems to be still in active development: https://github.com/ltearno/pom-explorer Maybe the other answers on that question also help you a bit? – cyberbrain Aug 25 '22 at 11:18
  • @cyberbrain this looks promising! However, it depends on projects loaded on local machine while what I want is something that integrates with git. – Hoshani Aug 28 '22 at 11:28
  • 1
    so your projects "ACC" and "HR" do _not_ have their build results published to a (even local) maven repository server? – cyberbrain Aug 28 '22 at 11:43
  • @cyberbrain if by build result you are referring to `jar` files then these are published only to gitlab. If this is not what you mean this please explain it further. – Hoshani Aug 29 '22 at 10:51
  • 1
    Yes, I meant exactly that. GitLab also has a feature called "Package Registry" to put packages with the `mvn deploy` command (see https://docs.gitlab.com/ee/user/packages/maven_repository/), do you use that, or just commit and push the jar-files into the regular source repository? The GitLab version I got here for comparison has this as feature "Packages" in the General settings. – cyberbrain Aug 29 '22 at 11:20
  • @cyberbrain so what you are proposing is to stop using nexus and go to gitlab, from there after registering packages I can search for their usage? – Hoshani Aug 30 '22 at 11:52
  • 1
    no, if you use Nexus already and publish your jars (of ACC and HR) with `mvn deploy` there, all maven based solutions should work - even without having the sourcecode available on the local machine. Maybe metadata will be cached on your local machine, but that should happen automatically in the background. – cyberbrain Aug 30 '22 at 11:56

2 Answers2

5

If you have the projects that you want checked out locally, you could find the dependency-tree for each project:

mvn depedency:tree

This gives you all the (transitive) dependencies that are used the projects. If you do this for each project and then grep your package-name, this should find you all projects that use it, or at least have a dependency on it.

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
  • That looks a lot cleaner then writing a search script. However, I want to do it without checking out the projects locally. – Hoshani Aug 30 '22 at 11:39
  • I'm not aware of any solutions that can do this remotely. You do only need the pom files though (but beware for multi module projects that have poms in trees as we) – Rob Audenaerde Aug 30 '22 at 19:05
1

I am not sure how to calculate dependencies through package names. But:

  • If you have access to the source you can scan your projects for pom.xml and find the dependencies
  • If you have access to the runtime libraries you can scan these jars for /META-INF/maven/*/*/pom.xml and check the dependencies

Either way you should be able to find jar files making use of Calc. There is no way to ask Calc where it got used from.

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • my first thought was to scan `pom.xml` file for every project, seems a little tedious and would require me to update what I am looking for every time. It should work but I think we can find something better. – Hoshani Aug 28 '22 at 11:36