1. 修改pom.xml
pom.xml中设置以下:html
<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>com.zd</groupId> <artifactId>logETL</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>logETL</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <base.dir>/</base.dir> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.2</version> </dependency> <dependency> <!-- 桥接:告诉Slf4j使用Log4j2 --> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> </dependencies> <build> <finalName>stormETL</finalName> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>assembly.xml</exclude> </excludes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>c3p0-config.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <!-- appendAssemblyId属性控制是否在生成的打包文件的文件名中包含assembly id --> <appendAssemblyId>false</appendAssemblyId> <!-- 这个jar-with-dependencies是assembly预先写好的一个assembly.xml,就不须要咱们本身再写了。 --> <!--<descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs>--> <!-- 设置META-INF/MANIFEST.MF中的Main-Class--> <archive> <manifest> <mainClass>com.cmsz.RunMain</mainClass> </manifest> </archive> <!-- 使用自定义的assembly.xml(文件名随意 XX.xml便可) --> <descriptors> <descriptor>src/main/resources/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <!-- 将assembly绑定到package生命周期上 --> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
其中<artifactId>maven-assembly-plugin</artifactId>的maven-assembly-plugin是这个插件的标准命名,在maven2.0.*中带的默认版本是java
execution的设置是为了将maven-assembly-plugin继承到标准的maven打包过程当中,这样在运行maven-package时就会执行maven-assembly-plugin的操做,从而实现咱们须要的自定义打包。mysql
2. assemble descriptor file
个人src/main/resources/assembly.xml内容以下:sql
<assembly> <!-- 最终生成的包名称为artifactId-version-id.format, id主要用来区分包是由哪一个assembly文件生成的,本例中 artifactId:test version:1.0.0 id:bin format:tar.gz 最终将会在target文件夹下生成一个名为test-1.0.0-bin.tar.gz的包 --> <id>bin</id> <!-- zip, tar, tar.gz, tar.bz2, jar, dir, war 也是支持的 --> <formats> <!-- 能够同时打多种格式的包 --> <!--<format>tar.gz</format>--> <!-- <format>zip</format> --> <format>jar</format> </formats> <!-- 默认值是true,此时将建立一个名为artifactId-version的根文件夹, 全部outputDirectory标签指定的路径将变为相对于该文件夹的路径。 例如:outputDirectory的值为/test,则最终径为/artifactId-version/test 若是includeBaseDirectory的值是false,则最终路径为/test --> <includeBaseDirectory>false</includeBaseDirectory> <!-- 将指定路径下全部文件输出到指定目录--> <fileSets> <!-- 若是存在fileSet标签,而且在标签中没有使用includes标签指定打入包中的文件, 默认状况下,工程中的全部文件(源文件、编译后产生的.class文件、配置文件等) 都会被打入包中 --> <fileSet> <outputDirectory>/</outputDirectory> <directory>src/main/java</directory> </fileSet> </fileSets> <!-- 将指定文件输出到指定目录--> <files> <file> <!-- source不能使用模糊匹配,必须是特定的文件 --> <source>src/main/java/c3p0-config.xml</source> <outputDirectory>/</outputDirectory> </file> </files> <dependencySets> <dependencySet> <!-- true是默认值,本次构建出来的jar包属于当前这个dependencySet, 此时jar包将会被添加至新的jar包中 --> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>/</outputDirectory> <!-- 定义了是否解压依赖包,若是为true,会解压出依赖包中的class文件,反之,则不进行解压 --> <unpack>true</unpack> </dependencySet> <dependencySet> <!-- 本次构建出来的jar包是否包含当前工程 --> <useProjectArtifact>false</useProjectArtifact> <outputDirectory>/test</outputDirectory> <!-- 定义了是否解压依赖包,若是为true,会解压出依赖包中的class文件,反之,则不进行解压 --> <unpack>true</unpack> <!-- 限定了对哪些依赖包进行操做(依赖包scope的值是在pom.xml中定义的) --> <scope>test</scope> </dependencySet> <dependencySet> <useProjectArtifact>false</useProjectArtifact> <outputDirectory>/provided</outputDirectory> <unpack>true</unpack> <!-- 限定了对哪些依赖包进行操做(依赖包scope的值是在pom.xml中定义的) --> <scope>provided</scope> </dependencySet> </dependencySets> </assembly>
详细的语法不介绍了,请参考官方指南,有很是详尽的说明:Assembly Descriptor Format reference
简单解释一下:
1) formatapache
format=zip设置打包的最终文件格式为zip. 支持的其余格式还有gz,tar,tar.gz,tar.bz2。
2) filesetapi
<fileSet> <directory>src/main/bin</directory> <outputDirectory>/</outputDirectory> </fileSet>
将src/main/bin目录下的文件打包到根目录(/)下.app
<fileSet> <directory>src/main/bin</directory> <outputDirectory>config</outputDirectory> </fileSet>
将src/main/config目录下的文件打包到config下.
3) dependencySetsmaven
<dependencySet> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> </dependencySet>
将scope为runtime的依赖包打包到lib目录下。ide