目的
将配置文件、启动脚本、jar、依赖抽取打包成tar
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.tianzehao</groupId>
<artifactId>hiveTest</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hiveTest</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<hive.version>2.1.1</hive.version>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>
</dependencies>
<build>
<!-- 定义打包后项目jar的名称 -->
<finalName>${artifactId}</finalName>
<!-- 定义资源,以便打包时抽取配置文件 -->
<resources>
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 这里是配置打包是需要排除的文件,配置文件都排除,不然会打包到项目jar中导致外层的配置失效 -->
<excludes>
<exclude>*.yml</exclude>
<exclude>*.xml</exclude>
<exclude>*.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
assembly配置文件
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>dist</id>
<formats>
<format>tar.gz</format>
</formats>
<!-- 指定打的包是否包含打包层目录(比如finalName是hiveTest,当值为true,所有文件被放在包内的hiveTest目录下,否则直接放在包的根目录下)-->
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>bin</outputDirectory>
<directoryMode>0777</directoryMode>
<includes>
<include>**/*</include>
</includes>
<fileMode>0755</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
<!-- 将项目包放到最外层 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact> <!-- 指定打包时是否包含工程自身生成的jar包, false不包含。 在lib下剔除项目包 -->
<outputDirectory>lib</outputDirectory> <!-- 指定将这些依赖包打到包里lib目录下 -->
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
打包结果
解压如图
image.png
附录启动脚本
#!/bin/bash
app_name="hiveTest"
main_class=" org.asd.HiveQueryWithThreadPool"
LOG_DIR="/asdsd/1/logs/${app_name}"
source /etc/profile
if [ $# -eq 0 ]; then
echo "e.g.:sh $0 start|stop|restart or bash bin/service.sh start 2 or bash bin/service.sh start 2 -XX:HeapDumpPath=/heapdump.hprof"
exit
fi
JAVA_OPTS="-Dlogs.dir=${LOG_DIR} -Xmx1G -Xms1G -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/app/logs/${app_name}/heapdump.hprof"
if [ -n "$2" ] ;then
JAVA_OPTS="-Dlogs.dir=${LOG_DIR} -Dfile.encoding=utf-8 -Xmx${2}G -Xms${2}G -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/app/logs/${app_name}/heapdump.hprof"
fi
cd `dirname $0`
bin_dir=$(pwd)
project_path=${bin_dir%/*}
#load_libs
class_path="${project_path}/${app_name}.jar:${project_path}/conf:${project_path}/lib/*"
if [ $# -gt 2 ];then
JAVA_OPTS="${JAVA_OPTS} ${@:3}"
fi
function start(){
pid=$(ps -ef | grep java | grep -v grep | grep "${main_class}" | awk '{print $2}')
if [ -z "${pid}" ]; then
nohup java ${JAVA_OPTS} -cp ${class_path} ${main_class} > /dev/null 2>&1 &
echo "PID is $!"
else
echo "${app_name} is running..."
fi
}
function stop(){
pids=$(ps -ef | grep java | grep -v grep | grep "${main_class}" | wc -l)
if [ "${pids}" -eq 0 ]; then
echo "${app_name} is not running..."
else
ps -ef | grep java | grep -v grep | grep "${main_class}" | awk '{print $2}' | xargs -i kill -9 {}
echo "${app_name} is stop..."
fi
}
if [ $1 == "stop" ]; then
stop
elif [ $1 == "start" ]; then
start
elif [ $1 == "restart" ]; then
stop
start
fi
网友评论