首页 > 解决方案 > Get module path not root path in Java

问题描述

I have a multi-module Java(Spring) project, which build by Gradle 6.7.1. And I use in Jetbrain IDEA to develop. The file Structure like this:

root
  |--orm
  |   +---hibernates
  |
  |--web
      |--mvc
      |--rest

And then, I have tried some codes in my module project like below, what I get all are root path (/home/user/IdeaProjects/root/), not module path (/home/user/IdeaProjects/root/web/mvc). How can I get module path (/home/user/IdeaProjects/root/web/mvc) ?

new File("").getAbsolutePath()

标签: javaspringgradleintellij-ideagroovy

解决方案


Assuming for instance that your mvc project is setup like this in setting.gradle, in the root folder :

include 'mvc'
project(':mvc').projectDir = new File('./web/mvc')

Then, to get the path /home/user/IdeaProjects/root/web/mvc, just try this :

println project(':mvc').projectDir

Will prints :

/home/user/IdeaProjects/root/web/mvc


推荐阅读