打包SpringBoot工程并部署

时间:2022-03-29 00:12:21

  使用工具:Eclipse

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

一、打包成jar并部署

  步骤如下:

  首先上pom.xml:

 <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> <groupId>com.xxxe</groupId>
<artifactId>programme</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging> <name>programme</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc-core</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.4.1</version>
</dependency> <!-- 模板引擎 暂时不用 -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> -->
<!--
devtools可以实现热部署。devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的
如果设置SpringApplication.setRegisterShutdownHook(false),则自动重启将不起作用。
设置 spring.devtools.restart.enabled 属性为false,可以关闭该特性。
--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<!-- optional=true,依赖不会传递,-->
</dependency> </dependencies> <!-- Package as an executable jar -->
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
<fork>true</fork><!-- 如果没有该项配置,可能devtools不会起作用 -->
</configuration>
</plugin> </plugins>
</build> <!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<!--
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
--> </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

   打包SpringBoot工程并部署

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

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

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

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

   打包SpringBoot工程并部署

    文件说明:

        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

  打包SpringBoot工程并部署

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

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

    打包SpringBoot工程并部署

    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-\bin
startup

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

CUR_PATH=$(cd "$(dirname "$")"; pwd)
echo "Current path: ${CUR_PATH}" cd ${CUR_PATH}/apache-tomcat-/bin export JAVA_OPTS="-Dpm.home=${CUR_PATH}" ./startup.sh start cd ${CUR_PATH}

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

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

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