How to deal with relative path in Junits between Maven and Intellij

前端 未结 5 1654
臣服心动
臣服心动 2021-01-30 08:37

I have a maven project with a module

/myProject
pom.xml
    /myModule
    pom.xml
       /foo
       bar.txt

Consider a Junit in myModule

5条回答
  •  情深已故
    2021-01-30 09:22

    Solution from @tbruyelle works if you keep your project files(.idea) in the same directory as the source code. If you choose Keep Project files in ... in a different location, then $MODULE_DIR$ is trying to look up in the workspace directory and the paths cannot be found. This looks like a bug on IntelliJ, hope they fix it soon.

    Workaround: You can specify absolute / relative path of the maven module in working directory

    $MODULE_DIR$/../master/mavenmodule1
    
    $MODULE_DIR$: points to workspace directory
    ../: relative path to source code
    master: root directory of your source code
    mavenmodule1: maven module name / directory name of the child module.
    

    For multi module maven project you don't have a choice, you need to have a different run configurations pointing to that module. I wish there is another variable that points just to the $MAVEN_MODULE$ ($MODULE_DIR$/../master/$MAVEN_MODULE$) so we can use this configuration for all modules. For the above example, $MAVEN_MODULE$ will be substituted with mavenmodule1

提交回复
热议问题