SpringBoot簡單打包部署(附工程)

來源:互聯網
上載者:User

標籤:manifest   過多   ini   github   utils   相容   如何   本地   多版本   

前言

本文主要介紹SpringBoot的一些打包事項和項目部署以及在其中遇到一些問題的解決方案。

SpringBoot打包

在SpringBoot打包這塊,我們就用之前的一個web項目來進行打包。
首先需要明確的是,該項目打包的形態是可執行檔jar包,還是在tomcat下啟動並執行war包。
雖然本項目是用maven構建的,用maven打包也更加方便,但是這裡也說明普通非maven打包的項目如何打包。

Maven打包

首先是maven方式打包:
如果是jar
需在pom.xml指定打成的包為:

<packaging>jar</packaging>  

如果是war包。
需在pom.xml指定打成的包為:

<packaging>war</packaging>  

並通過&lt;scope&gt;標籤在打包的時候排除tomcat依賴

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-tomcat</artifactId>    <scope>provided</scope></dependency>

然後添加SpringBoot內建的打包方式
樣本如下:

<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>

註:&lt;finalName&gt;標籤是指定打包之後的名稱,&lt;mainClass&gt;是指定main函數。

也可以不用SpringBoot內建的打包方式,使用mavenassembly外掛程式進行打包。
樣本如下:

<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同級)輸入

mvn clean package

即可完成打包
如果想排除測試代碼,則可以輸入:

mvn clean package  -Dmaven.test.skip=true

來進行打包。

一般我們是把application.propertieslogback.xml檔案放在resources檔案夾中,但是進行打包之後,它們也會包含在jarwar包中,如果我們想更改配置,則會比較麻煩。
如果想將它們和項目放在同級目錄下,application.properties可以直接移出和項目同級的目錄下,因為Spring程式會按優先順序從下面這些路徑來載入application.properties設定檔:

  • 目前的目錄下的/config目錄
  • 目前的目錄
  • classpath裡的/config目錄
  • classpath 根目錄

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 -&gt;java -&gt; runnable jar file-&gt; package required libraries into generated jar 指定main方法,然後選擇打包的名稱以及打包的路徑。點擊finish完成打包。

如果是war
eclipse中右鍵項目 export -&gt;web -&gt; war file,然後選擇打包的名稱以及打包的路徑。點擊finish完成打包。

Ant打包

介紹了上述兩種打包之後,這裡介紹下通過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>

註:&lt;mkdir dir="${build}" /&gt;是指定檔案存放的檔案夾,executable是使用cmd命令,line是執行的語句,<copy> 標籤是將檔案複製到指定的檔案夾中。

然後再建立一個 build.bat檔案,裡面只需要填寫 ant就行了。
準備完之後,只需雙擊build.bat,項目和設定檔就自動到build檔案中了,省去了很多操作。

雖然現在流行通過jenkins進行打包部署,不過使用ant加maven進行打包也不錯的,比較簡單。

SpringBoot部署

如果是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-packagechmod +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打包部署的項目工程地址:
https://github.com/xuwujing/springBoot-study/tree/master/springboot-package

SpringBoot整合系列:

  • springBoot設定檔的讀取以及過濾器和攔截器的使用

  • SpringBoot的Restful風格的服務
  • SpringBoot+Mybatis+ Druid+PageHelper實現多資料來源並分頁

  • SpringBoot整合Elasticsearch實現多版本的相容

  • SpringBoot整合Kafka和Storm

  • SpringBoot整合Jsp和Thymeleaf

  • SpringBoot整合Netty並使用Protobuf進行資料轉送

原創不易,如果感覺不錯,希望給個推薦!您的支援是我寫作的最大動力!
著作權聲明:
虛無境
部落格園出處:http://www.cnblogs.com/xuwujing
CSDN出處:http://blog.csdn.net/qazwsxpcm    
個人部落格出處:http://www.panchengming.com

SpringBoot簡單打包部署(附工程)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.