IntelliJ 添加外部 jar 作为相对路径的依赖项

IntelliJ add external jar as dependency from relative path

恢复这个问题:IntelliJ external jar relative path 因为答案对我帮助不大。

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?

我想要的是能够从相对路径添加依赖项,这样我的,呃,我想,当移动到另一台计算机时,“微服务”将与项目的其余部分协同工作,或者 CI 等等。没有任何绝对路径。

问题中的回答提到了变量 $MODULE_DIR。我发现 https://www.jetbrains.com/help/idea/absolute-path-variables.html 提到了一个变量 $MODULE_IML_DIR。两者都不适合我。

$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)

如果库在项目根目录下,路径将使用占位符相对存储。

在库 .xml 文件或项目 .iml 文件中,您将看到如下内容:

<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>