Home > Enterprise >  IntelliJ add external jar as dependency from relative path
IntelliJ add external jar as dependency from relative path

Time:06-02

Reviving this question: IntelliJ external jar relative path because the answers were not very helpful to me.

I'm just trying out IntelliJ IDE and I want to add an external jar to my java project.

I have already figured out how to add external jars to my project in ProjectSettings -> Modules -> Dependencies. But the path is always absolute. I haven't found a way to make the path relative to the project folder (my lib folder is inside my project folder).

e.g. I don't want this C:\Users\AUser\IdeaProjects\AProject\lib\alibrary.jar

Instead I want this. ...\AProject\lib\alibrary.jar

In Netbeans this was possible by just checking a checkbox. Is there something similar in IntelliJ?

If not, how do you deal with the situation when someone else is trying to open up your project? Does he need to edit all library paths?

What I want is to be able to add dependencies from a relative path so that my, uh, "Microservice" I guess, will work in tandem with the rest of the project when moved to a different computer, or CI and such. No absolute paths whatsoever.

The responses in the question mention the variable $MODULE_DIR. I've found https://www.jetbrains.com/help/idea/absolute-path-variables.html which has mention of a variable $MODULE_IML_DIR. Neither worked for me.

$MODULE_DIR will be the directory of the IntelliJ Module - so probably try $MODULE_DIR/../../AProject/lib/alibrary.jar (or what your relative path will be)

CodePudding user response:

If the library is inside the project root, the path will be stored relatively using the placeholder.

In the library .xml file or in the project .iml file you'll see something like this:

<component name="libraryTable">
  <library name="log4j-1.2.17">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/lib/log4j-1.2.17.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
</component>
  • Related