Maven打包并生成運(yùn)行腳本的示例代碼
1.定義插件
<properties>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
<maven-assembly-plugin.version>2.4</maven-assembly-plugin.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
</properties>
<plugins>
<!-- compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<!--jar plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<!--<mainClass></mainClass>-->
</manifest>
</archive>
<excludes>
<!--<exclude></exclude>-->
</excludes>
</configuration>
</plugin>
<!--assembly plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<configuration>
<descriptors>
<descriptor>${project.basedir}/../assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
2.assembly配置
<assembly>
<id>bin</id>
<formats>
<format>tar.gz</format>
</formats>
<dependencySets>
<!-- runtime scope jar -->
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
<!-- system scope jar -->
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
<fileSets>
<!-- script -->
<fileSet>
<directory>${project.basedir}/../scripts</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0644</fileMode>
<directoryMode>0755</directoryMode>
<filtered>true</filtered>
</fileSet>
<!-- config -->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<directoryMode>0755</directoryMode>
<includes>
<include>*.xml</include>
<include>*.json</include>
<include>*.properties</include>
</includes>
<filtered>true</filtered>
</fileSet>
<!-- the project jar -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<!-- Document -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>doc</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
3.腳本
#!/bin/sh
#server id -- change
SERVER_ID=
#java home
JAVA_HOME=
#java command
JAVA_CMD=`which java`
#jvm option
JVM_OPT="-Xmx1024M -Xms512M -server -XX:+PrintGCDetails -XX:+PrintGCDateStamps"
#jar name
JAR=${project.artifactId}-${project.version}.jar
#main class
MAIN_CLASS=${MainClass}
# main class args
ARGS="${StartArgs}"
#environment
ENVIRONMENT=${profiles.environment}
#cd working path
cd_working_path(){
cd `dirname $0`
cd ..
}
#jar
jar(){
WK_PATH=`pwd`
/usr/bin/nohup ${JAVA_CMD} -Denvironment=${ENVIRONMENT} -Dlog4j.configurationFile=${WK_PATH}/config/log4j2.xml ${JVM_OPT} -cp ${WK_PATH}/lib/${JAR}:${WK_PATH}/lib/* ${MAIN_CLASS} ${ARGS} >/dev/null 2>&1 &
}
#get pid
get_pid(){
echo `ps -ef | grep ${JAR} | grep server_id=${SERVER_ID} |grep -v 'grep' |awk '{print $2}'`
}
#check
check(){
#check server id
if [ ! -n "$SERVER_ID" ];then
echo "Please set up the server id 'SERVER_ID'"
exit
fi
}
#start service
start(){
#check
check
#check pid
PID=`get_pid`
if [ -n "$PID" ];then
echo "Process exists, PID >> "${PID}
exit
fi
#check java
if [ -n "$JAVA_HOME" ];then
JAVA_CMD=${JAVA_HOME}/bin/java
fi
#start service
${JAVA_CMD} -version
jar
#check
if [ $? -ne 0 ];then
echo "Service startup failed."
exit
fi
#check service
PID=`get_pid`
if [ ! -n "$PID" ];then
echo "Service startup failed."
else
echo "Service startup success, Current environment is ${ENVIRONMENT} , PID >> "${PID}
fi
}
#stop service
stop(){
#check
check
#check pid
PID=`get_pid`
if [ ! -n "$PID" ];then
echo "Process not exists."
else
kill ${PID}
echo "Kill pid >> '$PID'"
if [ $? -ne 0 ];then
echo "Service shutdown failed."
exit
else
echo "Service shutdown success."
fi
fi
}
#restart service
restart(){
#stop service
stop
COUNT=0
while true
do
PID=`get_pid`
if [ ! -n "$PID" ];then
#start service
start
break
else
let COUNT++
echo "Restarting..."
if [ ${COUNT} -eq 3 ];then
echo "Restart error"
exit
fi
fi
sleep 3
done
}
#check state
state(){
PID=`get_pid`
if [ ! -n "$PID" ];then
echo "Service not exists."
else
echo "Service status is normal, PID >> '$PID'"
fi
}
#main
main(){
#cd working path
cd_working_path
if [ ! -n "$1" ];then
echo "***********************************************"
echo "* start : Start service *"
echo "* stop : Stop service *"
echo "* restart : Restart service *"
echo "* state : Check service state *"
echo "***********************************************"
read -p "Please choose >> ": CASE
PARAMETER=${CASE}
else
PARAMETER=$1
fi
case "$PARAMETER" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
state)
state
;;
*)
main
;;
esac
}
main $1
PS:下面看下Maven打包生成可運(yùn)行bat/sh腳本文件
利用Maven的appassembler-maven-plugin插件,就可以實(shí)現(xiàn)自動(dòng)打包可運(yùn)行的腳本,還可以跨平臺(tái)。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<repositoryLayout>flat</repositoryLayout>
<repositoryName>lib</repositoryName>
<configurationSourceDirectory>src/main/resources/conf</configurationSourceDirectory>
<!-- Set the target configuration directory to be used in the bin scripts -->
<configurationDirectory>conf</configurationDirectory>
<!-- Copy the contents from "/src/main/config" to the target configuration
directory in the assembled application -->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!-- Include the target configuration directory in the beginning of
the classpath declaration in the bin scripts -->
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<!-- prefix all bin files with "mycompany" -->
<binPrefix>startup</binPrefix>
<!-- set alternative assemble directory -->
<assembleDirectory>${project.build.directory}/server</assembleDirectory>
<!-- Extra JVM arguments that will be included in the bin scripts -->
<extraJvmArguments>-Xms768m -Xmx768m -XX:PermSize=128m
-XX:MaxPermSize=256m -XX:NewSize=192m -XX:MaxNewSize=384m
</extraJvmArguments>
<!-- Generate bin scripts for windows and unix pr default -->
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<programs>
<program>
<mainClass>com.coderli.onecoder.server.HypervisorServer</mainClass>
<name>startup</name>
</program>
</programs>
</configuration>
</plugin>
然后選擇要編譯的工程,右鍵->maven build… 命令如下圖:
package appassembler:assemble

然后執(zhí)行run,一個(gè)可執(zhí)行的腳本文件就生成好了。startup.bat是windows下的,startup.sh是linux下的

總結(jié)
到此這篇關(guān)于Maven打包并生成運(yùn)行腳本的文章就介紹到這了,更多相關(guān)Maven打包并生成運(yùn)行腳本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)全局監(jiān)聽(tīng)鍵盤詳解
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)全局監(jiān)聽(tīng)鍵盤的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2024-01-01
在Mybatis @Select注解中實(shí)現(xiàn)拼寫(xiě)動(dòng)態(tài)sql
這篇文章主要介紹了在Mybatis @Select注解中實(shí)現(xiàn)拼寫(xiě)動(dòng)態(tài)sql,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
SpringBoot使用AES對(duì)JSON數(shù)據(jù)加密和解密的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot使用AES對(duì)JSON數(shù)據(jù)加密和解密的實(shí)現(xiàn)方法,文章通過(guò)代碼示例介紹的非常詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08
SpringBoot為啥不用配置啟動(dòng)類的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot為啥不用配置啟動(dòng)類的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
SpringBoot整合ShedLock解決定時(shí)任務(wù)防止重復(fù)執(zhí)行的問(wèn)題
ShedLock是一個(gè)用于分布式系統(tǒng)中防止定時(shí)任務(wù)重復(fù)執(zhí)行的庫(kù),本文主要介紹了SpringBoot整合ShedLock解決定時(shí)任務(wù)防止重復(fù)執(zhí)行的問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
基于Java HttpClient和Htmlparser實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)代碼
這篇文章主要介紹了基于Java HttpClient和Htmlparser實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)代碼的相關(guān)資料,需要的朋友可以參考下2015-12-12

