Skip to content

Commit

Permalink
数据库文件改为zip压缩,精简了项目依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashuaizhang committed Dec 20, 2021
1 parent 564b364 commit bb4fae5
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 deletions.
16 changes: 1 addition & 15 deletions yyets-history/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand Down Expand Up @@ -53,16 +49,6 @@
<artifactId>hutool-all</artifactId>
<version>5.7.17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
Expand Down Expand Up @@ -110,7 +96,7 @@
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>static/yyets.7z</exclude>
<exclude>static/yyets.zip</exclude>
</excludes>
</resource>
</resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import static com.zhangjiashuai.yyetshistory.util.NativeOperationUtils.onStartFinish;
import static com.zhangjiashuai.yyetshistory.util.NativeOperationUtils.onStartPrepare;

@SpringBootApplication
Expand All @@ -12,7 +11,6 @@ public class YyetsHistoryApplication {
public static void main(String[] args) {
onStartPrepare(args);
SpringApplication.run(YyetsHistoryApplication.class, args);
onStartFinish();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.extra.compress.extractor.SevenZExtractor;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.spring.EnableSpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
Expand All @@ -16,7 +16,6 @@
import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardCopyOption;


@Slf4j
Expand All @@ -32,12 +31,12 @@ public YyetsHistoryProperties yyetsHistoryConfig() {

@Bean
public DataSource dataSource(DataSourceProperties properties) {
extractDb7zFile();
uncompressDbFile();
return DataSourceBuilder.create().driverClassName(properties.getDriverClassName())
.url(properties.getUrl()).build();
}

private void extractDb7zFile() {
private void uncompressDbFile() {
URL dbResource = getClass().getResource("/static/yyets.db");
if (dbResource != null) {
log.info("检测到数据源文件: {}", URLDecoder.decode(dbResource.getPath(), StandardCharsets.UTF_8));
Expand All @@ -50,30 +49,29 @@ private void extractDb7zFile() {
projectPath = projectPath + moduleName;
}
String staticResourcePath = projectPath + "/src/main/resources/static/";
String dbFilePath = staticResourcePath + "yyets.db";
File dbFile = new File(dbFilePath);
File dbFile = new File(staticResourcePath + "yyets.db");
if (dbFile.exists()) {
throw new IllegalStateException("请重新编译项目");
}
String db7zFilePath = staticResourcePath + "yyets.7z";
File db7zFile = new File(db7zFilePath);
if (!db7zFile.exists()) {
throw new IORuntimeException("数据源文件缺失并且无法找到压缩包: " + db7zFilePath);
String zipFilePath = staticResourcePath + "yyets.zip";
File zipFile = new File(staticResourcePath + "yyets.zip");
if (!zipFile.exists()) {
throw new IORuntimeException("数据源文件缺失并且无法找到压缩包: " + zipFilePath);
}
log.info("开始解压数据源文件: {}", db7zFilePath);
try (SevenZExtractor sevenZExtractor = new SevenZExtractor(db7zFile)) {
sevenZExtractor.extract(new File(staticResourcePath));
log.info("开始解压数据源文件: {}", zipFilePath);
try {
ZipUtil.unzip(zipFile, new File(staticResourcePath));
if (dbFile.exists()) {
log.info("数据源文件自动解压完成: {}", dbFilePath);
log.info("数据源文件自动解压完成: {}", dbFile.getPath());
String classPath = getClass().getResource("/static").getPath();
classPath = URLDecoder.decode(classPath, StandardCharsets.UTF_8);
FileUtil.copyFile(dbFile, new File(classPath), StandardCopyOption.REPLACE_EXISTING);
FileUtil.copyFile(dbFile, new File(classPath));
log.info("复制数据源文件至classpath: {}", classPath);
} else {
throw new IORuntimeException("未知的解压异常: " + db7zFilePath);
throw new IORuntimeException("未知的解压异常: " + zipFilePath);
}
} catch (Exception e) {
String msg = "数据源文件自动解压失败,请手动解压文件: resources/static/yyets.7z";
String msg = "数据源文件自动解压失败,请手动解压文件: " + zipFilePath;
throw new IORuntimeException(msg, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.zhangjiashuai.yyetshistory.config;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import static com.zhangjiashuai.yyetshistory.util.NativeOperationUtils.onStartFinish;

@Configuration
public class AppRunner implements ApplicationRunner {

@Override
public void run(ApplicationArguments args) {
onStartFinish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void loadCommandLineArgs(String[] args) {

static String getHost() {
String host = StrUtil.toStringOrNull(getProperty("yyets-history.host"));
return StrUtil.isNotBlank(host) ? StrUtil.trim(host) : DEFAULT_HOST;
return StrUtil.isNotBlank(host) ? host.trim() : DEFAULT_HOST;
}

static int getPort() {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.swing.DesktopUtil;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.hutool.extra.compress.extractor.SevenZExtractor;
import cn.hutool.core.util.ZipUtil;
import com.zhangjiashuai.yyetshistory.util.NativeOperationUtils;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -48,16 +48,18 @@ public void testLinkedHashSet() {
}

@Test
public void testExtract7z() {
public void testExtractZip() {
String projectPath = System.getProperty("user.dir");
String staticResourcePath = projectPath + "/src/main/resources/static/";
String db7zFilename = "yyets.7z";
File db7zFile = new File(staticResourcePath + db7zFilename);
System.out.println(db7zFile.exists());
try (SevenZExtractor sevenZExtractor = new SevenZExtractor(db7zFile)) {
sevenZExtractor.extract(new File(staticResourcePath));
}
String dbZipFilename = "yyets.zip";
File dbZipFile = new File(staticResourcePath + dbZipFilename);
Assert.isTrue(dbZipFile.exists());
File dbFile = new File(staticResourcePath + "yyets.db");
if(!dbFile.exists()) {
long start = System.currentTimeMillis();
ZipUtil.unzip(dbZipFile, new File(staticResourcePath));
System.out.println("解压完成,耗时: " + (System.currentTimeMillis() - start));
}
Assert.isTrue(dbFile.exists());
}

Expand Down

0 comments on commit bb4fae5

Please sign in to comment.