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);


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




  }


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


  private static Collection\<File\> listFiles(String projectBaseDir) {
  Collection\<File\> files = FileUtil.loopFiles(projectBaseDir);
  // 移除 IDEA、Git 自身的文件、Node 编译出来的文件
  files = files.stream()
  .filter(file -\> !file.getPath().contains(separator + "target" + separator)
  \&\& !file.getPath().contains(separator + "node_modules" + separator)
  \&\& !file.getPath().contains(separator + ".idea" + separator)
  \&\& !file.getPath().contains(separator + ".git" + separator)
  \&\& !file.getPath().contains(separator + "dist" + separator)
  \&\& !file.getPath().contains(".iml")
  \&\& !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() \> 0 ? FileTypeUtil.getType(file) : "";
  }




`}
`

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