Maven打成Zip包

如果当前项目是使用Maven管理,并且需要打成Zip,按平时的配置是不行,不过实现打成Zip也不复杂,只需要2步即可。

 

1、在src/main/resources下面创建一个自定义打包的配置文件,名为assembly.xml

<assembly>
	<!-- 项目打成Zip包的Id -->
	<id>bin</id>
	<formats>
		<!-- 包的后缀 -->
		<format>zip</format>
	</formats>
	<dependencySets>
		<!-- 项目依赖包的输出目录,下面为当前目录的lib文件夹下 -->
		<dependencySet>
			<outputDirectory>/lib</outputDirectory>
		</dependencySet>
	</dependencySets>
	<fileSets>
		<!-- 项目一些说明的资源输出的目录,下面为当前目录 -->
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>/</outputDirectory>
			<!-- 指定那些文件需要复制进该目录下-->
			<includes>
				<include>README.txt</include>
				<include>LICENSE</include>
				<include>COPYRIGHT</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>/config</outputDirectory>
			<includes>
				<include>configuration.xml</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>/bin</outputDirectory>
			<includes>
				<include>*.sh</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>

 

2、在pom.xml中使用上面的文件进去打包,配置如下:

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
     <archive>
      <manifest>
       <addClasspath>true</addClasspath><!-- 把依赖包的路径设置为在lib文件夹下 -->
       <mainClass>
        main    <!--启动的Main类-->   
</mainClass>
      </manifest>
     </archive>
    </configuration>
   </plugin>
<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.8</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>lib/</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>
								false
							</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.2.1</version>
				<configuration>
					<descriptors>
						<descriptor>src/main/resources/assembly.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

 

你可能感兴趣的:(maven)