使用IntelliJ IDEA开发工具,maven进行项目构建!
单模块的springboot项目部署
1、新建一个单模块的springboot项目,项目目录:
指定打包方式<package>jar</package>,这也是springboot默认的打包方式;
请务必配置生成插件plugin,如果没有配置的话,可以打包成功,但是,打包的jar文件执行时报错:XXX.jar 中没有主清单属性!
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
附上全部的pom文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.7.14</version> </parent> <packaging>jar</packaging> <groupId>com.tim</groupId> <artifactId>springbootsinglejar</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、使用maven的package命令进行打包,双击package:
打包成功后,将在src同级的target下生成XXX.jar包文件。
3、进入jar包所在的目录,执行命令:java -jar XXX.jar 即可运行
3.1 windows系统:
3.2 linux中部署,将jar包上传到linux服务器任意目录,同样执行 java -jar XXX.jar命令
[root@tim_liunx webapps]# java -jar springbootsinglejar-1.0.0.jar . ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )\___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.14) 2024-01-21 10:50:29.956 INFO 18385 --- [ main] com.tim.MainApp : Starting MainApp v1.0.0 using Java 11.0.12 on tim_liunx with PID 18385 (/usr/local/tomcat/webapps/springbootsinglejar-1.0.0.jar started by root in /usr/local/tomcat/webapps) 2024-01-21 10:50:29.970 INFO 18385 --- [ main] com.tim.MainApp : No active profile set, falling back to 1 default profile: "default" 2024-01-21 10:50:32.115 INFO 18385 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http) 2024-01-21 10:50:32.140 INFO 18385 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2024-01-21 10:50:32.141 INFO 18385 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.78] 2024-01-21 10:50:32.347 INFO 18385 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2024-01-21 10:50:32.347 INFO 18385 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2268 ms 2024-01-21 10:50:33.771 INFO 18385 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '' 2024-01-21 10:50:33.803 INFO 18385 --- [ main] com.tim.MainApp : Started MainApp in 4.998 seconds (JVM running for 7.871) 2024-01-21 10:56:13.431 INFO 18385 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2024-01-21 10:56:13.432 INFO 18385 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2024-01-21 10:56:13.433 INFO 18385 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms 单模块项目,jar包部署 单模块项目,jar包部署 单模块项目,jar包部署
在宿主机器中通过IP访问,注意,请确定linux服务器的8081端口已开放。
多模块的springboot项目部署
1、新建一个多模块的springboot项目,api模块为项目的主/启动模块,依赖common模块,项目目录:
2、在启动模块的pom中,配置生成插件plugin,还可以通过finalName属性指定jar包的文件名称
<build> <finalName>module_jar</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
在父项目中,配置打包方式为pom:<packaging>pom</packaging>
3、父项目中执行maven的 install命令,主/启动项目中,执行package,即可生成jar文件
4、在windwos中或linux运行java -jar XXX.jar 即可运行项目
Linux环境,将jar包程序封装成系统服务交付给系统systemctl
在/etc/systemd/system 目录新建文件: vim XXX.service 文件,内容模板如下,替换成自己的系统配置
[Unit] #替换你的jar包名称/应用名称 Description=module_jar After=syslog.target network.target [Service] #替换系统的用户名 User=root #替换系统组 Group=root Environment="JAVA_HOME=/usr/local/jdk11" #替换你的java命令路径及jar包绝对路径 ExecStart=/usr/local/jdk11/bin/java -jar /data/www/module_jar.jar ExecStop=/bin/kill -15 $MAINPID SuccessExitStatus=143 [Install] WantedBy=multi-user.target
然后,就可以通过
systemctl start module_jar 启动服务;
systemctl stop module_jar 停止服务;
systemctl status module_jar 查看服务状态;
systemctl enable module_jar.service 将服务跟随系统启动
systemctl deamon-reload 重新加载systemd