1. 程式人生 > >一段實用的maven pom:將專案依賴打包到資料夾或打入jar中

一段實用的maven pom:將專案依賴打包到資料夾或打入jar中

使用了maven-assembly-plugin外掛和maven-dependency-plugin:

<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/maven-v4_0_0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <groupId>net.aty.deps</groupId>  
    <artifactId>MyJar</artifactId>  
    <packaging>jar</packaging>  
    <version>0.0</version>  
  
    <dependencies>  
        <dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.6</version>
		</dependency>
    </dependencies>  
  
    <build>  
		<plugins> 
			
			<plugin>
			    <groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>
			
			
			<plugin>  
				<artifactId>maven-assembly-plugin</artifactId>  
				<configuration>  
					<descriptorRefs>  
						<descriptorRef>jar-with-dependencies</descriptorRef>  
					</descriptorRefs>  
					<archive>  
						<manifest>  
						  <mainClass></mainClass>  
						</manifest>  
					</archive>  
				</configuration>  
			    <executions>  
					<execution>  
						<id>make-assembly</id>  
						<phase>package</phase>  
						<goals>  
							<goal>single</goal>  
						</goals>  
					</execution>  
			  </executions>  
			</plugin> 
			
		</plugins>
    </build>  
</project> 

執行mvn clean package命令,我們就可以得到如下結果: