maven 打包的同时将生成的jar复制到指定路径

<build>
    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
    <testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-lib-src-webapps</id>
                    <phase>package</phase>
                    <configuration>
                        <tasks>
                           <!-- <delete dir="src/main/webapp/WEB-INF/lib" />-->
                            <copy todir="${project.web-common.lib-targetPath}">
                                <fileset dir="${project.commonmodule.lib-source.directory}">
                                    <include name="*" />
                                </fileset>
                            </copy>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 目的:将打包后生成的jar复制到指定路径
阻碍:命令mvn package后,才生成jar的包,如果单纯的用resources方法复制文件,是无法在package时复制一个还未存在的东西到某地的。
解决:添加antrun plugin,达到更改打包和复制的顺序的目的,使打包之后再复制成为可能。
PS:在project中properties里规定的路径,project的module可以理解使用。

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.jw.system</groupId>
		<artifactId>system-parent</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<groupId>com.jw.system.base</groupId>
	<artifactId>system-base</artifactId>
	<version>1.0-SNAPSHOT</version>
	<name>system-base</name>
	<url>http://maven.apache.org</url>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.4</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.deploy.lib}</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<phase>package</phase>
						<configuration>
							<tasks>
								<copy todir="${project.deploy.lib}">
									<fileset dir="${project.build.directory}">
										<include name="*.jar" />
									</fileset>
								</copy>
							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-install-plugin</artifactId>
				<version>2.4</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>install-file</goal>
						</goals>
						<configuration>
							<packaging>jar</packaging>
							<artifactId>${project.artifactId}</artifactId>
							<groupId>${project.groupId}</groupId>
							<version>${project.version}</version>
							<file>${project.deploy.lib}/${project.artifactId}-${project.version}.jar</file>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 

猜你喜欢

转载自awen7916.iteye.com/blog/2240503