Python 官方文档:入门教程 => 点击学习
目录项目背景功能介绍技术分析平台识别windows注册服务linux注册服务yml配置文件识别h2database使用servelt内置Tomcat打包项目背景 在java项目部署过
在java项目部署过程中,由于内外部各种因素,可能会遇到一些感觉操作不便捷的场景,例如
在工作中之前也总结了windows的Jar包部署工具与linux下的jar包自动化部署脚本,这次就想着否能将二者统一结合,本着简单/高效/功能专一的原则,做出一
个可视化jar包部署平台,JarManage应运而生
项目地址:https://gitee.com/code2roc/jar-manage
支持在线创建项目,上传Jar包,自动备份,配置启动参数,注册系统服务,查看启动日志等功能,具有以下优点
系统架构图如下
系统截图展示
首先通过系统os识别是windows平台还是linux平台
String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
platfORM = DepolyPlatform.Windows;
}
通过system-release文件识别部分基于Centos开发的Linux系统
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文件识别部分基于ubuntu/Debian开发的Linux系统
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;
}
}
通过sc query命令判断服务状态
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这个开源项目配置exe和xml文件将jar包注册为windows服务,项目地址:Https://GitHub.com/winsw/winsw/
通过systemctl status命令判断服务状态
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目录下注册linux服务
<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;
}
<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);
}
}
<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>
//启动tomcat服务
// 1.创建一个内嵌的Tomcat
Tomcat tomcat = new Tomcat();
// 2.设置Tomcat端口
tomcat.setPort(8555);
// 3.设置工作目录,tomcat需要使用这个目录进行写一些东西
final String baseDir = "workspace" + File.separator;
tomcat.setBaseDir(baseDir);
tomcat.getHost().setAutoDeploy(false);
// 4. 设置WEBapp资源路径
String webappDirLocation = "webapp" + File.separator;
StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
// 5. 设置上下文路每径
String contextPath = "";
ctx.setPath(contextPath);
ctx.addLifecycleListener(new Tomcat.FixContextListener());
ctx.setName("jar-manage");
tomcat.getHost().addChild(ctx);
//6.启动
tomcat.getConnector();
tomcat.start();
tomcat.getServer().await();
<plugins>
<plugin>
<!-- 打包包含引用 -->
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<!-- 自定义配置 -->
<descriptor>package.xml</descriptor>
</descriptors>
<arcHive>
<manifest>
<!-- 运行类 -->
<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>
以上就是从零构建可视化jar包部署平台JarManage的详细内容,更多关于从零构建可视化jar包部署平台JarManage的资料请关注编程网其它相关文章!
--结束END--
本文标题: 从零构建可视化jar包部署平台JarManage教程
本文链接: https://lsjlt.com/news/214289.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0