打包SpringBoot工程并部署

Posted 凉城

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打包SpringBoot工程并部署相关的知识,希望对你有一定的参考价值。

  使用工具:Eclipse

  Linux下JDK版本:jdk-7u79-linux-x64.tar.gz

一、打包成jar并部署

 

  步骤如下:

  首先上pom.xml:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4 
  5   <groupId>com.xxxe</groupId>
  6   <artifactId>programme</artifactId>
  7   <version>0.0.1</version>
  8   <packaging>jar</packaging>
  9 
 10   <name>programme</name>
 11   <url>http://maven.apache.org</url>
 12   <properties>
 13     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 14     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 15   </properties>
 16 
 17     <parent>
 18         <groupId>org.springframework.boot</groupId>
 19         <artifactId>spring-boot-starter-parent</artifactId>
 20         <version>1.4.2.RELEASE</version>
 21     </parent>
 22 
 23   <dependencies>
 24     <dependency>
 25         <groupId>org.springframework.boot</groupId>
 26         <artifactId>spring-boot-starter-web</artifactId>
 27     </dependency>
 28     
 29     
 30     <dependency>
 31       <groupId>junit</groupId>
 32       <artifactId>junit</artifactId>
 33       <scope>test</scope>
 34     </dependency>
 35     <dependency>
 36         <groupId>org.springframework.data</groupId>
 37         <artifactId>spring-data-commons</artifactId>
 38     </dependency>
 39     <dependency>
 40         <groupId>org.springframework.data</groupId>
 41         <artifactId>spring-data-jpa</artifactId>
 42     </dependency>
 43     <dependency>
 44         <groupId>org.springframework.data</groupId>
 45         <artifactId>spring-data-jdbc-core</artifactId>
 46         <version>1.2.1.RELEASE</version>
 47     </dependency>
 48     <dependency>
 49         <groupId>com.fasterxml.jackson.core</groupId>
 50         <artifactId>jackson-core</artifactId>
 51     </dependency>
 52     <dependency>
 53         <groupId>com.fasterxml.jackson.core</groupId>
 54         <artifactId>jackson-annotations</artifactId>
 55     </dependency>
 56     <dependency>
 57         <groupId>com.fasterxml.jackson.core</groupId>
 58         <artifactId>jackson-databind</artifactId>
 59     </dependency>
 60     <dependency>
 61         <groupId>org.slf4j</groupId>
 62         <artifactId>slf4j-api</artifactId>
 63     </dependency>
 64     <dependency>
 65         <groupId>org.apache.httpcomponents</groupId>
 66         <artifactId>httpcore</artifactId>
 67         </dependency>
 68     <dependency>
 69         <groupId>org.apache.httpcomponents</groupId>
 70         <artifactId>httpclient</artifactId>
 71         </dependency>
 72     <dependency>
 73         <groupId>org.apache.httpcomponents</groupId>
 74         <artifactId>httpmime</artifactId>
 75         </dependency>
 76     <dependency>
 77               <groupId>mysql</groupId>
 78               <artifactId>mysql-connector-java</artifactId>
 79           </dependency>
 80           <dependency>
 81             <groupId>org.springframework.boot</groupId>
 82             <artifactId>spring-boot-starter-jdbc</artifactId>
 83         </dependency>
 84 
 85 
 86           <dependency>
 87               <groupId>javax</groupId>
 88               <artifactId>javaee-api</artifactId>
 89               <version>7.0</version>
 90           </dependency>
 91           <dependency>
 92               <groupId>org.apache.shiro</groupId>
 93               <artifactId>shiro-spring</artifactId>
 94               <version>1.2.3</version>
 95           </dependency>
 96           <dependency>
 97               <groupId>org.apache.shiro</groupId>
 98               <artifactId>shiro-cas</artifactId>
 99               <version>1.2.3</version>
100           </dependency>
101           <dependency>
102               <groupId>org.apache.shiro</groupId>
103               <artifactId>shiro-ehcache</artifactId>
104               <version>1.2.3</version>
105           </dependency>
106           <dependency>
107               <groupId>org.springframework</groupId>
108               <artifactId>spring-jdbc</artifactId>
109               </dependency>
110           <dependency>
111               <groupId>org.apache.shiro</groupId>
112               <artifactId>shiro-core</artifactId>
113               <version>1.2.3</version>
114           </dependency>
115           <dependency>
116               <groupId>org.apache.shiro</groupId>
117               <artifactId>shiro-web</artifactId>
118               <version>1.2.3</version>
119           </dependency>
120           <dependency>
121               <groupId>org.jasig.cas.client</groupId>
122               <artifactId>cas-client-core</artifactId>
123               <version>3.4.1</version>
124           </dependency>
125           
126           <!-- 模板引擎 暂时不用 -->
127          <!--  <dependency>
128             <groupId>org.springframework.boot</groupId>
129             <artifactId>spring-boot-starter-thymeleaf</artifactId>
130           </dependency> -->
131         <!-- 
132             devtools可以实现热部署。devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的
133             如果设置SpringApplication.setRegisterShutdownHook(false),则自动重启将不起作用。
134             设置 spring.devtools.restart.enabled 属性为false,可以关闭该特性。
135          -->
136     
137         <dependency>
138             <groupId>org.springframework.boot</groupId>
139             <artifactId>spring-boot-devtools</artifactId>
140             <optional>true</optional>
141             <!-- optional=true,依赖不会传递,-->
142         </dependency>
143           
144   </dependencies>
145   
146    <!-- Package as an executable jar -->
147     <build>
148         <defaultGoal>compile</defaultGoal>
149         <plugins>
150             <plugin>
151                 <groupId>org.springframework.boot</groupId>
152                 <artifactId>spring-boot-maven-plugin</artifactId>
153                  <configuration>
154                     <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>  
155                     <fork>true</fork><!-- 如果没有该项配置,可能devtools不会起作用 -->
156                 </configuration>
157             </plugin>
158             
159         </plugins>
160     </build>
161 
162     <!-- Add Spring repositories -->
163     <!-- (you don\'t need this if you are using a .RELEASE version) -->
164      <!--    
165          <repositories>
166         <repository>
167             <id>spring-snapshots</id>
168             <url>http://repo.spring.io/snapshot</url>
169             <snapshots><enabled>true</enabled></snapshots>
170         </repository>
171         <repository>
172             <id>spring-milestones</id>
173             <url>http://repo.spring.io/milestone</url>
174         </repository>
175     </repositories>
176     <pluginRepositories>
177         <pluginRepository>
178             <id>spring-snapshots</id>
179             <url>http://repo.spring.io/snapshot</url>
180         </pluginRepository>
181         <pluginRepository>
182             <id>spring-milestones</id>
183             <url>http://repo.spring.io/milestone</url>
184         </pluginRepository>
185     </pluginRepositories> 
186     -->
187   
188 </project>
pom.xml

  1、工程--右键选择运行配置:

   在Goals中输入:org.apache.maven.plugins:maven-jar-plugin:2.4:jar org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage

   

  2、编译打包:工程-->Run As--> Maven build (编译)---  Maven install(打包)

  3、获取jar包:在工程下的target文件夹下找到打包的jar文件;

  4、部署:springboot工程打包jar部署只需要jre坏境即可:

   文件结构如下:满足linux与windows机器部署需要,使用不同的jre

   

    文件说明:

        dev:部署的工程.jar所在文件夹;

        jre7: windows下的jre环境

        jer7-linux:linux下的jre环境

        application.properties: 主要配置文件

        pm-logback.xml :日志配置文件

        startup.bat:Windows启动命令;

set dir=%CD%
set JRE_HOME=%dir%\\jre7
%dir%\\jre7\\bin\\java -jar %dir%\\dev\\pm.jar

        startup.sh :Linux启动命令:

CUR_PATH=$(cd "$(dirname "$0")"; pwd)
echo "Current path: ${CUR_PATH}"
${CUR_PATH}/jre7-linux/bin/java -jar ${CUR_PATH}/dev/pm.jar

    5、启动是按照情况点击startup

二、打包成war并部署:

    官网的说明:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#build-tool-plugins-maven-packaging

    好像不起作用,待研究!  

    又试了一下,完全按照官网是来是可以的! 

   1、工程--pom.xml-->右键Run Config...  Goals输入package

  

  2、如果正常的话,可以在工程-target文件夹下找到打包好的war;

  3、部署:需要jre和tomcat,结构如下:

    

    war包放入tomcat文件夹中的webapps文件夹下;

    startup.bat:

set dir=%CD%
set JRE_HOME=%dir%\\jre7
set JAVA_OPTS=-Dpm.home=%CD% -Dapp.home=%CD%
cd  %dir%\\apache-tomcat-7\\bin
startup

  startup.sh:所部署的机器必须有jre_home

CUR_PATH=$(cd "$(dirname "$0")"; pwd)
echo "Current path: ${CUR_PATH}"

cd ${CUR_PATH}/apache-tomcat-7/bin

export JAVA_OPTS="-Dpm.home=${CUR_PATH}"

./startup.sh start 

cd ${CUR_PATH}

  4、启动,输入...../pm/

------------------------------

  关于配置文件放到外部的情况,正在研究......     

    

以上是关于打包SpringBoot工程并部署的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot简单打包部署(附工程)

Vue.Js+SpringBoot 打包部署至生产环境

springboot 部署k8s

如何将springboot项目打包成jar包并部署到服务器上

Linux——在Linux系统上打包和部署springboot+vue项目,nginx+mysql+redis+minio

Linux——在Linux系统上打包和部署springboot+vue项目,nginx+mysql+redis+minio