提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab; |
H |
2 |
|
|
3 |
import cn.hutool.core.io.FileTypeUtil; |
|
4 |
import cn.hutool.core.io.FileUtil; |
|
5 |
import cn.hutool.core.util.StrUtil; |
|
6 |
import com.iailab.framework.common.util.collection.SetUtils; |
|
7 |
import lombok.extern.slf4j.Slf4j; |
|
8 |
|
|
9 |
import java.io.File; |
|
10 |
import java.nio.charset.StandardCharsets; |
|
11 |
import java.util.Collection; |
|
12 |
import java.util.Set; |
|
13 |
import java.util.regex.Matcher; |
|
14 |
import java.util.stream.Collectors; |
|
15 |
|
|
16 |
import static java.io.File.separator; |
|
17 |
|
|
18 |
/** |
|
19 |
* 项目修改器,一键替换 Maven 的 groupId、artifactId,项目的 package 等 |
|
20 |
* <p> |
|
21 |
* 通过修改 groupIdNew、artifactIdNew、projectBaseDirNew 三个变量 |
|
22 |
* |
|
23 |
* @author iailab |
|
24 |
*/ |
|
25 |
@Slf4j |
|
26 |
public class ProjectReactor { |
|
27 |
|
|
28 |
private static final String GROUP_ID = "com.iailab"; |
|
29 |
private static final String ARTIFACT_ID = "iailab"; |
|
30 |
private static final String PACKAGE_NAME = "com.iailab"; |
|
31 |
private static final String TITLE = "工业互联网微服务平台"; |
|
32 |
|
|
33 |
/** |
|
34 |
* 白名单文件,不进行重写,避免出问题 |
|
35 |
*/ |
|
36 |
private static final Set<String> WHITE_FILE_TYPES = SetUtils.asSet("gif", "jpg", "svg", "png", // 图片 |
|
37 |
"eot", "woff2", "ttf", "woff", // 字体 |
|
38 |
"xdb"); // IP 库 |
|
39 |
|
|
40 |
public static void main(String[] args) { |
|
41 |
long start = System.currentTimeMillis(); |
|
42 |
String projectBaseDir = getProjectBaseDir(); |
|
43 |
log.info("[main][原项目路劲改地址 ({})]", projectBaseDir); |
|
44 |
|
|
45 |
// ========== 配置,需要你手动修改 ========== |
|
46 |
String groupIdNew = "com.iailab"; |
|
47 |
String artifactIdNew = "iailab"; |
|
48 |
String packageNameNew = "com.iailab"; |
|
49 |
String titleNew = "工业互联网微服务平台"; |
|
50 |
String projectBaseDirNew = projectBaseDir.replace("iailab-cloud", artifactIdNew); // 一键改名后,“新”项目所在的目录 |
|
51 |
log.info("[main][检测新项目目录 ({})是否存在]", projectBaseDirNew); |
|
52 |
if (FileUtil.exist(projectBaseDirNew)) { |
|
53 |
log.error("[main][新项目目录检测 ({})已存在,请更改新的目录!程序退出]", projectBaseDirNew); |
|
54 |
return; |
|
55 |
} |
|
56 |
// 如果新目录中存在 PACKAGE_NAME,ARTIFACT_ID 等关键字,路径会被替换,导致生成的文件不在预期目录 |
|
57 |
if (StrUtil.containsAny(projectBaseDirNew, PACKAGE_NAME, ARTIFACT_ID, StrUtil.upperFirst(ARTIFACT_ID))) { |
|
58 |
log.error("[main][新项目目录 `projectBaseDirNew` 检测 ({}) 存在冲突名称「{}」或者「{}」,请更改新的目录!程序退出]", |
|
59 |
projectBaseDirNew, PACKAGE_NAME, ARTIFACT_ID); |
|
60 |
return; |
|
61 |
} |
|
62 |
log.info("[main][完成新项目目录检测,新项目路径地址 ({})]", projectBaseDirNew); |
|
63 |
// 获得需要复制的文件 |
|
64 |
log.info("[main][开始获得需要重写的文件,预计需要 10-20 秒]"); |
|
65 |
Collection<File> files = listFiles(projectBaseDir); |
|
66 |
log.info("[main][需要重写的文件数量:{},预计需要 15-30 秒]", files.size()); |
|
67 |
// 写入文件 |
|
68 |
files.forEach(file -> { |
|
69 |
// 如果是白名单的文件类型,不进行重写,直接拷贝 |
|
70 |
String fileType = getFileType(file); |
|
71 |
if (WHITE_FILE_TYPES.contains(fileType)) { |
|
72 |
copyFile(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); |
|
73 |
return; |
|
74 |
} |
|
75 |
// 如果非白名单的文件类型,重写内容,在生成文件 |
|
76 |
String content = replaceFileContent(file, groupIdNew, artifactIdNew, packageNameNew, titleNew); |
|
77 |
writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); |
|
78 |
}); |
|
79 |
log.info("[main][重写完成]共耗时:{} 秒", (System.currentTimeMillis() - start) / 1000); |
|
80 |
} |
|
81 |
|
|
82 |
private static String getProjectBaseDir() { |
|
83 |
String baseDir = System.getProperty("user.dir"); |
|
84 |
if (StrUtil.isEmpty(baseDir)) { |
|
85 |
throw new NullPointerException("项目基础路径不存在"); |
|
86 |
} |
|
87 |
return baseDir; |
|
88 |
} |
|
89 |
|
|
90 |
private static Collection<File> listFiles(String projectBaseDir) { |
|
91 |
Collection<File> files = FileUtil.loopFiles(projectBaseDir); |
|
92 |
// 移除 IDEA、Git 自身的文件、Node 编译出来的文件 |
|
93 |
files = files.stream() |
|
94 |
.filter(file -> !file.getPath().contains(separator + "target" + separator) |
|
95 |
&& !file.getPath().contains(separator + "node_modules" + separator) |
|
96 |
&& !file.getPath().contains(separator + ".idea" + separator) |
|
97 |
&& !file.getPath().contains(separator + ".git" + separator) |
|
98 |
&& !file.getPath().contains(separator + "dist" + separator) |
|
99 |
&& !file.getPath().contains(".iml") |
|
100 |
&& !file.getPath().contains(".html.gz")) |
|
101 |
.collect(Collectors.toList()); |
|
102 |
return files; |
|
103 |
} |
|
104 |
|
|
105 |
private static String replaceFileContent(File file, String groupIdNew, |
|
106 |
String artifactIdNew, String packageNameNew, |
|
107 |
String titleNew) { |
|
108 |
String content = FileUtil.readString(file, StandardCharsets.UTF_8); |
|
109 |
// 如果是白名单的文件类型,不进行重写 |
|
110 |
String fileType = getFileType(file); |
|
111 |
if (WHITE_FILE_TYPES.contains(fileType)) { |
|
112 |
return content; |
|
113 |
} |
|
114 |
// 执行文件内容都重写 |
|
115 |
return content.replaceAll(GROUP_ID, groupIdNew) |
|
116 |
.replaceAll(PACKAGE_NAME, packageNameNew) |
|
117 |
.replaceAll(ARTIFACT_ID, artifactIdNew) // 必须放在最后替换,因为 ARTIFACT_ID 太短! |
|
118 |
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)) |
|
119 |
.replaceAll(TITLE, titleNew); |
|
120 |
} |
|
121 |
|
|
122 |
private static void writeFile(File file, String fileContent, String projectBaseDir, |
|
123 |
String projectBaseDirNew, String packageNameNew, String artifactIdNew) { |
|
124 |
String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); |
|
125 |
FileUtil.writeUtf8String(fileContent, newPath); |
|
126 |
} |
|
127 |
|
|
128 |
private static void copyFile(File file, String projectBaseDir, |
|
129 |
String projectBaseDirNew, String packageNameNew, String artifactIdNew) { |
|
130 |
String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); |
|
131 |
FileUtil.copyFile(file, new File(newPath)); |
|
132 |
} |
|
133 |
|
|
134 |
private static String buildNewFilePath(File file, String projectBaseDir, |
|
135 |
String projectBaseDirNew, String packageNameNew, String artifactIdNew) { |
|
136 |
return file.getPath().replace(projectBaseDir, projectBaseDirNew) // 新目录 |
|
137 |
.replace(PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(separator)), |
|
138 |
packageNameNew.replaceAll("\\.", Matcher.quoteReplacement(separator))) |
|
139 |
.replace(ARTIFACT_ID, artifactIdNew) // |
|
140 |
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)); |
|
141 |
} |
|
142 |
|
|
143 |
private static String getFileType(File file) { |
|
144 |
return file.length() > 0 ? FileTypeUtil.getType(file) : ""; |
|
145 |
} |
|
146 |
} |