From 63570d2b8d224d3ca0f56e735cf9b3ed276b3ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Wed, 16 Oct 2024 09:06:10 +0800 Subject: [PATCH] 'commit' --- dsBase/dependency-reduced-pom.xml | 85 ----------------- .../com/dsideal/base/Util/CommonUtil.java | 2 +- dsGw/pom.xml | 50 ++++++++++ .../java/com/dsideal/gw/GwApplication.java | 9 +- .../java/com/dsideal/gw/Util/CommonUtil.java | 94 +++++++++++++++++-- dsGw/src/main/resources/package.xml | 67 +++++++++++++ dsRes/pom.xml | 29 ++++++ .../com/dsideal/resource/ResApplication.java | 26 +++-- .../com/dsideal/resource/Util/CommonUtil.java | 83 ++++++++++++++++ dsRes/src/main/resources/package.xml | 67 +++++++++++++ 10 files changed, 400 insertions(+), 112 deletions(-) delete mode 100644 dsBase/dependency-reduced-pom.xml create mode 100644 dsGw/src/main/resources/package.xml create mode 100644 dsRes/src/main/resources/package.xml diff --git a/dsBase/dependency-reduced-pom.xml b/dsBase/dependency-reduced-pom.xml deleted file mode 100644 index 8f0e462f..00000000 --- a/dsBase/dependency-reduced-pom.xml +++ /dev/null @@ -1,85 +0,0 @@ - - - - dsProject - com.dsideal - 1.0 - - 4.0.0 - dsBase - dsBase - 1.0 - - - - src/main/java - - **/*.sql - **/*.class - - - - src/main/resources - - - dsRes - - - maven-compiler-plugin - 3.6.1 - - 21 - 21 - UTF-8 - -parameters - - - - maven-shade-plugin - 3.2.1 - - - package - - shade - - - dsRes - - - com.demo.common.DemoConfig - - - - - - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - - - - - - - - - - - org.projectlombok - lombok - 1.18.34 - provided - - - - 21 - 21 - UTF-8 - - diff --git a/dsBase/src/main/java/com/dsideal/base/Util/CommonUtil.java b/dsBase/src/main/java/com/dsideal/base/Util/CommonUtil.java index 3b3e67ab..f7195ab0 100644 --- a/dsBase/src/main/java/com/dsideal/base/Util/CommonUtil.java +++ b/dsBase/src/main/java/com/dsideal/base/Util/CommonUtil.java @@ -81,7 +81,7 @@ public class CommonUtil { public static List getAllSql() throws IOException { List list = new ArrayList<>(); if (isRunInJar()) { - URL url = BaseApplication.class.getClassLoader().getResource("Sql/"); + URL url = CommonUtil.class.getClassLoader().getResource("Sql/"); String jarPath = url.toString().substring(0, url.toString().indexOf("!/") + 2); URL jarURL = new URL(jarPath); JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection(); diff --git a/dsGw/pom.xml b/dsGw/pom.xml index 7a2b432c..c547ed4e 100644 --- a/dsGw/pom.xml +++ b/dsGw/pom.xml @@ -118,4 +118,54 @@ provided + + + dsBase + + + + src/main/resources + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 21 + 21 + UTF-8 + + -parameters + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + src/main/resources/package.xml + + + + com.dsideal.base.BaseApplication + + + + + + make-assembly + package + + single + + + + + + diff --git a/dsGw/src/main/java/com/dsideal/gw/GwApplication.java b/dsGw/src/main/java/com/dsideal/gw/GwApplication.java index e9391a5b..70b91bfb 100644 --- a/dsGw/src/main/java/com/dsideal/gw/GwApplication.java +++ b/dsGw/src/main/java/com/dsideal/gw/GwApplication.java @@ -1,17 +1,17 @@ package com.dsideal.gw; -import cn.hutool.core.io.FileUtil; import com.dsideal.gw.Contoller.IndexController; import com.dsideal.gw.Handler.RouterHandler; import com.dsideal.gw.Plugin.YamlProp; +import com.dsideal.gw.Util.CommonUtil; import com.dsideal.gw.Util.LogBackLogFactory; import com.jfinal.config.*; import com.jfinal.kit.Prop; import com.jfinal.server.undertow.UndertowServer; import com.jfinal.template.Engine; +import lombok.SneakyThrows; import org.yaml.snakeyaml.Yaml; -import java.io.File; import java.util.*; public class GwApplication extends JFinalConfig { @@ -110,11 +110,10 @@ public class GwApplication extends JFinalConfig { /** * 在jfinal启动完成后马上执行 */ + @SneakyThrows @Override public void onStart() { //打印 启动Logo - String path = Objects.requireNonNull(GwApplication.class.getClassLoader().getResource("logo.txt")).getPath(); - File file = new File(path); - System.out.println(FileUtil.readUtf8String(file)); + System.out.println(CommonUtil.txt2String("logo.txt")); } } diff --git a/dsGw/src/main/java/com/dsideal/gw/Util/CommonUtil.java b/dsGw/src/main/java/com/dsideal/gw/Util/CommonUtil.java index 1585b2a1..54434039 100644 --- a/dsGw/src/main/java/com/dsideal/gw/Util/CommonUtil.java +++ b/dsGw/src/main/java/com/dsideal/gw/Util/CommonUtil.java @@ -1,23 +1,103 @@ package com.dsideal.gw.Util; -import com.alibaba.fastjson.JSONObject; -import com.jfinal.kit.StrKit; -import com.jfinal.plugin.activerecord.Page; -import com.jfinal.plugin.activerecord.Record; -import org.apache.commons.codec.binary.Base64; +import com.jfinal.kit.PathKit; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.JarURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; @SuppressWarnings("unchecked") public class CommonUtil { //在独立的main函数中,使用下面的方式进行声明logback对象 private static final Logger log = LoggerFactory.getLogger(CommonUtil.class); + /** + * 判断当前项目是否在jar包中 + * + * @return 是/否 + */ + public static boolean isRunInJar() { + String resourcePath = "/" + CommonUtil.class.getPackageName().replace(".", "/"); + URL url = CommonUtil.class.getResource(resourcePath); + if (url.getProtocol().equals("jar")) { + return true; + } + return false; + } + /** + * 获取文本文件内容,兼容jar包与文件系统两种情况 + * + * @param path 文件路径 + * @return 文件内容 + * @throws IOException + */ + public static String txt2String(String path) throws IOException { + InputStream is = CommonUtil.class.getClassLoader().getResourceAsStream(path); + BufferedInputStream bis = null; + StringBuilder sb = new StringBuilder(); + try { + bis = new BufferedInputStream(is); + byte[] temp = new byte[1024]; + int len; + while ((len = bis.read(temp)) != -1) { + sb.append(new String(temp, 0, len, StandardCharsets.UTF_8)); + } + } finally { + if (bis != null) { + bis.close(); + } + if (is != null) { + is.close(); + } + } + return sb.toString(); + } + + /** + * 获取所有sql文件,并返回一个list集合 + * + * @return + */ + public static List getAllSql() throws IOException { + List list = new ArrayList<>(); + if (isRunInJar()) { + URL url = CommonUtil.class.getClassLoader().getResource("Sql/"); + String jarPath = url.toString().substring(0, url.toString().indexOf("!/") + 2); + URL jarURL = new URL(jarPath); + JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection(); + JarFile jarFile = jarCon.getJarFile(); + Enumeration jarEntrys = jarFile.entries(); + while (jarEntrys.hasMoreElements()) { + JarEntry entry = jarEntrys.nextElement(); + String name = entry.getName(); + if (name.startsWith("Sql/") && !entry.isDirectory()) { + list.add(name); + } + } + } else {//如果运行在文件系统中,直接加载sql文件 + //遍历sql目录下所有的sql文件 + File sqlDir; + String basePath = PathKit.getRootClassPath(); + sqlDir = new File(basePath + "/Sql"); + File[] sqlFiles = sqlDir.listFiles(); + for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) { + if (sqlFile.getName().indexOf(".sql") > 0) {//只加载.sql文件 + list.add("Sql/"+sqlFile.getName()); + } + } + } + return list; + } /** * 加签 diff --git a/dsGw/src/main/resources/package.xml b/dsGw/src/main/resources/package.xml new file mode 100644 index 00000000..b759bd85 --- /dev/null +++ b/dsGw/src/main/resources/package.xml @@ -0,0 +1,67 @@ + + + + bin + + + zip + + + + + + + false + + lib + + false + + + + + + + + + ${project.basedir}/src/main/resources/${env} + /config + + + + + *.* + + + + + + ${project.basedir}/src/main/resources/${env} + + + scf*.xml + + + + + + ${project.build.directory} + + + *.jar + + + + + + ${project.build.directory}/jar + + + lib/*.jar + + + *.jar + + + + \ No newline at end of file diff --git a/dsRes/pom.xml b/dsRes/pom.xml index a643bfb2..bdc443ec 100644 --- a/dsRes/pom.xml +++ b/dsRes/pom.xml @@ -266,11 +266,15 @@ + + dsBase + src/main/resources + org.apache.maven.plugins @@ -284,6 +288,31 @@ -parameters + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + src/main/resources/package.xml + + + + com.dsideal.base.BaseApplication + + + + + + make-assembly + package + + single + + + + \ No newline at end of file diff --git a/dsRes/src/main/java/com/dsideal/resource/ResApplication.java b/dsRes/src/main/java/com/dsideal/resource/ResApplication.java index 7043e5dd..bad4f7c2 100644 --- a/dsRes/src/main/java/com/dsideal/resource/ResApplication.java +++ b/dsRes/src/main/java/com/dsideal/resource/ResApplication.java @@ -4,6 +4,7 @@ import com.dsideal.resource.Base.Controller.BaseController; import com.dsideal.resource.Index.IndexController; import com.dsideal.resource.Interceptor.*; import com.dsideal.resource.Plugin.YamlProp; +import com.dsideal.resource.Util.CommonUtil; import com.dsideal.resource.Util.FileUtil; import com.dsideal.resource.Util.LogBackLogFactory; import com.jfinal.config.*; @@ -15,8 +16,10 @@ import com.jfinal.plugin.hikaricp.HikariCpPlugin; import com.jfinal.plugin.redis.RedisPlugin; import com.jfinal.server.undertow.UndertowServer; import com.jfinal.template.Engine; +import lombok.SneakyThrows; import java.io.File; +import java.util.List; public class ResApplication extends JFinalConfig { @@ -79,6 +82,7 @@ public class ResApplication extends JFinalConfig { // 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生 SQLException, 缺省:30秒 private long connectionTimeoutMs = 30000; + @SneakyThrows @Override public void configPlugin(Plugins me) { HikariCpPlugin masterPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"), @@ -107,18 +111,13 @@ public class ResApplication extends JFinalConfig { ActiveRecordPlugin slaveArp = new ActiveRecordPlugin("slave", slavePlugin); slaveArp.setDialect(new MysqlDialect()); - //遍历sql目录下所有的sql文件 - File sqlDir; - String basePath = ResApplication.class.getResource("/").getPath(); - sqlDir = new File(basePath + "/Sql"); - File[] sqlFiles = sqlDir.listFiles(); - for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) { - //只加载.sql文件 - if (sqlFile.getName().indexOf(".sql") > 0) { - masterArp.addSqlTemplate("/Sql/" + sqlFile.getName()); - slaveArp.addSqlTemplate("/Sql/" + sqlFile.getName()); - } + //加载所有的sql文件 + List list = CommonUtil.getAllSql(); + for (String sqlFile : list) { + masterArp.addSqlTemplate(sqlFile); + slaveArp.addSqlTemplate(sqlFile); } + //加载 me.add(masterArp); me.add(slaveArp); @@ -168,11 +167,10 @@ public class ResApplication extends JFinalConfig { /** * 在jfinal启动完成后马上执行 */ + @SneakyThrows @Override public void onStart() { //打印 启动Logo - String path = ResApplication.class.getClassLoader().getResource("logo.txt").getPath(); - File file = new File(path); - System.out.println(FileUtil.txt2String(file)); + System.out.println(CommonUtil.txt2String("logo.txt")); } } diff --git a/dsRes/src/main/java/com/dsideal/resource/Util/CommonUtil.java b/dsRes/src/main/java/com/dsideal/resource/Util/CommonUtil.java index c24ba5dd..fc84e1c5 100644 --- a/dsRes/src/main/java/com/dsideal/resource/Util/CommonUtil.java +++ b/dsRes/src/main/java/com/dsideal/resource/Util/CommonUtil.java @@ -2,6 +2,7 @@ package com.dsideal.resource.Util; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.jfinal.kit.PathKit; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.Page; import com.jfinal.plugin.activerecord.Record; @@ -11,9 +12,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; +import java.net.JarURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -21,7 +27,84 @@ import java.util.regex.Pattern; public class CommonUtil { //在独立的main函数中,使用下面的方式进行声明logback对象 private static final Logger log = LoggerFactory.getLogger(CommonUtil.class); + /** + * 判断当前项目是否在jar包中 + * + * @return 是/否 + */ + public static boolean isRunInJar() { + String resourcePath = "/" + CommonUtil.class.getPackageName().replace(".", "/"); + URL url = CommonUtil.class.getResource(resourcePath); + if (url.getProtocol().equals("jar")) { + return true; + } + return false; + } + /** + * 获取文本文件内容,兼容jar包与文件系统两种情况 + * + * @param path 文件路径 + * @return 文件内容 + * @throws IOException + */ + public static String txt2String(String path) throws IOException { + InputStream is = CommonUtil.class.getClassLoader().getResourceAsStream(path); + BufferedInputStream bis = null; + StringBuilder sb = new StringBuilder(); + try { + bis = new BufferedInputStream(is); + byte[] temp = new byte[1024]; + int len; + while ((len = bis.read(temp)) != -1) { + sb.append(new String(temp, 0, len, StandardCharsets.UTF_8)); + } + } finally { + if (bis != null) { + bis.close(); + } + if (is != null) { + is.close(); + } + } + return sb.toString(); + } + + /** + * 获取所有sql文件,并返回一个list集合 + * + * @return + */ + public static List getAllSql() throws IOException { + List list = new ArrayList<>(); + if (isRunInJar()) { + URL url = CommonUtil.class.getClassLoader().getResource("Sql/"); + String jarPath = url.toString().substring(0, url.toString().indexOf("!/") + 2); + URL jarURL = new URL(jarPath); + JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection(); + JarFile jarFile = jarCon.getJarFile(); + Enumeration jarEntrys = jarFile.entries(); + while (jarEntrys.hasMoreElements()) { + JarEntry entry = jarEntrys.nextElement(); + String name = entry.getName(); + if (name.startsWith("Sql/") && !entry.isDirectory()) { + list.add(name); + } + } + } else {//如果运行在文件系统中,直接加载sql文件 + //遍历sql目录下所有的sql文件 + File sqlDir; + String basePath = PathKit.getRootClassPath(); + sqlDir = new File(basePath + "/Sql"); + File[] sqlFiles = sqlDir.listFiles(); + for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) { + if (sqlFile.getName().indexOf(".sql") > 0) {//只加载.sql文件 + list.add("Sql/"+sqlFile.getName()); + } + } + } + return list; + } /** * 功能:删除指定目录下所有的文件 diff --git a/dsRes/src/main/resources/package.xml b/dsRes/src/main/resources/package.xml new file mode 100644 index 00000000..b759bd85 --- /dev/null +++ b/dsRes/src/main/resources/package.xml @@ -0,0 +1,67 @@ + + + + bin + + + zip + + + + + + + false + + lib + + false + + + + + + + + + ${project.basedir}/src/main/resources/${env} + /config + + + + + *.* + + + + + + ${project.basedir}/src/main/resources/${env} + + + scf*.xml + + + + + + ${project.build.directory} + + + *.jar + + + + + + ${project.build.directory}/jar + + + lib/*.jar + + + *.jar + + + + \ No newline at end of file