描述
spring boot是javaWeb框架中最流行的企业级框架,它以spring为主体maven构建项目,简化项目构建过程,去除繁重的xml配置,内嵌tomcat等容器,支持jar包直接启动。
构建项目
创建maven项目,创建过程不再赘述,pom.xml设置springboot相关配置。方式有两种parent和dependency,一个maven项目只能有一个parent,被其他占用时,使用dependency。
parent方式
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.3.1.RELEASE</ version>
</ parent>
dependency方式
< dependencyManagement>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-dependencies</ artifactId>
< version> 2.3.1.RELEASE</ version>
< type> pom</ type>
< scope> import</ scope>
</ dependency>
</ dependencies>
</ dependencyManagement>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-thymeleaf</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-devtools</ artifactId>
< optional> true</ optional>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
</ dependency>
</ dependencies>
导包
导包有两种
一是:自身代码和引用jar导入一个jar包,好处是启动方便,java -jar 包名
启动即可。缺点每次更新都要上传一个几十M的大jar包。
二是:自身代码和引用jar分开,好处代码更新只上传自身代码包,依赖包不必重复上传,缺点启动通过-cp引入所有包,相对复杂。
基本设置,配置spring boot插件,设置打包跨过test目录代码。
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
< executions>
< execution>
< goals>
< goal> repackage</ goal>
</ goals>
</ execution>
</ executions>
</ plugin>
< plugin>
< groupId> org.apache.maven.plugins</ groupId>
< artifactId> maven-surefire-plugin</ artifactId>
< configuration>
< skipTests> true</ skipTests>
</ configuration>
</ plugin>
导出独立包
以上配置,直接运行run --> maven install会在项目根目录target目录下打包独立包。
不包含引用导出
增加build导出编写的代码逻辑,不包含引用jar
< plugin>
< groupId> org.apache.maven.plugins</ groupId>
< artifactId> maven-jar-plugin</ artifactId>
< configuration>
< archive>
< manifest>
< addClasspath> true</ addClasspath>
< classpathPrefix> lib/</ classpathPrefix>
< mainClass> ***.***.Application</ mainClass>
</ manifest>
</ archive>
</ configuration>
</ plugin>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
< configuration>
< fork> true</ fork>
< includes>
< include>
< groupId> nothing</ groupId>
< artifactId> nothing</ artifactId>
</ include>
</ includes>
</ configuration>
< executions>
< execution>
< goals>
< goal> repackage</ goal>
</ goals>
</ execution>
</ executions>
</ plugin>
导出独立包,但部署时需要的依赖包,打包时也一起导出到target下lib目录。
< plugin>
< groupId> org.apache.maven.plugins</ groupId>
< artifactId> maven-dependency-plugin</ artifactId>
< executions>
< execution>
< id> copy-dependencies</ id>
< phase> prepare-package</ phase>
< goals>
< goal> copy-dependencies</ goal>
</ goals>
< configuration>
< outputDirectory> ${project.build.directory}/lib</ outputDirectory>
< overWriteReleases> false</ overWriteReleases>
< overWriteSnapshots> false</ overWriteSnapshots>
< overWriteIfNewer> true</ overWriteIfNewer>
</ configuration>
</ execution>
</ executions>
</ plugin>
同时导出独立包和不包含引用包
有时即需要导出独立包,又需要不包含引用包,可以在上面配置基础上增加如下配置,target目录下会生成一个包含dependencies关键字的独立包。
< plugin>
< artifactId> maven-assembly-plugin</ artifactId>
< configuration>
< descriptorRefs>
< descriptorRef> -dependencies</ descriptorRef>
</ descriptorRefs>
< archive>
< manifest>
< mainClass> </ mainClass>
</ manifest>
</ archive>
</ configuration>
< executions>
< execution>
< id> make-assembly</ id>
< phase> package</ phase>
< goals>
< goal> single</ goal>
</ goals>
</ execution>
</ executions>
</ plugin>