天天看点

通过Maven构建打包Spring boot,并将config配置文件提取到jar文件外

如果通过不同的IDE打包,着实会觉得依赖性太大,并且容易出现错误,操作也比较复杂 

同时,spring-boot-maven-plugin的使用感觉,相关配置太少,并且无法满足方便部署和运行的需求。 

这里我们使用了,Maven的如下插件 

maven-jar-plugin,负责将应用程序打包成可执行的jar文件 

maven-assembly-plugin,负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署 

需求1,将依赖的jar提取到可运行的jar文件之外,我们使用maven-jar-plugin来实现 

比如我的项目最终的打包目录如下 下载

代码目录结构如下 

最终的可运行文件jar文件并不包含依赖的jar包,所有依赖的jar包都放在和ps.jar平行的lib文件夹内,这样如果以后想快速部署,就不用每一次都把体积很大的lib包都要传一遍,除非依赖包有所变化,当然这些都是后续如果想这么做的前提,我这里还是为了使部署的文件比较规整 

这里的maven-jar-plugin的配置文件如下 

Xml代码  下载

<plugin>  

                <groupId>org.apache.maven.plugins</groupId>  

                <artifactId>maven-jar-plugin</artifactId>  

                <version>2.6</version>  

                <configuration>  

                    <archive>  

                    <!-- 添加index则不从mainfest中读取classpath,而是从Index.list中读取 -->  

<!--                         <index>true</index> -->  

                        <manifest>  

                            <mainClass>com.vmpay.pay.App</mainClass>  

                            <!-- to create a class path to your dependecies you have to fill true   

                                in this field -->  

                            <addClasspath>true</addClasspath>  

                            <classpathPrefix>lib/</classpathPrefix>  

                            <!--<classpathLayoutType>custom</classpathLayoutType> <customClasspathLayout>   

                                lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension} </customClasspathLayout> -->  

                        </manifest>  

                        <manifestEntries>  

                            <Class-Path>./</Class-Path>  

                        </manifestEntries>  

                    </archive>  

                    <excludes>  

                        <exclude>config/**</exclude>  

                    </excludes>  

                </configuration>  

            </plugin>  

其中manifest的部分是核心,在可执行的jar文件中,打包后会在jar文件内的META-INF文件夹下,生成一个MANIFEST.MF文件,里面记录了可执行文件的一些相关配置,比如像上面一段代码中所配置的内容,这里面就配置了可执行jar文件未来读取classpath的相对目录位置在什么地方,以及引入的jar文件都有哪些,上面的配置就是classpath目录是./(稍后会解释为什么) 

mainClass配置表示,哪个class作为程序的入口来执行 

addClasspath配置表示,是否将依赖的classpath一起打包 

classpathPrefix配置表示,依赖的classpath的前缀,也就是打包后生成的MANIFEST.MF文件里,引入的jar文件都会加上前缀,lib/,比如fastjson-1.2.7.jar,在mainfest文件里就会是lib/fastjson-1.2.7.jar 

excludes配置表示,排除哪些文件夹不被打包进去 

其实maven-jar-plugin主要就是配置了MANIFEST.MF这个文件而已,就是让可执行文件知道自己怎么执行,加载哪些文件执行的描述,剩下的工作交由maven-assembly-plugin来处理 

在pom文件中配置类似如下 

                <artifactId>maven-assembly-plugin</artifactId>  

                    <!-- not append assembly id in release file name -->  

                    <appendAssemblyId>false</appendAssemblyId>  

                    <descriptors>  

                        <descriptor>src/main/build/package.xml</descriptor>  

                    </descriptors>  

                <executions>  

                    <execution>  

                        <id>make-assembly</id>  

                        <phase>package</phase>  

                        <goals>  

                            <goal>single</goal>  

                        </goals>  

                    </execution>  

                </executions>  

重点的就是package.xml的路径了,使用maven-assembly-plugin的相关配置实际上都在这个文件里面 

package.xml的文件内容 

<?xml version="1.0" encoding="UTF-8"?>  

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">  

    <id>package</id>  

    <formats>  

        <format>zip</format>  

    </formats>  

    <includeBaseDirectory>true</includeBaseDirectory>  

    <fileSets>  

        <fileSet>  

            <directory>bin</directory>  

            <outputDirectory>/</outputDirectory>  

        </fileSet>  

            <directory>src/main/resources</directory>  

            <directory>${project.build.directory}</directory>  

            <includes>  

                <include>*.jar</include>  

            </includes>  

    </fileSets>  

    <dependencySets>  

        <dependencySet>  

            <outputDirectory>lib</outputDirectory>  

            <scope>runtime</scope>  

<!--             <unpack>false</unpack> -->  

            <excludes>  

<!--                 <exclude>${project.name}-${project.version}</exclude> -->  

                <exclude>${groupId}:${artifactId}</exclude>  

            </excludes>  

        </dependencySet>  

    </dependencySets>  

</assembly>  

其他相关配置可参看官方文档 下载

[url] 

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_unpackOptions 

[/url] 

这里面我配置了,最终压缩的文件格式,为zip,也就是最终打包出来的是一个zip的文件,然后发布到服务器上进行解压部署,相关我要的配置都在这个压缩包内,解压即可直接使用 

下面的fileSets中配置了我需要将那些文件打包到我的最终压缩包中, 

我的配置文件包括了启动脚本bin文件夹,里面放着shell的启动脚本, 

相关的配置文件src/main/resources,里面放着整个程序提取的properties等相关的配置文件 

最终可运行的jar文件,使用了${project.build.directory}变量,也就是通过maven-jar-plugin生成的那个jar文件 

dependencySets里面配置了依赖库最终输出到lib文件夹下,与上面的maven-jar-plugin配置生成的manifest文件路径相对应,这样可运行jar就会按照manifest的路径来找相应的文件进行加载 

start.sh 

Shell代码  下载

###启动  

#!/bin/sh  

moduleName="ps"  

pidPath="/var/run/$moduleName-tpid"  

rm -f $pidPath  

nohup java -jar ./$moduleName.jar -server -Xms1024m -Xmx2048m -Xss256k > ./run.log 2>&1 &  

echo $! > $pidPath  

stop.sh 

###停止  

tpid=`cat /var/run/$moduleName-tpid | awk '{print $1}'`  

tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`  

if [ ${tpid} ]; then  

kill -9 $tpid  

fi  

继续阅读