-
  name: "Installing Maven to ~/.sdkman/candidates/maven/current/bin/mvn for {{ service_user }}"
  ansible.builtin.shell: "{{ item }}"
  become: true
  become_user: "{{ service_user }}"
  register: mvnmanresult
  until: "mvnmanresult is not failed"
  retries: 3
  args:
    executable: "/bin/bash"
    creates: "{{ service_home_dir }}/.sdkman/candidates/maven/current/bin/mvn"
  with_items:
    - "source {{ service_home_dir }}/.sdkman/bin/sdkman-init.sh && sdk install maven {{ maven_version }} "

-
  name: "Adding Maven to /etc/profile.d/maven.sh for {{ service_user }}"
  ansible.builtin.lineinfile:
    mode: '0755'
    path: "{{ item.dir }}"
    regexp: "^PATH=\"$PATH:{{ service_home_dir }}/.sdkman/candidates/maven/current/bin\""
    insertbefore: EOF
    create: true
    line: 'PATH=$PATH:{{ service_home_dir }}/.sdkman/candidates/maven/current/bin'
  with_items:
    - { user: "{{ service_user }}", dir: "/etc/profile.d/maven.sh" }

-
  name: "Updating alternatives for Maven"
  community.general.alternatives:
    name: "{{ item.name }}"
    path: "{{ sdkman_dir }}/candidates/{{ item.candidate }}/current/bin/{{ item.name }}"
    link: "{{ item.link }}"
  loop: "{{ sdkman_update_alternatives }}"
  become: true
  when: ansible_os_family != 'Darwin'
  tags:
    - sdkman_privilege
