mvn打jar包的幾個外掛分析
前言:在部署project專案時,希望專案目錄結構是這樣的:一個bin目錄包含啟動指令碼、停止指令碼、狀態檢查指令碼等(在bin資料夾下 直接 ./專案名 start 就可以後臺啟動專案;./專案名 status 檢視專案執行狀態;./專案名 stop 停專案);一個conf目錄包含專案配置檔案(這裡是content.properties檔案);一個lib目錄包含專案依賴jar包。所有的這些目錄都可以由assembly外掛完成。DEMO下載地址:https://pan.baidu.com/s/1XOYP5QDsG5iGUUylEOdYFw 提取碼:33qt 以下是專案外掛分析:
<build>
<plugins>
<plugin>
<!-- 新增maven釋出jar檔案的外掛 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<!-- 生成jsw(java service wrapper)後臺執行程式的外掛 -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<!-- lib目錄中jar的存放規則,預設是${groupId}/${artifactId}的目錄格式,flat表示直接把jar放到lib目錄,而不把jar包解壓 -->
<repositoryLayout>flat</repositoryLayout>
<!-- 生成bin資料夾下不同平臺可執行檔案的配置 -->
<daemons>
<!-- 生成bin,及可執行檔案 ./專案名 { console | start | stop | restart | status | dump } -->
<daemon>
<!-- 應用入口(含main方法)啟動類 -->
<mainClass>com.springboot.beetlsql.BeetlSQLTest</mainClass>
<id>${project.artifactId}</id><!-- 可執行檔案的名稱 -->
<commandLineArguments>
<commandLineArgument>start</commandLineArgument>
</commandLineArguments>
<platforms>
<platform>jsw</platform>
</platforms>
<generatorConfigurations>
<generatorConfiguration>
<generator>jsw</generator>
<includes>
<include>linux-x86-64</include>
<include>windows-x86-64</include>
<include>macosx-universal-64</include>
</includes>
<configuration>
<property>
<!-- wrapper.conf配置檔案存放的路徑 -->
<name>configuration.directory.in.classpath.first</name>
<value>etc</value>
</property>
<property>
<name>set.default.REPO_DIR</name>
<value>lib</value>
</property>
<property>
<name>wrapper.logfile.loglevel</name>
<value>NONE</value>
</property>
</configuration>
</generatorConfiguration>
</generatorConfigurations>
<jvmSettings>
<!-- JVM引數 -->
<initialMemorySize>2048M</initialMemorySize>
<maxMemorySize>2048M</maxMemorySize>
<maxStackSize>128M</maxStackSize>
<extraArguments>
<extraArgument>-server</extraArgument>
<extraArgument>-XX:PermSize=64M</extraArgument>
<extraArgument>-XX:MaxPermSize=128m</extraArgument>
</extraArguments>
</jvmSettings>
</daemon>
</daemons>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>generate-daemons</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptors>
<!-- 檔案配置匯出描述在jsw.xml中,是project最終目錄結構的描述檔案 -->
<descriptor>src/main/assembly/jsw.xml</descriptor>
</descriptors>
</configuration>
<executions>
<!-- 若要同時打多個包(如windows和linux不同系統的包),可配置多個execution,此處只打tar.gz,因此配置一個 -->
<!-- 預設生成的一個包 ${project.build.directory}/${project.build.finalName}-bin(這是jsw檔案開頭的id=bin).tar.gz(這是jsw檔案中format=tar.gz)-->
<execution>
<!-- 繫結到package生命週期階段上,即在mvn package的時候打包 -->
<phase>package</phase>
<goals>
<!-- 只執行一次 -->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!-- 在maven中執行Ant任務,在打包階段,對檔案進行復制 -->
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-app</id>
<phase>package</phase>
<configuration>
<tasks>
<copy todir="../../juwenzhe/springboot"
file="${project.build.directory}/${project.build.finalName}-bin.tar.gz" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<!-- 需要打包的資原始檔 -->
<resource>
<directory>src/main/resources/env/${deploy.type}</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<!-- 選擇不同環境下對應的引數列表 -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<deploy.type>dev</deploy.type>
</properties>
</profile>
<profile>
<id>sit</id>
<properties>
<deploy.type>sit</deploy.type>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<deploy.type>prod</deploy.type>
</properties>
</profile>
</profiles>
----------------jsw.xml內容:最終釋出包的目錄結構描述檔案------------------
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id><!-- 打成的包附帶的名稱 如: wx_gws-1.1.5.5-bin.tar.gz-->
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<!-- 需要打包的路徑 -->
<directory>${project.build.directory}/generated-resources/appassembler/jsw/${project.artifactId}</directory>
<!-- 打包後輸出的路徑 -->
<outputDirectory>${project.artifactId}</outputDirectory>
<!-- 設定檔案、資料夾的訪問許可權755 -->
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
</fileSet>
<fileSet>
<!-- 由打包命令mvn package -P sit決定${deploy.type}=sit -->
<directory>src/main/resources/env/${deploy.type}/conf</directory>
<outputDirectory>${project.artifactId}/etc/conf</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>${project.artifactId}/logs</outputDirectory>
<!-- 排除某些檔案的樣例 -->
<excludes>
<exclude>**/*</exclude>
</excludes>
</fileSet>
</fileSets>
<!-- 開啟下面的配置,會生成一個資料夾,存放所有執行時的依賴包到生成專案同級目錄的lib資料夾下 -->
<!-- <dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>將scope為runtime的依賴包打包到lib目錄下。
<scope>runtime</scope>
</dependencySet>
</dependencySets> -->
</assembly>
可能遇到的問題一:
[[email protected] bin]# ./HelloWorld start
Unable to locate any of the following operational binaries:
/juwenzhe/HelloWorld/bin/./wrapper-linux-x86-64 (Found but not executable.)
/juwenzhe/HelloWorld/bin/./wrapper-linux-x86-32
/juwenzhe/HelloWorld/bin/./wrapper
解決思路與方法:
wrapper-linux-x86-64檔案缺少許可權
chmod 777 wrapper-linux-x86-64
可能遇到的問題二:
[[email protected] bin]# ./HelloWorld console
Running HelloWorld...
wrapper | ERROR: Could not write pid file /juwenzhe/HelloWorld/logs/HelloWorld.pid: No such file or directory
解決思路與方法:
手動在解壓後的logs新增 HelloWorld.pid 檔案 存放專案啟動後的程序號
參考文獻:
1.Maven學習總結:幾個常用的maven外掛 http://shmilyaw-hotmail-com.iteye.com/blog/2170300
2.maven 安裝問題:mvn -v 不是內部命令 https://www.cnblogs.com/starriver/p/7793104.html
3.Maven命令列使用:mvn clean package(打包) https://www.cnblogs.com/frankyou/p/6062179.html
4.java學習筆記8 - maven profile實現多環境打包 https://blog.csdn.net/wuxinzaiyu/article/details/8524274
5.maven整合命令-U -B -P -e https://blog.csdn.net/tlqfreedom/article/details/60955757
6.Maven Assembly外掛介紹 http://blueram.iteye.com/blog/1684070
7.關於Maven的assembly外掛 https://www.douban.com/note/207395473/?type=like
8.maven打zip包及windows下jsw安裝java服務 https://www.2cto.com/kf/201607/522111.html