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的appassembler-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是windows下的,startup.sh是linux下的

总结

到此这篇关于Maven打包并生成运行脚本的文章就介绍到这了,更多相关Maven打包并生成运行脚本内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 浅谈maven的jar包和war包区别 以及打包方法

    jar文件包括java普通类.资源文件和普通文件,在maven中即是打包src/main/java和src/main/resources资源文件夹下的所有文件.在打包的时候会自动生成MATA-INF文件夹,用于存储maven的pom信息和MANIFEST.MF文件.例如: war文件包含全部的web应用程序,即所有的java类,配置信息和jsp.js等静态资源.但是需要注意war引用war的时候会将应用war的资源全部拷贝到当前war的相同文件下,重名的文件会被替换.例如: war包依赖: <d

  • Maven项目打包成war包部署到Tomcat的方法

    有关于 Maven 项目的打包部署,我这里用的是 Eclipse 编辑器,以此来做个简单的记录. 实践环境 操作系统: Windows IDE: Eclipse 打包部署过程 1 项目打包 1.1 右键点击所需要打包的项目,点击如图所示 Maven clean,这里 Maven 会清楚掉之前对这个项目所有的打包信息. 1.2 进行完 Maven clean 操作后,在eclipse的控制台会出现以下的信息. 1.3 然后我们重新右键所需打包的项目,点击如图所示 Maven build 1.4 在

  • SpringBoot+Maven 多模块项目的构建、运行、打包实战

    本篇文章主要介绍了SpringBoot+Maven 多模块项目的构建.运行.打包,分享给大家,具体如下: 项目使用的工具: IntelliJ IDEA JDK 1.8 apache-maven-3.3.9 项目的目录: 主项目 springboot-multi 子模块 entity.dao.service.web 一.使用IDEA创建一个SpringBoot项目 : File -> new -> Project 项目名称为springboot-multi 二.删除项目中的src目录,把pom.

  • 配置pom.xml用maven打包java工程的方法(推荐)

    最近由于项目需要,研究了一下maven的打包,项目要做到 1,生成3个目录/lib,/conf,/bin目录 2,把所有的jar目录编译.拷贝到/lib目录(包括maven的jar包和lib目录下的jar,以及编译的jar包) 3,把所有的启动脚本从工程根目录拷贝到/bin目录 4,把所有的配置文件从src/main/resources拷贝到/conf 下面是配置的pom.xml,我把相关的配置都加了注释,一看就能明白,把build节点拷贝到你们的项目中,就基本可以用了:) <project x

  • maven多模块工程打包部署的方法步骤

    一般maven多模块工程结构如下图,图中分为dao数据层和上层web层(当然还可以有service层),在进行多模块划分的时候,一般将dao层采用jar进行打包,web层进行war打包.在进行war包部署时,发现dao是以jar包形式存在于lib包目录下,如果在部署服务器上需要进行相关配置修改会比较麻烦.因此研究了下用maven进行合并打包的方法: 1.确保dao pom.xml中有以下配置 <resources> <resource> <directory>${bas

  • maven打包web项目时同时打包为war和jar文件的方法

    本文介绍了maven打包web项目时同时打包为war和jar文件的方法,分享给大家,具体如下: 首先在pom.xml文件中指定war的打包方式,war <artifactId>test</artifactId> <name>test</name> <packaging>war</packaging> 上述代码在eclipse中执行maven install时, 会默认打成war,并放入本地仓库. web项目时同时打包为war和jar文

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

  • Maven打包jar生成javadoc文件和source文件代码实例

    这篇文章主要介绍了Maven打包jar生成javadoc文件和source文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 官方文章 : https://maven.apache.org/plugin-developers/cookbook/attach-source-javadoc-artifacts.html 生成文件的样本 attach-source-javadoc |-- pom.xml |-- src\ `-- target

  • SpringBoot项目中使用Groovy脚本的示例代码

    目录 1. 引入依赖 2. 使用脚本引擎运行groovy脚本 3.思考 SpringBoot+Groovy运行动态脚本 GroovyClassLoader方式 GroovyScriptEngine方式 变量绑定 最近项目中遇到了这样的需求:需要检查一个表的某些字段,是否为空,或者是否符合预期规则:比如大于0,或者在某个范围内.考虑将表名和字段名配置在数据库中,然后规则使用Groovy来写,比较灵活. 1. 引入依赖 <dependency> <groupId>org.codehau

  • Java中Maven项目导出jar包配置的示例代码

    具体代码如下所示: <!-- 第一种打包方式 (maven-jar-plugin), 将依赖包和配置文件放到jar包外 --> <build> <sourceDirectory>src/main/java</sourceDirectory> <resources> <resource> <directory>src/main/resources</directory> <!-- 将<director

  • Python实现多线程下载脚本的示例代码

    0x01 分析 一个简单的多线程下载资源的Python脚本,主要实现部分包含两个类: Download类:包含download()和get_complete_rate()两种方法. download()方法种首先用 urlopen() 方法打开远程资源并通过 Content-Length获取资源的大小,然后计算每个线程应该下载网络资源的大小及对应部分吗,最后依次创建并启动多个线程来下载网络资源的指定部分. get_complete_rate()则是用来返回已下载的部分占全部资源大小的比例,用来回

  • Python实现自动签到脚本的示例代码

    实训课期间忙里偷闲的学习了python的selenium包,唯一一点不好是要自己去查英文文档,明摆着欺负我这种英语不好的,想着用谷歌翻译一下,代码也给我翻译了,不知道是几个意思. 大二的时候就让我们做自动签到脚本,说用JS可以写一下,但是说着说着就给忘了,现在学了python后又想起来要写一个自动签到的脚本,不得不佩服python的强大,短短二十行左右的代码就实现了,虽然说脚本还需要手动操作去运行,以后还是可以慢慢优化的. 开发环境 : Windows10 + sublime(编辑器装好pyth

  • Python将QQ聊天记录生成词云的示例代码

    在这个情人节前夕,我把现任对象回收掉了,这段感情积攒了太多的失望,也给了我太多的伤害,所以我看到这个活动的第一反应是拒绝的.然而人生嘛,最重要的就是体验,沉浸在过去的回忆里没有意义,积极面对才能让自己更好地重振旗鼓. 所以,当大家都一致地在这个活动里各种秀恩爱时,我决定走一条不一样的路来为单身狗和刚分手的小伙伴们打打气:时间能改变的,是那些原本就不坚定的东西,未来的路还很长,笑一笑,一切都会过去的! 言归正传,我们要做的任务是,把 QQ 分手聊天记录导出,使用 Python 分词后做成分开的桃心

  • Python生成九宫格图片的示例代码

    一.前言 大家在朋友圈应该看到过用一张图片以九宫格的方式显示,效果大致如下: 要实现上面的效果非常简单,我们只需要截取图片的九个区域即可.今天我们就要带大家使用Python来实现一下九宫格图片的生成.在开始之前,我们需要安装一下Pillow模块,语句如下: pip install pillow 下面我们先来看看一些简单的图片操作. 二.图片基本操作 今天我们会使用到三个操作,分别是读取图片.保存图片和截取图片.下面我们分别来看看. 2.1 读取图片 在Pillow中,我们最常用的就是Image子

  • Java 生成随机字符的示例代码

    示例代码: import java.util.Random; import java.util.UUID; public class Dept { /** * 生成随机字符串 uuid */ public static String getUUID() { return UUID.randomUUID().toString(); } /** * 生成随机字符串 uuid 将"-"替换为"" */ public static String getUUNUM() { r

  • python实现自动抢课脚本的示例代码

    目录 自动抢课脚本使用手册 1.准备工作 2.配合使用py脚本和xlsx文件 3.auto_get_lesson_pic_recognize功能介绍 4.坐标版本(不建议使用) 5.代码 自动抢课脚本使用手册 @danteking dating from 2021.12.7 and last updating at 2021.12.8 gitee仓库 github仓库 借助pyautogui库,我们可以轻松地控制鼠标.键盘以及进行图像识别,实现自动抢课的功能 1.准备工作 我们在仓库里提供了2个

随机推荐