Skip to content

Commit ba30976

Browse files
committed
优化获取项目根路径的方式,兼容更多的情况
1 parent 009f495 commit ba30976

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/ProjectPathResolver.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,25 @@ public class ProjectPathResolver {
2929

3030
public ProjectPathResolver(String basePackage) {
3131
this.basePackage = basePackage;
32-
String curentThreadPath = Thread.currentThread().getContextClassLoader().getResource(".").getPath();
33-
curentThreadPath = getUTF8String(curentThreadPath);
34-
String[] paths = curentThreadPath.split("/");
35-
StringBuilder temp = new StringBuilder();
36-
for (int i = 0; i < paths.length; i++) {
37-
String path = paths[i];
38-
if (i < paths.length - 2) {
39-
temp.append(path);
40-
temp.append("/");
32+
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
33+
String projectDir = System.getProperty("user.dir");
34+
if (contextLoader.getResource(".") != null) {
35+
projectDir = contextLoader.getResource(".").getPath();
36+
projectDir = getUTF8String(projectDir);
37+
String[] paths = projectDir.split("/");
38+
StringBuilder temp = new StringBuilder();
39+
for (int i = 0; i < paths.length; i++) {
40+
String path = paths[i];
41+
if (i < paths.length - 2) {
42+
temp.append(path);
43+
temp.append("/");
44+
}
4145
}
46+
baseProjectPath = temp.toString();
47+
} else {
48+
projectDir = getUTF8String(projectDir).replace("\\", "/") + "/";
49+
baseProjectPath = projectDir;
4250
}
43-
baseProjectPath = temp.toString();
4451
sourcePath = new File(baseProjectPath + "src/main/java").toString();
4552
resourcePath = new File(baseProjectPath + "src/main/resources").toString();
4653
}

0 commit comments

Comments
 (0)