【问题标题】:Running "mvn clean install" maven command using ansible module使用 ansible 模块运行“mvn clean install”maven 命令
【发布时间】:2018-11-07 13:36:29
【问题描述】:

我正在尝试为我的项目实施 CI/CD 管道。我正在使用 Ansible、Docker 和 jenkins。 SVN checkout , Image docker image building , image push to Dockerhub , Pulling and deploying 等每个阶段都计划使用 ansible 角色。现在我成功地实现了示例 svncheckout , image building and pushing and docker image deploying using ansible modules.

我正在使用 Maven 构建工具。所以在这里我很困惑,从 svn 存储库签出后,我需要使用 ansible 运行“mvn clean install”。现在我正在尝试找到一个 ansible 模块。但我没有得到 ansible 模块。为此,是否有任何适用于 docker_image 和 svn 的 maven 的 ansible 模块?如何使用 ansible 角色运行 maven 命令?

【问题讨论】:

标签: maven docker ansible


【解决方案1】:

如果您找不到特定任务的模块,您有两种选择:

  1. 用 Python 编写您自己的代码。将它们放入角色或剧本的“library/”目录中。
  2. 使用commandshell 模块执行所需的行为。

有些系统过于复杂,无法快速实现为 ansible 模块,不过,使用它们的 CLI 通常非常容易。

【讨论】:

    【解决方案2】:

    如果你找不到任何适用于 maven 的 ansible 插件。您可以使用 shell 模块来执行此操作。 这是一个例子:

      - name: Running mvn clean
        shell: "mvn clean install"
        register: mvn_result
    
      - name: "mvn clean task output"
        debug:
         var: mvn_result
    

    【讨论】:

      【解决方案3】:

      我有类似的情况,我必须下载源代码(从 gi​​t)并将编译结果(Java jar)安装到本地 Maven 存储库中。

      AFAIK 没有适用于 Maven 的 Ansible 模块,但使用 command-module 编写功能实际上非常简单。我使用以下任务,仅当 repo 的本地克隆已更改时才会运行 Maven。

      - name: "source code : download"
        register: source_code_clones
        git:
          repo: ssh://git@somewhere.invalid/projects/{{ item }}.git
          dest: "{{ repodir }}/{{ item }}"
          version: master
          depth: 1
        with_items:
        - project1
        - project2
        - project3
      
      # - debug:
      #     msg: "{{ item }}"
      #   with_items: "{{ source_code_clones.results }}"
      
      - name: "source code : local install"
        when: item.changed
        command: mvn --batch-mode --quiet install
        args:
           chdir: "{{ repodir }}/{{ item.item }}"
        with_items: "{{ source_code_clones.results }}"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-23
        • 2014-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多