Maven 스 크 립 트 를 포장 하고 실행 하 는 예제 코드 를 생 성 합 니 다.

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 패키지 생 성 을 살 펴 보면 bat/sh 스 크 립 트 파일 을 실행 할 수 있 습 니 다.
Maven 의 appssembler-maven-plugin 플러그 인 을 이용 하면 실행 가능 한 스 크 립 트 를 자동 으로 포장 하고 플랫폼 을 뛰 어 넘 을 수 있 습 니 다.

<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 

그리고 run 을 실행 하면 실행 가능 한 스 크 립 트 파일 이 생 성 됩 니 다.startup.bat 는 윈도 우즈 아래,startup.sh 는 linux 아래

총결산
Maven 포장 및 실행 스 크 립 트 생 성 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.Maven 포장 및 실행 스 크 립 트 생 성 에 관 한 더 많은 내용 은 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기