package com.ks.codegenerator.utils; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.yeshi.utils.FileUtil; import org.yeshi.utils.StringUtil; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @author hxh * @title: ServiceUtil * @description: TODO * @date 2021/11/13 14:50 */ public class ServiceBuilder { private String name; private String pks; private String subpks; private String cacheDir; public ServiceBuilder setName(String name) { this.name = name; return this; } public ServiceBuilder setPks(String pks) { this.pks = pks; return this; } public ServiceBuilder setSubpks(String subpks) { this.subpks = subpks; return this; } public ServiceBuilder setCacheDir(String cacheDir) { this.cacheDir = cacheDir; return this; } /** * @return void * @author hxh * @description 生成服务 * @date 14:51 2021/11/13 **/ public String build() throws Exception { String destPath = String.format("%s/%s/%s", cacheDir, System.currentTimeMillis() + "", name); System.out.println(destPath); //copy文件夹 FileUtils.copyFileDir(ServiceBuilder.class.getClassLoader().getResource("code/service").getPath(), destPath); //删除target if (new File(destPath + "/target").exists()) { FileUtil.deleteFileDir(new File(destPath + "/target")); } if (new File(destPath + "/app/target").exists()) { FileUtil.deleteFileDir(new File(destPath + "/app/target")); } //修改文件名称与文件夹名称 renameDir(destPath); //重新命名包名 renamePackage(destPath + "/app/src/main/java/" + subpks.replace(".", "/")); replaceSettings(destPath); //压缩文件夹 File zip = new File(new File(destPath).getParent(), new File(destPath).getName() + ".zip"); ZipUtil.compressToZip(destPath, zip.getParent(), zip.getName()); //删除原来的文件夹 FileUtil.deleteFileDir(new File(destPath)); return zip.getAbsolutePath(); } private void replaceSettings(String path) throws Exception { FileUtils.replaceFileContent(path + "/pom.xml", "com.ks", String.format("%s", getGroupId(pks))); FileUtils.replaceFileContent(path + "/pom.xml", "demo", String.format("%s", getArtifactId(pks))); //更改parent以及artifactId DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(path + "/app/pom.xml"); NodeList list = doc.getElementsByTagName("project").item(0).getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if (list.item(i).getNodeName().equalsIgnoreCase("parent")) { NodeList parent = list.item(i).getChildNodes(); for (int j = 0; j < parent.getLength(); j++) { Node node = parent.item(j); switch (node.getNodeName()) { case "artifactId": node.setTextContent(getArtifactId(pks)); break; case "groupId": node.setTextContent(getGroupId(pks)); break; } } } else if (list.item(i).getNodeName().equalsIgnoreCase("artifactId")) { list.item(i).setTextContent(getArtifactId(subpks)); } } //保存 Transformer ts = TransformerFactory.newInstance().newTransformer(); ts.transform(new DOMSource(doc), new StreamResult(path + "/app/pom.xml")); FileUtils.replaceFileContent(path + "/app/src/main/resources/application-dev.yml", "com.ks.app", subpks); FileUtils.replaceFileContent(path + "/app/src/main/resources/application-pro.yml", "com.ks.app", subpks); } //重新命名文件夹与文件 private void renameDir(String path) throws Exception { FileUtils.renameFile(path + "/service.iml", name + ".iml"); //更改java文件的路径 FileUtils.copyFileDir(path + "/app/src/main/java/com/ks/app", path + "/app/src/main/java/" + subpks.replace(".", "/")); FileUtil.deleteFileDir(new File(path + "/app/src/main/java/com/ks/app")); } //重新命名包名 private void renamePackage(String path) throws Exception { if (!new File(path).exists()) { throw new Exception("文件不存在"); } if (new File(path).isDirectory()) { File[] fs = new File(path).listFiles(); for (File f : fs) { if (f.isFile()) { FileUtils.replaceFileContent(f.getPath(), "package com.ks.app", "package " + subpks); FileUtils.replaceFileContent(f.getPath(), "import com.ks.app.", "import " + subpks + "."); } else { renamePackage(f.getPath()); } } } } private static String getGroupId(String pks) { String[] sts = pks.split("\\."); List list = new ArrayList<>(); for (int i = 0; i < sts.length - 1; i++) { list.add(sts[i]); } return StringUtil.concat(list, "."); } private static String getArtifactId(String pks) { String[] sts = pks.split("\\."); return sts[sts.length - 1]; } public static void main(String[] args) throws Exception { System.out.println(getGroupId("com.ks.app")); } }