如何通过mvn install自动从GitHub下载并本地安装Maven依赖?
解决方案:直接执行
mvn install完成依赖安装与项目构建 问题根源在于:Maven在启动任何生命周期阶段(包括你绑定的validate)之前,会先执行依赖解析与可用性检查。这意味着你的maven-scm-plugin还没来得及克隆并安装依赖,Maven就已经检测到依赖缺失并报错了。
要实现仅执行mvn install完成所有操作,你需要把依赖安装的步骤放到依赖解析之前的阶段执行,以下是两种可行方案:
方案一:使用maven-invoker-plugin调用独立POM完成依赖安装
- 在项目根目录创建
dependencies-bootstrap/pom.xml,将原有的maven-scm-plugin配置迁移到这个文件中:
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>your.group.id</groupId> <artifactId>dependencies-bootstrap</artifactId> <version>1.0.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <version>2.0.0-M3</version> <configuration> <mavenHome>${maven.home}</mavenHome> <goals>install</goals> </configuration> <executions> <execution> <id>clone_and_install_1st_dependency</id> <phase>validate</phase> <configuration> <connectionUrl>scm:git:https://github.com/my_git/1st_dependency.git</connectionUrl> <scmVersionType>tag</scmVersionType> <scmVersion>${1st_dependency.version}</scmVersion> </configuration> <goals> <goal>bootstrap</goal> </goals> </execution> <execution> <id>clone_and_install_2nd_dependency</id> <phase>validate</phase> <configuration> <connectionUrl>scm:git:https://github.com/my_git/2nd_dependency.git</connectionUrl> <scmVersionType>tag</scmVersionType> <scmVersion>${2nd_dependency.version}</scmVersion> </configuration> <goals> <goal>bootstrap</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
- 在主项目的
pom.xml中添加maven-invoker-plugin配置,指定在pre-clean阶段(早于依赖解析)执行这个引导POM:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-invoker-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <id>install-external-dependencies</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <pom>dependencies-bootstrap/pom.xml</pom> <goals> <goal>scm:bootstrap</goal> </goals> <properties> <1st_dependency.version>${1st_dependency.version}</1st_dependency.version> <2nd_dependency.version>${2nd_dependency.version}</2nd_dependency.version> </properties> </configuration> </execution> </executions> </plugin>
方案二:使用exec-maven-plugin直接执行命令行操作
无需额外POM,直接在主项目pom.xml中配置exec-maven-plugin,绑定到pre-clean阶段执行克隆与安装命令:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>install-1st-dependency</id> <phase>pre-clean</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>bash</executable> <arguments> <argument>-c</argument> <argument> git clone --branch ${1st_dependency.version} https://github.com/my_git/1st_dependency.git && cd 1st_dependency && mvn install -DskipTests && cd .. && rm -rf 1st_dependency </argument> </arguments> </configuration> </execution> <execution> <id>install-2nd-dependency</id> <phase>pre-clean</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>bash</executable> <arguments> <argument>-c</argument> <argument> git clone --branch ${2nd_dependency.version} https://github.com/my_git/2nd_dependency.git && cd 2nd_dependency && mvn install -DskipTests && cd .. && rm -rf 2nd_dependency </argument> </arguments> </configuration> </execution> </executions> </plugin>
注意事项
- 方案二需要环境中已安装
git和bash(Windows环境可替换为cmd并调整命令格式) - 可根据需要添加
-DskipTests或其他Maven参数优化依赖安装过程
内容的提问来源于stack exchange,提问作者Fabrizio
相关产品推荐
相关产品推荐

