多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# How to deploy Maven projects to Artifactory with GitLab CI/CD > 原文:[https://docs.gitlab.com/ee/ci/examples/artifactory_and_gitlab/](https://docs.gitlab.com/ee/ci/examples/artifactory_and_gitlab/) * [Introduction](#introduction) * [Create the simple Maven dependency](#create-the-simple-maven-dependency) * [Prepare the dependency application](#prepare-the-dependency-application) * [Configure the Artifactory deployment](#configure-the-artifactory-deployment) * [Configure GitLab CI/CD for `simple-maven-dep`](#configure-gitlab-cicd-for-simple-maven-dep) * [Create the main Maven application](#create-the-main-maven-application) * [Prepare the main application](#prepare-the-main-application) * [Configure the Artifactory repository location](#configure-the-artifactory-repository-location) * [Configure GitLab CI/CD for `simple-maven-app`](#configure-gitlab-cicd-for-simple-maven-app) * [Conclusion](#conclusion) # How to deploy Maven projects to Artifactory with GitLab CI/CD[](#how-to-deploy-maven-projects-to-artifactory-with-gitlab-cicd "Permalink") ## Introduction[](#introduction "Permalink") 在本文中,我们展示了如何利用[GitLab CI / CD 的功能](https://about.gitlab.com/stages-devops-lifecycle/continuous-integration/)来构建[Maven](https://maven.apache.org/)项目,将其部署到[Artifactory](https://jfrog.com/artifactory/) ,然后从另一个 Maven 应用程序中将其用作依赖项. 您将创建两个不同的项目: * `simple-maven-dep` :构建并部署到 Artifactory 的应用程序(请参阅[simple-maven-dep](https://gitlab.com/gitlab-examples/maven/simple-maven-dep)示例项目) * `simple-maven-app` :使用前一个作为依赖[项的应用程序](https://gitlab.com/gitlab-examples/maven/simple-maven-app) (请参见[simple-maven-app](https://gitlab.com/gitlab-examples/maven/simple-maven-app)示例项目) 我们假设您已经在[GitLab.com](https://gitlab.com/)上拥有一个 GitLab 帐户,并且您知道 Git 和[GitLab CI / CD](https://about.gitlab.com/stages-devops-lifecycle/continuous-integration/)的基本用法. 我们还假定 Artifactory 实例在 Internet 上可用且可访问,并且您具有在其上部署的有效凭据. ## Create the simple Maven dependency[](#create-the-simple-maven-dependency "Permalink") First of all, you need an application to work with: in this specific case we will use a simple one, but it could be any Maven application. This will be the dependency you want to package and deploy to Artifactory, in order to be available to other projects. ### Prepare the dependency application[](#prepare-the-dependency-application "Permalink") 对于本文,您将使用可以从示例项目中克隆的 Maven 应用程序: 1. 登录到您的 GitLab 帐户 2. 通过选择**URL 通过 URL 回购导入项目来**创建新项目 3. 添加以下 URL: ``` https://gitlab.com/gitlab-examples/maven/simple-maven-dep.git ``` 4. Click **建立专案** 该应用程序不过是带有基于 JUnit 的测试套件的存根的基本类. 它公开了一种名为`hello`的方法,该方法接受字符串作为输入,并在屏幕上显示 hello 消息. 该项目的结构非常简单,您应该考虑以下两个资源: * `pom.xml` :项目对象模型(POM)配置文件 * `src/main/java/com/example/dep/Dep.java` :我们应用程序的源代码 ### Configure the Artifactory deployment[](#configure-the-artifactory-deployment "Permalink") 该应用程序已准备就绪,可以使用,但是您需要一些其他步骤才能将其部署到 Artifactory: 1. 使用您的用户凭据登录到 Artifactory. 2. 在主屏幕上,单击" **设置我"**面板中的`libs-release-local`项目. 3. 将" **部署"**段落下的配置片段复制到剪贴板. 4. 更改`url`值,以使其可通过变量进行配置. 5. 在`dependencies`部分之后,将片段复制到项目的`pom.xml`文件中. 该代码段应如下所示: ``` <distributionManagement> <repository> <id>central</id> <name>83d43b5afeb5-releases</name> <url>${env.MAVEN_REPO_URL}/libs-release-local</url> </repository> </distributionManagement> ``` 将依赖项部署到 Artifactory 之前,您需要做的另一步骤是配置身份验证数据. 这是一个简单的任务,但是 Maven 要求它保留在一个名为`settings.xml`的文件中,该文件必须位于用户 homedir 的`.m2`子目录中. 由于要使用 GitLab Runner 自动部署应用程序,因此应在项目的主目录中创建文件,并在`.gitlab-ci.yml`设置命令行参数以使用自定义位置,而不是默认位置: 1. 在存储库的根目录中创建一个名为`.m2`的文件夹 2. 在`.m2`文件夹中创建一个名为`settings.xml`的文件 3. 将以下内容复制到`settings.xml`文件中: ``` <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <servers> <server> <id>central</id> <username>${env.MAVEN_REPO_USER}</username> <password>${env.MAVEN_REPO_PASS}</password> </server> </servers> </settings> ``` 用户名和密码将使用变量替换为正确的值. ### Configure GitLab CI/CD for `simple-maven-dep`[](#configure-gitlab-cicd-for-simple-maven-dep "Permalink") 现在是时候设置[GitLab CI / CD](https://about.gitlab.com/stages-devops-lifecycle/continuous-integration/)来自动构建,测试和部署依赖项了! GitLab CI / CD 使用存储库根目录中的一个名为`.gitlab-ci.yml`来读取将由已配置的 GitLab Runners 执行的作业的定义. 您可以在[GitLab 文档中](../../yaml/README.html)阅读有关此文件的更多信息. 首先,请记住为您的部署设置变量. 导航到项目的**"设置">" CI / CD">"环境变量"**页面,并添加以下内容(当然,将它们替换为当前值): * **MAVEN_REPO_URL** : `http://artifactory.example.com:8081/artifactory` : **//artifactory.example.com** : **8081/artifactory** (您的 Artifactory URL) * **MAVEN_REPO_USER** : `gitlab` (您的 Artifactory 用户名) * **MAVEN_REPO_PASS** : `AKCp2WXr3G61Xjz1PLmYa3arm3yfBozPxSta4taP3SeNu2HPXYa7FhNYosnndFNNgoEds8BCS` (您的 Artifactory 加密密码) 现在是时候在`.gitlab-ci.yml`定义作业并将其推送到存储库了: ``` image: maven:latest variables: MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode" MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" cache: paths: - .m2/repository/ - target/ build: stage: build script: - mvn $MAVEN_CLI_OPTS compile test: stage: test script: - mvn $MAVEN_CLI_OPTS test deploy: stage: deploy script: - mvn $MAVEN_CLI_OPTS deploy only: - master ``` GitLab Runner 将使用最新的[Maven Docker 映像](https://hub.docker.com/_/maven/) ,该[映像](https://hub.docker.com/_/maven/)已经包含了管理项目以运行作业所需的所有工具和依赖项. 设置环境变量以指示 Maven 在搜索配置和依赖项时使用存储库的`homedir`而不是用户的`homedir` . 缓存`.m2/repository folder` ( `.m2/repository folder`所有 Maven 文件的位置)和`target`文件夹(将创建我们的应用程序的位置),对于按顺序运行所有 Maven 阶段来加快过程非常有用,因此,执行`mvn test`会在必要时自动运行`mvn compile` . `build`作业和`test`作业均利用`mvn`命令来编译应用程序,并按照应用程序一部分的测试套件中的定义对其进行测试. 部署到 Artifactory 的过程已由我们刚刚设置的变量定义. 仅当我们推送或合并到`master`分支时才进行部署,以便开发版本经过测试但未发布. 做完了! 现在,您已在 GitLab 存储库中进行了所有更改,并且已经为此提交启动了管道. 在" **管道"**选项卡中,您可以查看正在发生的事情. 如果部署成功,则部署作业日志将输出: ``` [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.983 s ``` > **注意** : `mvn`命令从 Internet 下载大量文件,因此,首次运行日志时,您会在日志中看到很多额外的活动. 好极了! 你做到了! 检入 Artifactory 将确认`libs-release-local`存储库中有可用的新工件. ## Create the main Maven application[](#create-the-main-maven-application "Permalink") 现在您已经有了 Artifactory 上的依赖项,是时候使用它了! 让我们看看如何将其作为对主应用程序的依赖. ### Prepare the main application[](#prepare-the-main-application "Permalink") 我们将再次使用可以从示例项目中克隆的 Maven 应用程序: 1. 通过选择**URL 通过 URL 回购导入项目来**创建新项目 2. 添加以下 URL: ``` https://gitlab.com/gitlab-examples/maven/simple-maven-app.git ``` 3. Click **建立专案** This one is a simple app as well. If you look at the `src/main/java/com/example/app/App.java` file you can see that it imports the `com.example.dep.Dep` class and calls the `hello` method passing `GitLab` as a parameter. 由于 Maven 不知道如何解决依赖关系,因此您需要修改配置: 1. 回到 Artifactory 2. 浏览`libs-release-local`存储库 3. 选择`simple-maven-dep-1.0.jar`文件 4. 从主面板的**Dependency Declaration**部分中找到配置片段 5. 将代码段复制到`pom.xml`文件的`dependencies`部分中. 该代码段应如下所示: ``` <dependency> <groupId>com.example.dep</groupId> <artifactId>simple-maven-dep</artifactId> <version>1.0</version> </dependency> ``` ### Configure the Artifactory repository location[](#configure-the-artifactory-repository-location "Permalink") 至此,您已经定义了应用程序的依赖关系,但是仍然错过了可以找到所需文件的位置. 您需要像创建依赖项项目一样创建一个`.m2/settings.xml`文件,并使用环境变量让 Maven 知道位置. 这是直接从 Artifactory 获取文件内容的方法: 1. 在主屏幕上,单击" **设置我"**面板中的`libs-release-local`项目 2. 单击**生成 Maven 设置** 3. 点击**生成设置** 4. 将配置文件复制到剪贴板 5. 在存储库中将文件另存为`.m2/settings.xml` 现在您可以使用 Artifactory 存储库来解决依赖关系,并在主应用程序中使用`simple-maven-dep` ! ### Configure GitLab CI/CD for `simple-maven-app`[](#configure-gitlab-cicd-for-simple-maven-app "Permalink") 您需要完成所有步骤:为该项目配置`.gitlab-ci.yml`文件,就像对`simple-maven-dep`所做的那样. 您想利用[GitLab CI / CD](https://about.gitlab.com/stages-devops-lifecycle/continuous-integration/)自动构建,测试和运行您的超赞应用程序,并查看是否可以按预期方式获得欢迎! 您需要做的就是将以下`.gitlab-ci.yml`添加到存储库中: ``` image: maven:latest stages: - build - test - run variables: MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode" MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" cache: paths: - .m2/repository/ - target/ build: stage: build script: - mvn $MAVEN_CLI_OPTS compile test: stage: test script: - mvn $MAVEN_CLI_OPTS test run: stage: run script: - mvn $MAVEN_CLI_OPTS package - mvn $MAVEN_CLI_OPTS exec:java -Dexec.mainClass="com.example.app.App" ``` 它与用于`simple-maven-dep`的配置非常相似,但是有一个`run`作业代替了`deploy`作业. 可能是您不想在实际项目中使用的东西,但是在这里查看自动执行的应用程序很有用. 就是这样! 在`run`作业输出日志中,您会找到与 GitLab 的友好问好! ## Conclusion[](#conclusion "Permalink") 在本文中,我们介绍了使用 Artifactory Maven 存储库自动发布和使用工件的基本步骤. 可以使用类似的方法与任何其他 Maven 兼容的 Binary Repository Manager 进行交互. 显然,您可以改进这些示例,优化`.gitlab-ci.yml`文件以更好地满足您的需求,并适应您的工作流程.