如何使用 Gitlab CI 构建 Java Maven 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33430487/
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-08-11 14:40:24  来源:igfitidea点击:

How to use Gitlab CI to build a Java Maven project?

javaspringmavengitlab-cigitlab-omnibus

提问by MRK187

I have been experimenting with no success whatsoever, I am running a Gitlab hosted on Linux, and trying to get my head around the CI functionality.

我一直在尝试但没有任何成功,我正在运行托管在 Linux 上的 Gitlab,并试图了解 CI 功能。

According to the Gitlab documentation you only need to create a .gitlab-ci.ymlfile, the Gitlab implementation of Travis-CI. Now from the looks of it you can accomplish a lot with the .gitlab-ci.yml, but a lot of the documentation is referencing Ruby and other languages. Nothing is said about how to build Java Maven projects.

根据 Gitlab 文档,您只需要创建一个.gitlab-ci.yml文件,即 Travis-CI 的 Gitlab 实现。现在从它的外观来看,您可以使用 完成很多工作.gitlab-ci.yml,但是很多文档都引用了 Ruby 和其他语言。没有说如何构建 Java Maven 项目。

How can I build a simple application in Java? Can I use the shared runner, or should I be using a specific runner, in that case what or which runner implementation should I choose: ssh, docker, or shell? Then, what should I put in the .gitlab-ci.ymlfile at least to build the project with Maven?

如何用 Java 构建一个简单的应用程序?我可以使用共享运行器,还是应该使用特定的运行器,在这种情况下,我应该选择什么或哪种运行器实现:ssh、docker 或 shell?那么,我.gitlab-ci.yml至少应该在文件中放入什么才能使用Maven构建项目?

回答by Mark O'Connor

The documentation describes the YAML syntax used to control builds:

该文档描述了用于控制构建的 YAML 语法:

So why don't you try starting with the following?:

那么为什么不尝试从以下开始呢?:

job1:
  script: "mvn package"

Presumably this will only work if Maven is already installed, so you'll need a runnerthat supports this.

据推测,如果已经安装了Maven的,这只会工作,所以你需要一个亚军支持此。

I have not used GitLab but the documentationsuggests you could further customize it to use the official Maven Docker imageto perform the builds. Looks very interesting, but I'd agree documentation is missing a Java example.

我没有使用过 GitLab,但文档建议您可以进一步自定义它以使用官方 Maven Docker 映像来执行构建。看起来很有趣,但我同意文档缺少 Java 示例。

回答by rolve

Register a Docker runnerand use one of the official Maven Docker images, e.g., maven:3-jdk7in your .gitlab-ci.ymlfile:

注册 Docker 运行程序并使用官方 Maven Docker 映像之一,例如,maven:3-jdk7在您的.gitlab-ci.yml文件中:

image: maven:3-jdk-7

build:
  script: "mvn install -B"

Note the -Bflag, which is recommended for non-interactive use.

请注意-Bflag,推荐用于非交互式使用。

As far as I understand, it does not matter whether the runner is shared or specific.

据我了解,跑步者是共享的还是特定的都没有关系。

回答by emeraldjava

I use this command but in general the documentation on java/maven builds seems quite rare

我使用这个命令,但一般来说关于 java/maven 构建的文档似乎很少见

maven-package:
  script: "mvn install -B"

回答by Milan Savaliya

I would like to add bit of information here guys. First let's clear out some confusion regarding shared and specific runner.

我想在这里添加一些信息。首先让我们澄清一些关于共享和特定跑步者的困惑。

Shared Runner:As its name sound, shared runners are the build process flow instances which can be used to execute jobs of every single project in your installed gitlab instance having Allowed Shared runnersoption enabled. To do that of-course you would need administrative rights. As per current gitlab documentation, only use with administrative rights is able to define shared runner.

Shared Runner:顾名思义,shared runners 是构建流程实例,可用于在已安装的 gitlab 实例中执行每个项目的作业,并启用Allowed Shared runners选项。要做到这一点,您当然需要管理权限。根据当前的 gitlab 文档,只有使用管理权限才能定义共享运行程序。

specific runnerThis kind of runner executes jobs of only one project.

特定运行器这种运行器只执行一个项目的作业。

Also, these are few important points to keep in mind when choosing runner for your projects.

此外,在为您的项目选择 runner 时,需要牢记这些要点。

  1. Shared Runnersare useful for jobs that have similar requirements, between multiple projects. Rather than having multiple runners idling for many projects, you can have a single or a small number of runners that handle multiple projects. This makes it easier to maintain and update runners for common set of projects.
  2. Specific runnersare useful for jobs that have special requirements or for projects with a specific demand. If a job has certain requirements, you can set up the specific runner with this in mind, while not having to do this for all runners. For example, if you want to deploy a certain project, you can setup a specific runner to have the right credentials for this.
  1. 共享运行器对于在多个项目之间具有相似要求的作业非常有用。与其让多个跑步者为多个项目闲置,您可以让单个或少数几个跑步者处理多个项目。这使得维护和更新通用项目集的运行器变得更加容易。
  2. 特定运行器对于具有特殊要求的工作或具有特定需求的项目非常有用。如果工作有某些要求,您可以考虑到这一点来设置特定的跑步者,而不必对所有跑步者都这样做。例如,如果您想部署某个项目,您可以设置一个特定的运行器以获得正确的凭据。

Now to select right executor for project, its very important that we have bird view on all the available executors for gitlab runner. Gitlab has made this job easy for us by providing nice documentation over hereexplaining what are the different options you will get with different executors.

现在要为项目选择正确的执行程序,非常重要的是我们可以鸟瞰 gitlab runner 的所有可用执行程序。Gitlab 在这里提供了很好的文档,解释了使用不同的执行程序可以获得哪些不同的选项,从而使我们的工作变得轻松。

If you would like to know more about runners and different executors, I would suggest you start with this article, Gitlab Runner

如果你想了解更多关于跑步者和不同执行者的信息,我建议你从这篇文章开始, Gitlab Runner

回答by UltimaWeapon

I spent a fair amount of time trying to set up our Java projects on Gitlab CI. I got it working with some degree of success. As mentioned by rolve, the most straight forward solution is to use an image from the official repo: https://hub.docker.com/_/maven

我花了很多时间尝试在 Gitlab CI 上设置我们的 Java 项目。我让它在某种程度上取得了成功。正如 rolve 所提到的,最直接的解决方案是使用官方 repo 中的图像:https://hub.docker.com/_/maven

However, we have a corporate proxy which was causing my builds to get timeout requests when fetching the project's dependencies. I tried many solutions and finally came across this post: https://gitlab.com/gitlab-org/gitlab-ce/issues/15167.

但是,我们有一个公司代理,它导致我的构建在获取项目的依赖项时收到超时请求。我尝试了很多解决方案,最后看到了这篇文章:https: //gitlab.com/gitlab-org/gitlab-ce/issues/15167

The post itself is about setting up maven to cache downloaded dependencies in a local repo which can be accessed among builds. The idea is that you can write a local maven configuration file by in .gitlab-ci.ymlto set up your cache directory and your proxy.

该帖子本身是关于设置 maven 以在本地存储库中缓存下载的依赖项,该存储库可在构建之间访问。这个想法是你可以在.gitlab-ci.yml 中编写一个本地 maven 配置文件来设置你的缓存目录和你的代理。

before_script:
  -echo '<settings
          xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
          https://maven.apache.org/xsd/settings-1.0.0.xsd">
          <localRepository>/cache/.m2</localRepository>
          <proxies>
              <proxy>
                  <active>true</active>
                  <protocol>'$PROXY_PROTOCOL'</protocol>
                  <host>'$PROXY_HOST'</host>
                  <port>'$PROXY_PORT'</port>
              </proxy>
          </proxies>
      </settings>' > $HOME/.m2/settings.xml

build_debug1:
  stage: build
  script: "echo $PROXY_HOST"

build_debug2:
  stage: build
  script: "cat $HOME/.m2/settings.xml"

build_maven:
  stage: build
  script: "mvn $MAVEN_CLI_OPTS package"
  artifacts:
    paths:
      - target/*.jar

deploy_debug1:
  stage: package
  script: "ls target/"

Notice the build debug jobs are only to see whether the proxy settings were being correctly injected. You can set the proxy environment variables as secrets using Gitlab by going to Project -> Settings -> CI/CD Pipelines -> Secret Variables.

请注意,构建调试作业仅用于查看代理设置是否被正确注入。您可以通过转到 Project -> Settings -> CI/CD Pipelines -> Secret Variables 使用 Gitlab 将代理环境变量设置为机密。

The last deploy_debugjob is to see what was generated in your target directory.

最后一项deploy_debug工作是查看目标目录中生成的内容。

回答by Mohammad Awwaad

With the help of How to deploy Maven projects to Artifactory with GitLab CI/CD

借助如何使用 GitLab CI/CD 将 Maven 项目部署到 Artifactory

You can compile your java maven project by just adding a .gitlab-ci.ymlfile to your repository's root directory with the following content:

你可以编译你的 java maven 项目,只需将一个.gitlab-ci.yml文件添加到你的存储库的根目录中,内容如下:

image: maven:latest

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
  paths:
    - .m2/repository/
    - target/

build:
  stage: build
  script:
    - mvn compile