您的位置:首页 > Web前端

Maven打包Spring boot,把依赖和配置文件及前端代码提取到jar文件外

2017-12-12 20:43 701 查看
运用到maven如下插件:

maven-jar-plugin,负责将应用程序打包成可执行的jar文件 
maven-assembly-plugin,负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署 

maven-compiler-plugin,负责编译项目

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



打包后目录如上,依赖都在lib文件夹中



代码目录结构如上,最终的可运行文件jar文件并不包含依赖的jar包,所有依赖的jar包都放在和XM003.jar平行的lib文件夹内,

这样部署的文件比较规整,如果有文件上传功能,程序可以找到前端目录,放置进去。

在pom文件中maven-jar-plugin的配置文件如下:

[html] view
plain copy

<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.sd.xm003.XM003Application</mainClass>  

                            <addClasspath>true</addClasspath>  

                            <classpathPrefix>lib/</classpathPrefix>  

                        </manifest>  

                        <manifestEntries>  

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

                        </manifestEntries>  

                    </archive>  

                    <excludes>  

                        <exclude>mapper/**</exclude>  

                        <exclude>static/**</exclude>  

                        <exclude>templates/**</exclude>  

                        <exclude>*.txt</exclude>  

                        <exclude>*.xml</exclude>  

                        <exclude>*.properties</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文件中maven-assembly-plugin配置如下:

[html] view
plain copy

<plugin>    

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

                <configuration>    

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

                    <appendAssemblyId>false</appendAssemblyId>    

                    <descriptors>    

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

                    </descriptors>    

                </configuration>    

                <executions>    

                    <execution>    

                        <id>make-assembly</id>    

                        <phase>package</phase>    

                        <goals>    

                            <goal>single</goal>    

                        </goals>    

                    </execution>    

                </executions>    

            </plugin>    

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

[html] view
plain copy

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

        <fileSet>    

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

            <outputDirectory>/</outputDirectory>    

        </fileSet>  

        <fileSet>    

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

            <outputDirectory>/</outputDirectory>    

            <includes>    

                <include>*.jar</include>    

            </includes>    

        </fileSet>    

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

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

下面的fileSets中配置了我需要将那些文件打包到我的最终压缩包中,  
相关的配置文件src/main/resources,里面放着整个程序提取的properties等相关的配置文件 
最终可运行的jar文件,使用了${project.build.directory}变量,也就是通过maven-jar-plugin生成的那个jar文件 
dependencySets里面配置了依赖库最终输出到lib文件夹下,与上面的maven-jar-plugin配置生成的manifest文件路径相对应,这样可运行jar就会按照manifest的路径来找相应的文件进行加载 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐