一段实用的maven pom:将项目依赖打包到文件夹或打入jar中

本文介绍如何使用maven-assembly-plugin和maven-dependency-plugin插件来打包项目的依赖到一个jar文件中。通过配置插件将项目的直接依赖复制到指定目录,并创建包含所有依赖的可执行jar。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用了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命令,我们就可以得到如下结果:




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值