本文主要介绍SpringBoot的一些打包事项和项目部署以及在其中遇到一些问题的解决方案。html
在SpringBoot打包这块,咱们就用以前的一个web项目来进行打包。 首先须要明确的是,该项目打包的形态是可执行的jar包,仍是在tomcat下运行的war包。 虽然本项目是用maven构建的,用maven打包也更加方便,可是这里也说明普通非maven打包的项目如何打包。java
首先是maven方式打包: 若是是jar包 需在pom.xml
指定打成的包为:git
<packaging>jar</packaging>
复制代码
若是是war包。 需在pom.xml
指定打成的包为:github
<packaging>war</packaging>
复制代码
并经过<scope>
标签在打包的时候排除tomcat依赖web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
复制代码
而后添加SpringBoot自带的打包方式 示例以下:spring
<build>
<defaultGoal>compile</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<finalName>springboot-package</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
<configuration>
<fork>true</fork>
<mainClass>com.pancm.App</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
复制代码
注:<finalName>
标签是指定打包以后的名称,<mainClass>
是指定main函数。shell
也能够不用SpringBoot自带的打包方式,使用maven的assembly插件进行打包。 示例以下:apache
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.pancm.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
复制代码
在pom.xml中添加完相应的标签以后,咱们只需在项目同级(pom.xml同级)输入tomcat
mvn clean package
复制代码
便可完成打包 若是想排除测试代码,则能够输入:springboot
mvn clean package -Dmaven.test.skip=true
复制代码
来进行打包。
通常咱们是把application.properties和logback.xml文件放在resources文件夹中,可是进行打包以后,它们也会包含在jar或war包中,若是咱们想更改配置,则会比较麻烦。 若是想将它们和项目放在同级目录下,application.properties能够直接移出和项目同级的目录下,由于Spring程序会按优先级从下面这些路径来加载application.properties配置文件:
springboot默认加载的logback是在classpath目录下,这时咱们只须要在application.properties配置文件指定logback.xml的路径便可。 添加以下:
logging.config=logback.xml
复制代码
若是引入了第三方的jar包,可是又没法经过maven私服进行下载,这时能够手动进行编译。 例如,我写了一个工具类为Mytools,而后把它打成了一个jar包,而后放在个人这个项目中lib目录下,而且须要引用它,那么此时即可以对该jar包进行编译到本地仓库中,而后再pom.xml添加相应的名称和版本号。 命令示例:
mvn install:install-file -Dfile=lib/pancmtools.jar -DgroupId=com.panncm.utils -DartifactId=pancm-utils -Dversion=1.0 -Dpackaging=jar
复制代码
pom.xml添加
<dependency>
<groupId>com.panncm.utils</groupId>
<artifactId>pancm-utils</artifactId>
<version>1.0</version>
</dependency>
复制代码
即可以进行打包了。
若是是普通的项目,没有使用maven构建的话,可使用eclipse等工具进行打包。 若是是jar包 首先在eclipse中运行该项目(main方法运行),而后在eclipse中右键项目 export ->java -> runnable jar file-> package required libraries into generated jar
指定main方法,而后选择打包的名称以及打包的路径。点击finish完成打包。
若是是war包 在eclipse中右键项目 export ->web -> war file
,而后选择打包的名称以及打包的路径。点击finish完成打包。
介绍了上述两种打包以后,这里介绍下经过ant方法进行打包(须要安装ant环境,安装方式基本和maven一致,指定路径,配置环境变量,这里就不在过多讲述了)。 通常打包以后,咱们须要将包和配置文件放在一个目录下,这时咱们又不想手动进行复制粘贴的话,就能够用ant来进行打包,将打包的文件整合在一块儿。 这里咱们就编写一个build.xml的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<project name="springboot-package" default="copyAll" basedir=".">
<property name="build" value="build" />
<property name="target" value="target" />
<target name="clean">
<delete dir="${target}" />
<delete dir="${build}" />
</target>
<target name="create-path" depends="clean">
<mkdir dir="${build}" />
</target>
<target name="mvn_package" depends="create-path">
<exec executable="cmd" failonerror="true">
<arg line="/c mvn install:install-file -Dfile=lib/pancmtools.jar -DgroupId=com.panncm.utils -DartifactId=pancm-utils -Dversion=1.0 -Dpackaging=jar" />
</exec>
<exec executable="cmd" failonerror="true">
<arg line="/c mvn clean package" />
</exec>
</target>
<target name="copyAll" depends="mvn_package">
<copy todir="${build}" file="${target}/springboot-package.jar"></copy>
<copy todir="${build}" file="logback.xml"></copy>
<copy todir="${build}" file="application.properties"></copy>
<copy todir="${build}" file="run.bat"></copy>
</target>
</project>
复制代码
注:<mkdir dir="${build}" />
是指定文件存放的文件夹,executable是使用cmd命令,line是执行的语句, 标签是将文件复制到指定的文件夹中。
而后再新建一个 build.bat文件,里面只须要填写 ant
就好了。 准备完以后,只需双击build.bat,项目和配置文件就自动到build文件中了,省去了不少操做。
虽然如今流行经过jenkins进行打包部署,不过使用ant加maven进行打包也不错的,比较简单。
若是是jar项目 Windows系统在项目同级目录下输入:
java -jar springboot-package
复制代码
便可启动项目。 关闭项目,只需关掉dos界面就能够了。 也能够写一个bat文件进行运行。
Linux系统在项目同级目录下输入:
nohup -jar springboot-package &
复制代码
便可启动。 关闭输入:
kill -9 pid(jar的进程id)
复制代码
也能够在init.d
注册一个服务 示例:
ln -s /home/jars/app/springboot-package.jar /etc/init.d/springboot-package
chmod +x /etc/init.d/springboot-package
复制代码
而后输入:
service springboot-package start|stop|restart
复制代码
进行启动或者中止。 固然也能够编写xshell脚本进行启动和关闭。
若是是war项目 将war放在tomcat/webapp目录下,而后启动tomcat就能够了。Windows系统 在tomcat/bin目录下双击startup.bat便可启动,双击shutdown.bat关闭。 Linux系统则在tomcat/bin 目录下输入startup.sh便可启动, 输入shutdown.sh关闭。
关于SpringBoot打包部署就讲解到这里了,若有不妥,欢迎指正! SpringBoot打包部署的项目工程地址: github.com/xuwujing/sp…
SpringBoot整合系列:
原创不易,若是感受不错,但愿给个推荐!您的支持是我写做的最大动力! 版权声明: 做者:虚无境 博客园出处:www.cnblogs.com/xuwujing CSDN出处:blog.csdn.net/qazwsxpcm 我的博客出处:www.panchengming.com