在pom.xml的plugins里添加一个plugin:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <!--<version>2.5.5</version>--> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> <execution> <id>make-jar</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>src/main/assembly/job.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
然后在src/main目录下新建assembly目录,在assembly目录下新建一个job.xml文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>job</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <files> <file> <source>target/com.fashici-0.0.1-SNAPSHOT-jar-with-dependencies.jar</source> <outputDirectory>/</outputDirectory> </file> <file> <source>./data.json</source> <outputDirectory>/</outputDirectory> </file> </files> </assembly>
然后执行mvn package命令就可以了。
如果要打包目录,可以使用fileSet域,格式如下:
<?xml version="1.0" encoding="UTF-8"?> <assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>job</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <files> <file> <source>target/${artifactId}-${version}-jar-with-dependencies.jar</source> <outputDirectory>resources/</outputDirectory> </file> </files> <fileSets> <fileSet> <directory>src/main/script</directory> <outputDirectory>script/</outputDirectory> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>resources/</outputDirectory> </fileSet> </fileSets> </assembly>
在job.xml的files下配置要打包到zip里的文件。