從零構(gòu)建可視化jar包部署平臺(tái)JarManage教程
項(xiàng)目背景
在java項(xiàng)目部署過程中,由于內(nèi)外部各種因素,可能會(huì)遇到一些感覺操作不便捷的場(chǎng)景,例如
- jar包未隨系統(tǒng)自動(dòng)啟動(dòng)需要每次手動(dòng)重啟
- 系統(tǒng)vpn堡壘機(jī)多重防御更新繁瑣
- 系統(tǒng)無圖形化界面命令行操作復(fù)雜
- 等等......
在工作中之前也總結(jié)了windows的Jar包部署工具與linux下的jar包自動(dòng)化部署腳本,這次就想著否能將二者統(tǒng)一結(jié)合,本著簡(jiǎn)單/高效/功能專一的原則,做出一
個(gè)可視化jar包部署平臺(tái),JarManage應(yīng)運(yùn)而生
功能介紹
項(xiàng)目地址:https://gitee.com/code2roc/jar-manage
支持在線創(chuàng)建項(xiàng)目,上傳Jar包,自動(dòng)備份,配置啟動(dòng)參數(shù),注冊(cè)系統(tǒng)服務(wù),查看啟動(dòng)日志等功能,具有以下優(yōu)點(diǎn)
- 基于servlet開發(fā),依賴簡(jiǎn)潔,部署包10MB左右
- 結(jié)合嵌入式tomcat一鍵部署,無外部容器依賴
- 使用h2db存儲(chǔ)數(shù)據(jù),無外部數(shù)據(jù)庫依賴
- 適配windows/linux平臺(tái),滿足多種環(huán)境
- 具體項(xiàng)目經(jīng)平臺(tái)部署后自動(dòng)注冊(cè)系統(tǒng)服務(wù),無需擔(dān)心服務(wù)器重啟
系統(tǒng)架構(gòu)圖如下

系統(tǒng)截圖展示


技術(shù)分析
平臺(tái)識(shí)別
首先通過系統(tǒng)os識(shí)別是windows平臺(tái)還是linux平臺(tái)
String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
platform = DepolyPlatform.Windows;
}通過system-release文件識(shí)別部分基于CentOS開發(fā)的Linux系統(tǒng)
String command = "cat /etc/system-release";
String result = CMDUtil.executeLinuxCommand(command);
if (result.startsWith("Red Hat")) {
platform = DepolyPlatform.LinuxRedHat;
} else if (result.startsWith("CentOS")) {
platform = DepolyPlatform.LinuxCentOS;
} else if (result.startsWith("openEuler")) {
platform = DepolyPlatform.LinuxOpenEuler;
}通過issue文件識(shí)別部分基于Ubuntu/Debian開發(fā)的Linux系統(tǒng)
command = "cat /etc/issue";
result = CMDUtil.executeLinuxCommand(command);
if (!StringUtil.isEmpty(result)) {
if (result.startsWith("Ubuntu")) {
platform = DepolyPlatform.LinuxUbuntu;
} else if (result.startsWith("Debian")) {
platform = DepolyPlatform.LinuxDebian;
}
}windows注冊(cè)服務(wù)
通過sc query命令判斷服務(wù)狀態(tài)

public String getStatus(String serviceName) {
String status = DepolyStatus.UnInstall;
try {
String command = "sc query " + serviceName;
String commandResultFilePath = CMDUtil.executeWindowCommandStoreFile(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath)));
String line = reader.readLine();
while (line != null) {
if (line.trim().startsWith("STATE")) {
if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("1"))
status = DepolyStatus.Stopped;
else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("2"))
status = DepolyStatus.Startting;
else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("3"))
status = DepolyStatus.Stopping;
else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("4"))
status = DepolyStatus.Running;
}
line = reader.readLine();
}
} catch (IOException e) {
LogUtil.error(e);
}
return status;
}通過winsw這個(gè)開源項(xiàng)目配置exe和xml文件將jar包注冊(cè)為windows服務(wù),項(xiàng)目地址:https://github.com/winsw/winsw/
linux注冊(cè)服務(wù)
通過systemctl status命令判斷服務(wù)狀態(tài)

public String getStatus(String serviceName) {
String status = DepolyStatus.UnInstall;
try {
String command = "systemctl status " + serviceName;
String commandResultFilePath = CMDUtil.executeLinuxCommandWithStore(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath)));
String line = reader.readLine();
while (line != null) {
if (line.trim().startsWith("Active")) {
if (line.trim().indexOf("inactive (dead)") > 0)
status = DepolyStatus.Stopped;
else if (line.trim().indexOf("active (running)") > 0)
status = DepolyStatus.Running;
else if (line.trim().indexOf("failed") > 0)
status = DepolyStatus.Stopped;
}
line = reader.readLine();
}
} catch (IOException e) {
LogUtil.error(e);
}
return status;
}通過拷貝service文件到systemd/system目錄下注冊(cè)linux服務(wù)
yml配置文件識(shí)別
- maven配置
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.26</version>
</dependency>- 配置文件
jarmanage: port: 8555 username: admin password: abcd@1234 backupcount: 5
- 工具類
public static String getConfigValue(String configName){
String configValue = "";
try{
Yaml yaml = new Yaml();
InputStream resourceAsStream = new FileInputStream(new File("resources"+File.separator+"application.yml"));
Map obj = yaml.load(resourceAsStream);
Map<String,Object> param = (Map) obj.get("jarmanage");
configValue = ConvertUtil.convert2String(param.get(configName));
}catch (Exception e){
LogUtil.error(e);
}
return configValue;
}h2database使用
- maven引用
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>- 工具類
public static Connection getConnection() throws Exception {
File file = new File("database");
Connection conn = DriverManager.getConnection("jdbc:h2:file:" + file.getAbsolutePath() + File.separator + "manage", "root", "abcd@1234");
return conn;
}
public static void executeSQL(String sql) {
try {
Connection conn = getConnection();
Statement stmt = conn.createStatement();
stmt.execute(sql);
stmt.close();
conn.close();
} catch (Exception e) {
LogUtil.error(e);
}
}servelt內(nèi)置tomcat打包
- maven引用
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.35</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>9.0.35</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.35</version>
</dependency>- 手動(dòng)啟動(dòng)
//啟動(dòng)tomcat服務(wù)
// 1.創(chuàng)建一個(gè)內(nèi)嵌的Tomcat
Tomcat tomcat = new Tomcat();
// 2.設(shè)置Tomcat端口
tomcat.setPort(8555);
// 3.設(shè)置工作目錄,tomcat需要使用這個(gè)目錄進(jìn)行寫一些東西
final String baseDir = "workspace" + File.separator;
tomcat.setBaseDir(baseDir);
tomcat.getHost().setAutoDeploy(false);
// 4. 設(shè)置webapp資源路徑
String webappDirLocation = "webapp" + File.separator;
StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
// 5. 設(shè)置上下文路每徑
String contextPath = "";
ctx.setPath(contextPath);
ctx.addLifecycleListener(new Tomcat.FixContextListener());
ctx.setName("jar-manage");
tomcat.getHost().addChild(ctx);
//6.啟動(dòng)
tomcat.getConnector();
tomcat.start();
tomcat.getServer().await();- 打包包含引用類庫,自定義配置xml,指定運(yùn)行class
<plugins>
<plugin>
<!-- 打包包含引用 -->
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<!-- 自定義配置 -->
<descriptor>package.xml</descriptor>
</descriptors>
<archive>
<manifest>
<!-- 運(yùn)行類 -->
<mainClass>com.code2roc.jarmanage.Application</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins><assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<!-- TODO: a jarjar format would be better -->
<id>depoly</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/webapp/</directory>
<outputDirectory>/webapp</outputDirectory>
<includes>
<include>**/**</include>
</includes>
</fileSet>以上就是從零構(gòu)建可視化jar包部署平臺(tái)JarManage的詳細(xì)內(nèi)容,更多關(guān)于從零構(gòu)建可視化jar包部署平臺(tái)JarManage的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于springboot中對(duì)sqlSessionFactoryBean的自定義
這篇文章主要介紹了springboot中對(duì)sqlSessionFactoryBean的自定義方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java?中很好用的數(shù)據(jù)結(jié)構(gòu)(你絕對(duì)沒用過)
今天跟大家介紹的就是?java.util.EnumMap,也是?java.util?包下面的一個(gè)集合類,同樣的也有對(duì)應(yīng)的的?java.util.EnumSet,對(duì)java數(shù)據(jù)結(jié)構(gòu)相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05
SpringBoot數(shù)據(jù)訪問的實(shí)現(xiàn)
本文主要介紹了SpringBoot數(shù)據(jù)訪問的實(shí)現(xiàn),引入各種xxxTemplate,xxxRepository來簡(jiǎn)化我們對(duì)數(shù)據(jù)訪問層的操作,感興趣的可以了解一下2023-11-11
java實(shí)現(xiàn)新浪微博Oauth接口發(fā)送圖片和文字的方法
這篇文章主要介紹了java實(shí)現(xiàn)新浪微博Oauth接口發(fā)送圖片和文字的方法,涉及java調(diào)用新浪微博Oauth接口的使用技巧,具有一定參考接借鑒價(jià)值,需要的朋友可以參考下2015-07-07
一文徹底弄懂spring?boot自動(dòng)配置的過程(推薦)
SpringBoot的自動(dòng)配置機(jī)制通過@SpringBootApplication注解作為起點(diǎn),結(jié)合@EnableAutoConfiguration和spring.factories文件,實(shí)現(xiàn)了基于類路徑依賴、環(huán)境配置和自定義代碼的智能化配置,感興趣的朋友跟隨小編一起看看吧2024-10-10
Spring MVC利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API詳解
這篇文章主要給大家介紹了關(guān)于在Spring MVC中利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Springboot 整合 Java DL4J 實(shí)現(xiàn)醫(yī)學(xué)影像診斷功能介紹
本文介紹如何利用SpringBoot整合Java Deeplearning4j實(shí)現(xiàn)醫(yī)學(xué)影像診斷功能,重點(diǎn)介紹了卷積神經(jīng)網(wǎng)絡(luò)在處理醫(yī)學(xué)影像中的應(yīng)用,以及如何進(jìn)行數(shù)據(jù)預(yù)處理、模型構(gòu)建、訓(xùn)練與預(yù)測(cè),提供了詳細(xì)的代碼實(shí)現(xiàn)和單元測(cè)試方法,目的是輔助醫(yī)生更準(zhǔn)確快速地進(jìn)行疾病診斷2024-10-10

