51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

SpringBoot项目修改器,一键替换项目坐标信息和package信息

使用代码一键修改项目的坐标信息和package信息。

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Sets;

import java.io.File; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Set; import java.util.regex.Matcher; import java.util.stream.Collectors;

import static java.io.File.separator;

/**

  • 项目修改器,一键替换 Maven 的 groupId、artifactId,项目的 package 等

  • 通过修改 groupIdNew、artifactIdNew、projectBaseDirNew 三个变量

  • @author 筱筱晶哥哥 */ @Slf4j public class ProjectReactor {

    private static final String GROUP_ID = "com.itjing.cloud"; private static final String ARTIFACT_ID = "xiaojingge-cloud"; private static final String PACKAGE_NAME = "com.itjing.cloud"; private static final String TITLE = "筱筱晶哥哥微服务";

    /**

    • 白名单文件,不进行重写,避免出问题 */ private static final Set<String> WHITE_FILE_TYPES = Sets.newHashSet( // 图片 "gif", "jpg", "svg", "png", // 字体 "eot", "woff2", "ttf", "woff", // IP 库 "xdb");

    public static void main(String[] args) { long start = System.currentTimeMillis(); String projectBaseDir = getProjectBaseDir(); log.info("[main][原项目路径改地址 ({})]", projectBaseDir);

     &lt;span class=&quot;hljs-comment&quot;&gt;// ========== 配置,需要你手动修改 ==========&lt;/span&gt;
     &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;groupIdNew&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;com.itjing.demo&quot;&lt;/span&gt;;
     &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;artifactIdNew&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;springboot-demo&quot;&lt;/span&gt;;
     &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;packageNameNew&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;com.itjing.demo&quot;&lt;/span&gt;;
     &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;titleNew&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;SpringBoot测试demo&quot;&lt;/span&gt;;
     &lt;span class=&quot;hljs-comment&quot;&gt;// 一键改名后,&quot;新&quot;项目所在的目录&lt;/span&gt;
     &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;projectBaseDirNew&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; projectBaseDir + &lt;span class=&quot;hljs-string&quot;&gt;&quot;-new&quot;&lt;/span&gt;;
     log.info(&lt;span class=&quot;hljs-string&quot;&gt;&quot;[main][检测新项目目录 ({})是否存在]&quot;&lt;/span&gt;, projectBaseDirNew);
     &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (FileUtil.exist(projectBaseDirNew)) {
         log.error(&lt;span class=&quot;hljs-string&quot;&gt;&quot;[main][新项目目录检测 ({})已存在,请更改新的目录!程序退出]&quot;&lt;/span&gt;, projectBaseDirNew);
         &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;;
     }
     &lt;span class=&quot;hljs-comment&quot;&gt;// 如果新目录中存在 PACKAGE_NAME,ARTIFACT_ID 等关键字,路径会被替换,导致生成的文件不在预期目录&lt;/span&gt;
     &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (StrUtil.containsAny(projectBaseDirNew, PACKAGE_NAME, ARTIFACT_ID, StrUtil.upperFirst(ARTIFACT_ID))) {
         log.error(&lt;span class=&quot;hljs-string&quot;&gt;&quot;[main][新项目目录 `projectBaseDirNew` 检测 ({}) 存在冲突名称「{}」或者「{}」或者「{}」,请更改新的目录!程序退出]&quot;&lt;/span&gt;,
                 projectBaseDirNew, PACKAGE_NAME, ARTIFACT_ID, StrUtil.upperFirst(ARTIFACT_ID));
         &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;;
     }
     log.info(&lt;span class=&quot;hljs-string&quot;&gt;&quot;[main][完成新项目目录检测,新项目路径地址 ({})]&quot;&lt;/span&gt;, projectBaseDirNew);
     &lt;span class=&quot;hljs-comment&quot;&gt;// 获得需要复制的文件&lt;/span&gt;
     log.info(&lt;span class=&quot;hljs-string&quot;&gt;&quot;[main][开始获得需要重写的文件,预计需要 10-20 秒]&quot;&lt;/span&gt;);
     Collection&amp;lt;File&amp;gt; files = listFiles(projectBaseDir);
     log.info(&lt;span class=&quot;hljs-string&quot;&gt;&quot;[main][需要重写的文件数量:{},预计需要 15-30 秒]&quot;&lt;/span&gt;, files.size());
     &lt;span class=&quot;hljs-comment&quot;&gt;// 写入文件&lt;/span&gt;
     files.forEach(file -&amp;gt; {
         &lt;span class=&quot;hljs-comment&quot;&gt;// 如果是白名单的文件类型,不进行重写,直接拷贝&lt;/span&gt;
         &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;fileType&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; getFileType(file);
         &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (WHITE_FILE_TYPES.contains(fileType)) {
             copyFile(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
             &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;;
         }
         &lt;span class=&quot;hljs-comment&quot;&gt;// 如果非白名单的文件类型,重写内容,在生成文件&lt;/span&gt;
         &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; replaceFileContent(file, groupIdNew, artifactIdNew, packageNameNew, titleNew);
         writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
     });
     log.info(&lt;span class=&quot;hljs-string&quot;&gt;&quot;[main][重写完成]共耗时:{} 秒&quot;&lt;/span&gt;, (System.currentTimeMillis() - start) / &lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;);
    

    }

    private static String getProjectBaseDir() { String baseDir = System.getProperty("user.dir"); if (StrUtil.isEmpty(baseDir)) { throw new NullPointerException("项目基础路径不存在"); } return baseDir; }

    private static Collection&lt;File&gt; listFiles(String projectBaseDir) { Collection&lt;File&gt; files = FileUtil.loopFiles(projectBaseDir); // 移除 IDEA、Git 自身的文件、Node 编译出来的文件 files = files.stream() .filter(file -&gt; !file.getPath().contains(separator + "target" + separator) &amp;&amp; !file.getPath().contains(separator + "node_modules" + separator) &amp;&amp; !file.getPath().contains(separator + ".idea" + separator) &amp;&amp; !file.getPath().contains(separator + ".git" + separator) &amp;&amp; !file.getPath().contains(separator + "dist" + separator) &amp;&amp; !file.getPath().contains(".iml") &amp;&amp; !file.getPath().contains(".html.gz")) .collect(Collectors.toList()); return files; }

    private static String replaceFileContent(File file, String groupIdNew, String artifactIdNew, String packageNameNew, String titleNew) { String content = FileUtil.readString(file, StandardCharsets.UTF_8); // 如果是白名单的文件类型,不进行重写 String fileType = getFileType(file); if (WHITE_FILE_TYPES.contains(fileType)) { return content; } // 执行文件内容都重写 return content.replaceAll(GROUP_ID, groupIdNew) .replaceAll(PACKAGE_NAME, packageNameNew) // 必须放在最后替换,因为 ARTIFACT_ID 太短! .replaceAll(ARTIFACT_ID, artifactIdNew) .replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)) .replaceAll(TITLE, titleNew); }

    private static void writeFile(File file, String fileContent, String projectBaseDir, String projectBaseDirNew, String packageNameNew, String artifactIdNew) { String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); FileUtil.writeUtf8String(fileContent, newPath); }

    private static void copyFile(File file, String projectBaseDir, String projectBaseDirNew, String packageNameNew, String artifactIdNew) { String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); FileUtil.copyFile(file, new File(newPath)); }

    private static String buildNewFilePath(File file, String projectBaseDir, String projectBaseDirNew, String packageNameNew, String artifactIdNew) { // 新目录 return file.getPath().replace(projectBaseDir, projectBaseDirNew) .replace(PACKAGE_NAME.replaceAll(".", Matcher.quoteReplacement(separator)), packageNameNew.replaceAll(".", Matcher.quoteReplacement(separator))) .replace(ARTIFACT_ID, artifactIdNew) .replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)); }

    private static String getFileType(File file) { return file.length() &gt; 0 ? FileTypeUtil.getType(file) : ""; }

}


赞(6)
未经允许不得转载:工具盒子 » SpringBoot项目修改器,一键替换项目坐标信息和package信息