maven 自定义打包

配置 pom.xml
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId> <!-- 官网给出的配置,没有配置 groupId,这里也不配置 -->
				<version>2.6</version>
				<executions>
					<execution>
						<id>make-assembly</id> <!-- ID 标识,命名随意 -->
						<phase>package</phase> <!-- 绑定到 PACKAGE 生命周期阶段 -->
						<goals>
							<goal>single</goal>  <!-- 在 PACKAGE 生命周期阶段仅执行一次 -->
						</goals>
					</execution>
				</executions>
				<configuration>
					<descriptors>
						<descriptor>assembly.xml</descriptor> <!-- 自定义打包的配置文件 -->
					</descriptors>
					<appendAssemblyId>false</appendAssemblyId> <!-- 设为 FALSE, 防止 WAR 包名加入 assembly.xml 中的 ID -->
				</configuration>
			</plugin>




		<profile>
			<id>PROD</id> <!-- ID 标识符 -->
			<properties>
				<env>product</env> <!-- properties 定义 key-value, 这里 key 是 env, value 是 PROD -->
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault> <!-- 默认激活 -->
			</activation>
		</profile>
		<profile>
			<id>DEV</id> <!-- ID 标识符 -->
			<properties>
				<env>dev</env> <!-- properties 定义 key-value, 这里 key 是 env, value 是dev -->
			</properties>
		</profile>
		<profile>
			<id>TEST</id> <!-- ID 标识符 -->
			<properties>
				<env>test</env> <!-- properties 定义 key-value, 这里 key 是 env, value 是 test -->
			</properties>
		</profile>


配置 assembly.xml


<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 标识,命名随意 -->
	<id>${project.artifactId}-assembly-${project.version}</id>
	<!-- 默认为 TRUE, 设为 FALSE, 防止将 ${project.finalName} 作为根目录打进 WAR 包 -->
	<!-- TRUE 结构: ${project.finalName}.war/${project.finalName}/WEB-INF -->
	<!-- FALSE 结构: ${project.finalName}.war/WEB-INF -->
	<includeBaseDirectory>false</includeBaseDirectory>
	<!-- 设置为 WAR 包格式 -->

	<formats>
		<format>war</format>
	</formats>

	<fileSets>
		<fileSet>
			<directory>${project.build.outputDirectory}</directory> <!-- target/classes -->
			<outputDirectory>WEB-INF/classes</outputDirectory>
			<excludes>
				<exclude>jdbc.properties</exclude>
				
				<exclude>log4j.properties</exclude>
			</excludes>
		</fileSet>
		<fileSet>
			<directory>${project.basedir}/env/${env}</directory>
			<outputDirectory>WEB-INF/classes/</outputDirectory>
		</fileSet>
		<!-- 将 webapp 下的文件输出到 WAR 包 -->
		<fileSet>
			<directory>${project.basedir}/src/main/webapp</directory>
			<outputDirectory>/</outputDirectory>
		</fileSet>
	</fileSets>
	<!-- 将项目依赖的JAR包输出到 WEB-INF/lib -->
	<dependencySets>
		<dependencySet>
			<useProjectArtifact>false</useProjectArtifact>
			<outputDirectory>WEB-INF/lib</outputDirectory>
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
</assembly>


通过以下命令运行生产、测试、开发环境

猜你喜欢

转载自gangling.iteye.com/blog/2305575