(1). 为什么要用Maven结合Ant

通过assembly打包,可以做到一键打包,但是,不够灵活,重点是不太适合我,比如:assembly的配置信息(xml/sh…),我个人认为发版用的脚本和配置信息,应该让开发尽量无感知.

(2). pom.xml配置

<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>help.lixin.spring</groupId>
	<artifactId>spring-example</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-example</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<Version>1.1</Version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.1.0.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-netflix</artifactId>
				<version>2.1.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>


	<dependencies>
		<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<scope>test</scope>
			</dependency>
	</dependencies>

	<build>
		<plugins>
			<!--  1. 拷贝所有的依赖到:lib目录下 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.10</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.8</version>
				<executions>
					<execution>
						<id>move-config</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<!--  移动:xml/json/yml到:conf目录下 -->
							<tasks>
								<move todir="${project.build.directory}/conf" overwrite="true">
									<fileset dir="${project.build.directory}/classes"
										includes="**.xml" />
									<fileset dir="${project.build.directory}/classes"
										includes="**.properties" />
									<fileset dir="${project.build.directory}/classes"
										includes="**.yml" />	
										<fileset dir="${project.build.directory}/classes"
										includes="**.json" />
								</move>
								
								<!-- 
									后续,建议从远程下载这个文件
									<get src="http://xxxx/app.sh" dest="${project.build.directory}/bin/app.sh"/>
								 -->
								<!-- 拷贝项目目录下的文件到bin目录 -->
								<copy todir="${project.build.directory}/bin" overwrite="true">
									<fileset dir="${basedir}/src/bin"/>
								</copy>
								<chmod dir="${project.build.directory}/bin" perm="ugo+rx" includes="*.sh"/>
								<!-- 创建日志目录 -->
								<mkdir dir="${project.build.directory}/logs"/>
							</tasks>
						</configuration>
					</execution>
					<execution>
						<id>move-jar-to-lib-dir</id>
						<phase>install</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<tasks>
								<!-- 移动当前项目所产生的jar到lib目录下 -->
								<move
									file="${project.build.directory}/${project.name}-${project.version}.jar"
									tofile="${project.build.directory}/lib/${project.name}-${project.version}.jar"
									overwrite="true" />
							</tasks>
						</configuration>
					</execution>
					<!--  创建zip包 -->
					<execution>
						<id>zip</id>
						<phase>install</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<tasks>
								<zip destfile="${project.build.directory}/${project.name}-${Version}.zip" duplicate="add">
									<zipfileset dir="${project.build.directory}/bin" prefix="bin"/>
									<zipfileset dir="${project.build.directory}/conf" prefix="conf"/>
									<zipfileset dir="${project.build.directory}/lib" prefix="lib"/>
									<zipfileset dir="${project.build.directory}/logs" prefix="logs"/>
								</zip>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

(3). 生成zip文件结构如下

lixin-macbook:spring-example-1.1 lixin$ tree
.
├── bin
│   ├── app.sh
├── conf
│   ├── application.properties
│   └── logback-spring.xml
├── lib
│   ├── accessors-smart-1.2.jar
│   ├── android-json-0.0.20131108.vaadin1.jar
│   ├── asm-5.0.4.jar
│   ├── assertj-core-3.11.1.jar
│   ├── byte-buddy-1.9.3.jar
│   ├── byte-buddy-agent-1.9.3.jar
│   ├── classmate-1.4.0.jar
│   ├── hamcrest-core-1.3.jar
│   ├── hamcrest-library-1.3.jar
│   ├── hibernate-validator-6.0.13.Final.jar
│   ├── jackson-annotations-2.9.0.jar
│   ├── jackson-core-2.9.7.jar
│   ├── jackson-databind-2.9.7.jar
│   ├── jackson-datatype-jdk8-2.9.7.jar
│   ├── jackson-datatype-jsr310-2.9.7.jar
│   ├── jackson-module-parameter-names-2.9.7.jar
│   ├── javax.annotation-api-1.3.2.jar
│   ├── jboss-logging-3.3.2.Final.jar
│   ├── json-path-2.4.0.jar
│   ├── json-smart-2.3.jar
│   ├── jsonassert-1.5.0.jar
│   ├── jul-to-slf4j-1.7.25.jar
│   ├── junit-4.12.jar
│   ├── log4j-api-2.11.1.jar
│   ├── log4j-to-slf4j-2.11.1.jar
│   ├── logback-classic-1.2.3.jar
│   ├── logback-core-1.2.3.jar
│   ├── mockito-core-2.23.0.jar
│   ├── objenesis-2.6.jar
│   ├── slf4j-api-1.7.25.jar
│   ├── snakeyaml-1.23.jar
│   ├── spring-aop-5.1.2.RELEASE.jar
│   ├── spring-beans-5.1.2.RELEASE.jar
│   ├── spring-boot-2.1.0.RELEASE.jar
│   ├── spring-boot-autoconfigure-2.1.0.RELEASE.jar
│   ├── spring-boot-starter-2.1.0.RELEASE.jar
│   ├── spring-boot-starter-json-2.1.0.RELEASE.jar
│   ├── spring-boot-starter-logging-2.1.0.RELEASE.jar
│   ├── spring-boot-starter-test-2.1.0.RELEASE.jar
│   ├── spring-boot-starter-tomcat-2.1.0.RELEASE.jar
│   ├── spring-boot-starter-web-2.1.0.RELEASE.jar
│   ├── spring-boot-test-2.1.0.RELEASE.jar
│   ├── spring-boot-test-autoconfigure-2.1.0.RELEASE.jar
│   ├── spring-context-5.1.2.RELEASE.jar
│   ├── spring-core-5.1.2.RELEASE.jar
│   ├── spring-example-1.0.0-SNAPSHOT.jar
│   ├── spring-expression-5.1.2.RELEASE.jar
│   ├── spring-jcl-5.1.2.RELEASE.jar
│   ├── spring-test-5.1.2.RELEASE.jar
│   ├── spring-web-5.1.2.RELEASE.jar
│   ├── spring-webmvc-5.1.2.RELEASE.jar
│   ├── tomcat-embed-core-9.0.12.jar
│   ├── tomcat-embed-el-9.0.12.jar
│   ├── tomcat-embed-websocket-9.0.12.jar
│   ├── validation-api-2.0.1.Final.jar
│   └── xmlunit-core-2.6.2.jar
└── logs

(4). 项目下载

“Maven结合Ant打包zip项目”