d6333fc3
YunaiV
增加 ProjectReactor...
|
1
2
3
4
5
6
7
8
9
10
|
package cn.iocoder.yudao;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
|
37900a73
zhoulc
修改一键改包的bug
|
11
|
import java.util.regex.Matcher;
|
f651b86d
zhoulc
更改 文件过滤方法,适应windows
|
12
|
import java.util.stream.Collectors;
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
13
14
15
|
/**
* 项目修改器,一键替换 Maven 的 groupId、artifactId,项目的 package 等
|
37900a73
zhoulc
修改一键改包的bug
|
16
|
* <p>
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
17
18
19
20
21
22
23
24
25
26
|
* 通过修改 groupIdNew、artifactIdNew、projectBaseDirNew 三个变量
*
* @author 芋道源码
*/
@Slf4j
public class ProjectReactor {
private static final String GROUP_ID = "cn.iocoder.boot";
private static final String ARTIFACT_ID = "yudao";
private static final String PACKAGE_NAME = "cn.iocoder.yudao";
|
c7295703
zhoulc
修改title
|
27
|
private static final String TITLE = "芋道管理系统";
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
28
29
|
public static void main(String[] args) {
|
37900a73
zhoulc
修改一键改包的bug
|
30
|
long start = System.currentTimeMillis();
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
31
|
String projectBaseDir = getProjectBaseDir();
|
0bbc0349
YunaiV
优化 ProjectReactor...
|
32
|
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
33
|
// ========== 配置,需要你手动修改 ==========
|
5ac7c3bd
YunaiV
code review Proje...
|
34
|
String groupIdNew = "cn.huwing.boot"; // TODO @seeker:这几个参数改回去原来的哈。
|
37900a73
zhoulc
修改一键改包的bug
|
35
36
|
String artifactIdNew = "zdm";
String packageNameNew = "cn.huwing.zdm";
|
d9afb0e6
zhoulc
添加html.gz文件的过滤
|
37
|
String titleNew = "测试名管理系统";
|
0bbc0349
YunaiV
优化 ProjectReactor...
|
38
|
String projectBaseDirNew = projectBaseDir + "-new"; // 一键改名后,“新”项目所在的目录
|
37900a73
zhoulc
修改一键改包的bug
|
39
|
log.info("[main][新项目路径地址]projectBaseDirNew: " + projectBaseDirNew);
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
40
41
42
43
|
// 获得需要复制的文件
log.info("[main][开始获得需要重写的文件]");
Collection<File> files = listFiles(projectBaseDir);
|
f651b86d
zhoulc
更改 文件过滤方法,适应windows
|
44
|
log.info("[main][需要重写的文件数量:{},预计需要 5-10 秒]", files.size());
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
45
46
|
// 写入文件
files.forEach(file -> {
|
0bbc0349
YunaiV
优化 ProjectReactor...
|
47
|
String content = replaceFileContent(file, groupIdNew, artifactIdNew, packageNameNew, titleNew);
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
48
49
|
writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
});
|
5ac7c3bd
YunaiV
code review Proje...
|
50
51
|
long end = System.currentTimeMillis(); // TODO @seeker:直接减,不用增加变量
log.info("[main][重写完成]共耗时:" + (end - start) / 1000 + "秒"); // TODO @seeker:logger 不要拼接字符串,用占位符
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
52
53
54
|
}
private static String getProjectBaseDir() {
|
37900a73
zhoulc
修改一键改包的bug
|
55
|
return StrUtil.subBefore(new File(ProjectReactor.class.getClassLoader().getResource(File.separator).getFile()).getPath(), "\\yudao-server", false);
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
56
57
58
59
|
}
private static Collection<File> listFiles(String projectBaseDir) {
Collection<File> files = FileUtils.listFiles(new File(projectBaseDir), null, true);
|
f651b86d
zhoulc
更改 文件过滤方法,适应windows
|
60
61
|
// 移除 IDEA Git GitHub 自身的文件; Node 编译出来的文件
files = files.stream()
|
5ac7c3bd
YunaiV
code review Proje...
|
62
|
.filter(file -> !file.getPath().contains("\\target\\") // TODO @seeker:使用 File.separator,也要考虑 mac
|
f651b86d
zhoulc
更改 文件过滤方法,适应windows
|
63
64
65
66
|
&& !file.getPath().contains("\\node_modules\\")
&& !file.getPath().contains("\\.idea\\")
&& !file.getPath().contains("\\.git\\")
&& !file.getPath().contains("\\.github\\")
|
50b6a4cb
zhoulc
更改 文件过滤方法,适应windows
|
67
|
&& !file.getPath().contains("\\dist\\")
|
d9afb0e6
zhoulc
添加html.gz文件的过滤
|
68
69
|
&& !file.getPath().contains(".iml")
&& !file.getPath().contains(".html.gz"))
|
f651b86d
zhoulc
更改 文件过滤方法,适应windows
|
70
|
.collect(Collectors.toList());
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
71
72
73
74
|
return files;
}
private static String replaceFileContent(File file, String groupIdNew,
|
0bbc0349
YunaiV
优化 ProjectReactor...
|
75
76
|
String artifactIdNew, String packageNameNew,
String titleNew) {
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
77
78
79
80
|
return FileUtil.readString(file, StandardCharsets.UTF_8)
.replaceAll(GROUP_ID, groupIdNew)
.replaceAll(PACKAGE_NAME, packageNameNew)
.replaceAll(ARTIFACT_ID, artifactIdNew) // 必须放在最后替换,因为 ARTIFACT_ID 太短!
|
0bbc0349
YunaiV
优化 ProjectReactor...
|
81
82
|
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew))
.replaceAll(TITLE, titleNew);
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
83
84
85
86
87
|
}
private static void writeFile(File file, String fileContent, String projectBaseDir,
String projectBaseDirNew, String packageNameNew, String artifactIdNew) {
String newPath = file.getPath().replace(projectBaseDir, projectBaseDirNew) // 新目录
|
37900a73
zhoulc
修改一键改包的bug
|
88
89
|
.replace(PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator)),
packageNameNew.replaceAll("\\.", Matcher.quoteReplacement(File.separator)))
|
d6333fc3
YunaiV
增加 ProjectReactor...
|
90
91
92
93
|
.replace(ARTIFACT_ID, artifactIdNew) //
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew));
FileUtil.writeUtf8String(fileContent, newPath);
}
|
5ac7c3bd
YunaiV
code review Proje...
|
94
|
}
|