Running "mvn clean install" maven command using ansible module

Viewed 6752

I am trying to implement the CI/CD pipeline for my project. I am using Ansible , Docker and jenkins. SVN checkout , Image docker image building , image pushing to Dockerhub , Pulling and deploying etc every stages are planning to do using ansible roles.Now I successfully implemented sample svncheckout , image building and pushing and docker image deploying using ansible modules.

I am using Maven build tool.So here I have confusion that , after checkouting from svn repository , I need to run "mvn clean install" using ansible. Noow I am trying to find a ansible module. But i did ot got ansible module . For doing this is there any ansible module for maven like docker_image and svn ? How I can run maven commands using ansible role ?

3 Answers

If you couldn't find a module for your specific task, you have two options:

  1. Write your own in Python. Put them into 'library/' directory of the role or playbook.
  2. Use command or shell module to execute desired behavior.

Some systems are way too complex to be quickly implemented as ansible modules, nevertheless, it's often very easy to use their CLI.

If you cannot find any ansible plugin for maven. You can use the shell module to do that. Here is an example:

  - name: Running mvn clean
    shell: "mvn clean install"
    register: mvn_result

  - name: "mvn clean task output"
    debug:
     var: mvn_result

I have similar case where I have to download source code (from git) and install the compilation results (Java jar) into local Maven repository.

AFAIK there is no Ansible module for Maven but writing the functionality with command-module is in fact a quite simple. I use the following tasks where Maven is run only when the local clone of the repo has been changed.

- 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 }}"
Related