如何为Maven项目添加本地测试依赖且不提交至pom.xml文件?
本地测试依赖的无提交解决方案
完全可以用次级pom.xml文件来存放本地测试依赖,同时通过Git忽略避免提交,另外还有更简便的Maven Profile方案,下面分别说明:
方法一:使用次级pom.xml文件
- 步骤1:创建本地专属的pom文件,比如命名为
pom-local.xml,在里面添加测试依赖:
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <parent> <groupId>你的项目groupId</groupId> <artifactId>你的项目artifactId</artifactId> <version>你的项目版本</version> </parent> <dependencies> <!-- 本地测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies> </project>
- 步骤2:把
pom-local.xml加入Git忽略:
编辑项目根目录下的.git/info/exclude文件,添加一行:pom-local.xml
这个文件和.gitignore功能类似,但只对当前本地仓库生效,不会提交到远程。 - 步骤3:使用时,执行Maven命令指定这个本地pom:
mvn clean install -f pom-local.xml
也可以在IDE的Maven运行配置里指定使用该pom文件。
方法二:使用Maven Profile(更推荐)
不用额外创建pom文件,直接在主pom.xml中添加本地专属的Profile,配合本地Maven配置就能实现需求:
- 步骤1:在主
pom.xml中添加一个仅本地激活的Profile:
<profiles> <profile> <id>local-dev</id> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies> </profile> </profiles>
- 步骤2:创建本地Maven配置文件
~/.m2/settings.xml(没有的话直接新建),添加Profile自动激活:
<settings> <activeProfiles> <activeProfile>local-dev</activeProfile> </activeProfiles> </settings>
主pom.xml的这个修改不需要提交(因为该Profile默认不会激活,只有本地配置了才会生效),而settings.xml是本地专属文件,本来就不会被Git追踪,完美解决你的需求。
内容的提问来源于stack exchange,提问作者ooomid
相关产品推荐
相关产品推荐

