Find maven dependencies programatically
Hello everyone,
I am writing a custom language plugin and need to resolve maven dependencies.
The plugin provides support for a custom language and is applied to projects containing files of this custom language.
Projects can depend on each other via Maven dependencies.
The archive files (jar files) of each dependency do not contain files of this custom file type.
The problem I am facing now is, that I need to collect all files of this custom type.
Example: Project A depends on Project B, Project B depends on Project C
Let's say the custom file type has the extension .myType
Project A, Project B, and Project C, contain multiple .myType files each.
The jar file of Project B inside the maven-repo contains only its own .myType - files.
How can I access the .myType - files of Project C? Are there any utility classes suitable for this problem?
In other posts I read about a MavenFacade but I think it does not exist anymore? At least I could not find it.
I can collect all .myType - files using the ProjectScope.getLibrariesScope, but as every project has nighly builds, I receive multiple same .myType files - one per jar file of each nightly build.
Please sign in to leave a comment.
Hi,
Could you please link the other posts mentioning
MavenFacade
?Also, could you please elaborate on the issue with nightly builds in more detail?
Hi,
One post mentioning the MavenFacade is this one: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206781955-How-can-I-discover-the-maven-module-containing-a-file-
However, this post is from 2010 and I guess this class has been removed (?).
The problem with the nighly builds is the following:
Project B and Project C are build every night. This means, that Project A downloads the sources of Project B and Project C every day Project A is being built.
The sources are suffixed by the date, e.g. Project-C-20230810
When I programatically locate the dependencies of Project A, I receive the jar file of Project B.
In the same folder, where this jar file is located, are also the other nightly jar files of Project B, suffixed by different dates.
Since the .myType files of Project C are NOT included inside the jar of Project B, I need to find the exact jar-file name of the Project C jar, which is used in the jar file I received programatically.
I hope this does not sound too confusing. For a probably better understanding, here is a folder view of my local maven repo
So in this case, I need to get the correct Project-C-xxx.jar.
Can I somehow create an org.jetbrains.idea.maven.project.MavenProject, or something similar from the Project-B-xxx.jar / Project-B-xxx.pom and access the dependencies this way?
Update: I think what expresses the problem better is the following:
When expanding the external libraries on the bottom left, there are multiple "same" libraries with different versions (these are jar files).
When opening a file of such an external library in the editor, how do I resolve the dependencies of the file's library?
Dobra101, would something along these lines be helpful to you?
This code communicates with a Maven server to determine the dependencies of a specified POM file. While it takes some shortcuts, it can serve as a starting point.
A higher-level code might look like this (but note that it adds the POM as a module to your project)
Thank you for the code snippets, I will give it a try.
However, my main problem is the following (maybe there is a much simpler solution I don't thought of yet):
Files of .myType are intermediate code files - the content is being rendered to a different language. That's why they are not included in the jar files.
However, references across these files are possible, such that inside a .myType file of Project A, parts of a .myType file of Project C can be referenced.
Where I now struggle is the reference resolve step. Resolving .myType references from Project A to Project B works as expected.
But when I open a .myType file in the editor, I can't resolve .myType references to Project C, as I probably have multiple versions of Project C in my external library, and I don't know which version is required for Project B.
That's why I somehow try to backward resolve the transitive dependencies of Maven.
As I said, there's probably a much simpler solution, but as of now I am clueless.
So the question is about resolving references in code in IDEA editor?
Let's take the same example "Project A depends on Project B, Project B depends on Project C", but consider java language.
Project A pom.xml
Project B pom.xml
Class A in Project A
Scenario 1. All 3 projects are imported as IDEA modules
Here is the Project Structure. Note that IDEA module ProjectA depends on ProjectC via a module dependency. This is why ClassC reference is correctly resolved in ClassA. If we manually remove ProjectC dependency, ClassC will not be resolved in ClassA anymore.
Scenario 2. Only Project A is imported as an IDEA module
Now Project Structure looks like this. Project A still depends on both Project B and C, but those are library dependencies now. Code resolution works, but if we remove one of the dependencies it won't.
Are you missing a module/library dependency in your project structure? If so, try adding it manually and see if it helps.
Hi, that cleared it a bit, thank you.
However, the problem is, that I have scenario 2.
I resolve a dependency inside ProjectA, and the file of ProjectB opens in the editor.
The reference resolve inside this ProjectB file was the part where I struggled.
For now I could solve it by searching the project library tables for the library which contains this file.
For this table entry I can receive the module it belongs to (I'm not on the PC at the moment, so I don't know what the method was called).
However, this module is called "coverage-aggregate". I don't know yet what that means, but now I can search the .myType files inside the module scope.
Thank you for your help!