运用到maven如下插件:
maven-jar-plugin,负责将应用程序打包成可执行的jar文件
maven-assembly-plugin,负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署
maven-compiler-plugin,负责编译项目
将依赖的jar提取到可运行的jar文件之外,使用maven-jar-plugin来实现
![](https://i-blog.csdnimg.cn/blog_migrate/207b96ec217d50a61e9f27e7bb19d19e.png)
打包后目录如上,依赖都在lib文件夹中
![](https://i-blog.csdnimg.cn/blog_migrate/207b96ec217d50a61e9f27e7bb19d19e.png)
代码目录结构如上,最终的可运行文件jar文件并不包含依赖的jar包,所有依赖的jar包都放在和XM003.jar平行的lib文件夹内,
这样部署的文件比较规整,如果有文件上传功能,程序可以找到前端目录,放置进去。
在pom文件中maven-jar-plugin的配置文件如下:
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <archive>
-
-
- <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配置如下:
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
-
- <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的文件内容 :
- <?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>
-
- <excludes>
-
- <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的路径来找相应的文件进行加载