如何在 Maven 中执行 git pull?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6359332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:10:59  来源:igfitidea点击:

How can I do a git pull in Maven?

gitmavenpom.xmlmaven-scm

提问by user786045

I'm new to both maven and git and wanted to get some help in setting a project.

我是 maven 和 git 的新手,想在设置项目时获得一些帮助。

Is there a way to define a goal in the pom to push/pull from git during linked to a maven phase? For example, can I pull from git during the maven install phase?

有没有办法在 pom 中定义一个目标,以便在链接到 maven 阶段期间从 git 推/拉?例如,我可以在 maven 安装阶段从 git 中提取吗?

If yes, how can that be accomplished? I would appreciate any code examples.

如果是,如何实现?我将不胜感激任何代码示例。

回答by Vítor E. Silva Souza

Good idea or not, here's how you could do a checkout (pullclone from Github) using Maven. Have your pom.xml look like this:

好主意与否,这是您如何进行结帐(从 Github 克隆)使用 Maven。让你的 pom.xml 看起来像这样:

<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>br.com.javamagazine</groupId>
<artifactId>engesoft</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>engesoft Maven Webapp</name>
<url>http://maven.apache.org</url>

<scm>
    <connection>scm:git:git://github.com/vitorsouza/EngeSoft.git</connection>
    <developerConnection>scm:git:https://[email protected]/vitorsouza/EngeSoft.git</developerConnection>
    <url>https://github.com/vitorsouza/EngeSoft</url>
</scm>
</project>

Then use mvn scm:checkout. When I ran this, it pulled the code to folder target/engesoft. There's probably a way to configure it to place it somewhere else. Check out the following pages:

然后使用mvn scm:checkout. 当我运行它时,它将代码拉到文件夹target/engesoft. 可能有一种方法可以将其配置为将其放置在其他地方。查看以下页面:

回答by Aaron Digulla

Instead of doing this during the Maven build, use a CI server like Jenkins. It will do the git pullbefore running maven, so the build tool can concentrate on it's main purpose: Building source code.

不要在 Maven 构建期间这样做,而是使用像Jenkins这样的 CI 服务器。它将git pull在运行 maven 之前完成,因此构建工具可以专注于它的主要目的:构建源代码。

This also makes it more simple for you to develop since pull's will only happen when you want them. If you pull all the time, another developer can change something and you will get errors that you don't expect.

这也使您的开发变得更加简单,因为 pull 只会在您需要时发生。如果你一直拉,另一个开发者可以改变一些东西,你会得到你意想不到的错误。

回答by James Evans

The scm:checkout goalVitor is referring to is a clone, not a pull (huge difference in Git).

Vitor 所指的scm:checkout 目标是克隆,而不是拉取(Git 中的巨大差异)。

I had to use the exec goal to do what you're describing. I also didn't want to do an entire clone each time there was a build. Instead, I use git reset --hardand then pull -f origin Release:Release(via exec).

我不得不使用 exec 目标来做你所描述的。我也不想在每次构建时都做一个完整的克隆。相反,我使用git reset --hard然后pull -f origin Release:Release(通过 exec)。

If I find a better way (and there HAS to be one) I'll post it here.

如果我找到更好的方法(并且必须有一个),我会在这里发布。