delegate_to: localhost and gather_facts: yes playbook still holds previous hosts facts.

Ansible fixed the bug as misunderstood and alsogather_factsa play directive, it does not work on tasks.

then whats the way to get localhost facts?

  1. create role and role YML file with remote hosts defined as

  2. create tasks file which actually needs to gather fact of localhost

  3. run the role yml file

role file :- 
-----------------------------
---
- name: My role name
  hosts: windows
  gather_facts: yes
  vars_files:
    - ./roles/myrole/vars/main.yml

  roles:
    - { role: myrole,                tags: ["myrole"] }
------------------------------------------------------------

Tasks file :- 
------------------------------------------------------------
---
#---------------------------------------------------------------------------
  - name: Windows OS User home directory
    delegate_to: localhost
    gather_facts: true
    set_fact:
       userhomedir: '{{ ansible_user_dir }}\.m2'
    register: userhomedirwin
    when: ansible_os_family == "Windows"
    tags:
        - winuserhome
  - name: DEBUG Windows OS User home directory
    debug:
      var: userhomedirwin
      verbosity: 3
    when: ansible_os_family == "Windows"
  - name: Linux OS User home directory
    delegate_to: localhost
    gather_facts: true
    set_fact:
       userhomedir: "{{ ansible_user_dir }}/.m2"
    when: ansible_os_family != "Windows"
    register: userhomedirlin
    tags:
          - linuserhome
  - name: DEBUG Linux OS User home directory
    debug:
      var: userhomedirlin
      verbosity: 3
    when: ansible_os_family != "Windows"
  - name: Creating Maven settting config file from template
    local_action: template src={{ srcSettingsXml }} dest={{ destSettingsXml }}
    register: settingxml
    tags:
        - settingxml
  - name: DEBUG Creating Maven Setting xml config file from template
    debug:
      var: settingxml
      verbosity: 3
#---------------------------------------------------------------------------

`

TASK [cobrowse : Windows OS User home directory] ************************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:57
ok: [192.168.100.222 -> localhost] => {
    "ansible_facts": {
        "userhomedir": "C:\Users\Administrator\.m2"
    },
    "changed": false,
    "failed": false
}
Read vars_file './roles/myrole/vars/main.yml'

TASK [cobrowse : DEBUG Windows OS User home directory] ******************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:66
ok: [192.168.100.222 ] => {
    "userhomedirwin": {
        "ansible_facts": {
            "userhomedir": "C:\Users\Administrator\.m2"
        },
        "ansible_facts_cacheable": false,
        "changed": false,
        "failed": false
    }
}
Read vars_file './roles/myrole/vars/main.yml'
Read vars_file './roles/myrole/vars/main.yml'

TASK [cobrowse : Linux OS User home directory] **************************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:71
skipping: [192.168.100.222 ] => {
    "changed": false,
    "skip_reason": "Conditional result was False",
    "skipped": true
}
Read vars_file './roles/myrole/vars/main.yml'

TASK [cobrowse : DEBUG Linux OS User home directory] ********************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:80
skipping: [192.168.100.222 ] => {
    "skip_reason": "Conditional result was False"
}
Read vars_file './roles/myrole/vars/main.yml'
Read vars_file './roles/myrole/vars/main.yml'

`

Ansible fixed the bug as misunderstood and alsogather_factsa play
directive, it does not work on tasks.

then whats the way to get localhost facts?

   1. create role and role YML file with remote hosts defined as
   2. create tasks file which actually needs to gather fact of localhost

It looks like you av mixing gather facts versus setting facts.

Gather facts is about getting a lot of information about a host, what actually happening is it runs the setup module on the host.
Setting facts is setting one or more facts/variables on a host with set_facts module.

   3. run the role yml file

role file :-
-----------------------------
---
- name: My role name
  hosts: windows
  gather_facts: yes

This is correct use of gather_facts and only place it's allowed to can be used, on the play.

  vars_files:
    - ./roles/myrole/vars/main.yml

  roles:
    - { role: myrole, tags: ["myrole"] }
------------------------------------------------------------

Tasks file :-
------------------------------------------------------------
---
#---------------------------------------------------------------------------
  - name: Windows OS User home directory
    delegate_to: localhost
    gather_facts: true
    set_fact:
       userhomedir: '{{ ansible_user_dir }}\.m2'
    register: userhomedirwin
    when: ansible_os_family == "Windows"
    tags:
        - winuserhome

As already mention gather_facts can only be used on plays, not on tasks.

  - name: DEBUG Windows OS User home directory
    debug:
      var: userhomedirwin
      verbosity: 3
    when: ansible_os_family == "Windows"
  - name: Linux OS User home directory
    delegate_to: localhost
    gather_facts: true
    set_fact:
       userhomedir: "{{ ansible_user_dir }}/.m2"
    when: ansible_os_family != "Windows"
    register: userhomedirlin
    tags:
          - linuserhome
  - name: DEBUG Linux OS User home directory
    debug:
      var: userhomedirlin
      verbosity: 3
    when: ansible_os_family != "Windows"
  - name: Creating Maven settting config file from template
    local_action: template src={{ srcSettingsXml }} dest={{ destSettingsXml }}
    register: settingxml
    tags:
        - settingxml
  - name: DEBUG Creating Maven Setting xml config file from template
    debug:
      var: settingxml
      verbosity: 3

That said, I'm not sure what it is you are trying to achieve.