<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 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`文件以更好地滿足您的需求,并適應您的工作流程.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看