maven打包时拷贝依赖文件到lib

本文介绍了一种使用Maven插件进行项目依赖自动复制的方法。通过配置maven-dependency-plugin插件,在特定阶段将依赖项复制到指定目录,并且设置maven-jar-plugin来更新JAR文件中的类路径信息。这有助于确保最终构建的可执行JAR包含所有必要的依赖。

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

 <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>
                                                <groupId>org.apache.maven.plugins</groupId>
                                                <artifactId>maven-jar-plugin</artifactId>
                                                <configuration>
                                                            <archive>
                                                                        <manifest>
                                                                                    <addClasspath>true</addClasspath>
                                                                                    <classpathPrefix>lib/</classpathPrefix>
                                                                                    <mainClass>com.zctech.zcreader.main.Main</mainClass>
                                                                        </manifest>
                                                            </archive>
                                                </configuration>
                                    </plugin>
                        </plugins>
            </build>
当你使用Maven进行项目打包,通常默认情况下,它会将项目的依赖括`pom.xml`文件中的`<dependencies>`部分)一起含到最终的JAR或WAR文件中。如果你希望将系统的(比如`java-sdk`, `junit`等)而不是项目的依赖也一并打包,你需要做以下操作: 1. **手动配置**: - 使用maven-shade-plugin插件,在`pom.xml`中添加以下配置,这将会"shade"或合并所有的依赖括系统库)到一个新的独立的Jar中: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/services/*</resource> </transformer> <!-- 添加更多需要复制的资源 --> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 确保添加了正确的`<resource>`标签来指定哪些资源需要被复制。 2. **排除不需要的**: 如果你想保留特定系统库,可以设置`excludes`属性来排除那些不想打包依赖: ```xml <dependency> <groupId>org.example</groupId> <artifactId>system-dep</artifactId> <scope>system</scope> <systemPath>${project.basedir}/lib/system-lib.jar</systemPath> <optional>true</optional> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>other-system-dep</artifactId> <version>1.0</version> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> ``` 3. **注意**: 对于系统路径(system path)依赖,它们不会通过Maven仓库管理,所以如果部署到其他环境中,可能需要保证系统库文件的存在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值