# Copyright 2025 Canonical Ltd.## This program is free software; you can redistribute it and/or# modify it under the terms of the GNU Lesser General Public# License version 3 as published by the Free Software Foundation.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>."""Utilities for Maven projects and settings."""importosfrompathlibimportPathfromurllib.parseimporturlparsefromcraft_partsimportinfosfrom._xmlimport(PROXIES_TEMPLATE,PROXY_CREDENTIALS_TEMPLATE,PROXY_TEMPLATE,SETTINGS_TEMPLATE,)
[docs]defcreate_maven_settings(*,part_info:infos.PartInfo)->Path|None:"""Create a Maven configuration file. The settings file contains additional configuration for Maven, such as proxy parameters. If it detects that no configuration is necessary, it will return None and do nothing. :param part_info: The part info for the part invoking Maven. :return: Returns a Path object to the settings file if one is created, otherwise None. """ifnot_needs_proxy_config():returnNonesettings_path=part_info.part_build_subdir/".parts/.m2/settings.xml"settings_path.parent.mkdir(parents=True,exist_ok=True)proxies_element=_get_proxy_config()settings_xml=SETTINGS_TEMPLATE.format(proxies_element=proxies_element)settings_path.write_text(settings_xml)returnsettings_path
def_get_proxy_config()->str:"""Generate an XML string for proxy configurations. Reads the environment for information on desired proxy settings and transforms those variables into Maven XML settings entries. """# Transform all environment variables to their lowercase form to support HTTPS_PROXY# vs. https_proxy and suchcase_insensitive_env={item[0].lower():item[1]foriteminos.environ.items()}proxies:list[str]=[]forprotocolin["http","https"]:env_name=f"{protocol}_proxy"ifenv_namenotincase_insensitive_env:continueproxy_url=urlparse(case_insensitive_env[env_name])ifproxy_url.usernameisnotNoneandproxy_url.passwordisnotNone:credentials=PROXY_CREDENTIALS_TEMPLATE.format(username=proxy_url.username,password=proxy_url.password)else:credentials=""proxy_element=PROXY_TEMPLATE.format(id=env_name,protocol=protocol,host=proxy_url.hostname,port=proxy_url.port,credentials=credentials,non_proxy_hosts=_get_no_proxy_string(),)proxies.append(proxy_element)returnPROXIES_TEMPLATE.format(proxies="\n".join(proxies))def_needs_proxy_config()->bool:"""Determine whether or not proxy configuration is necessary for Maven."""proxy_vars=["http_proxy","https_proxy","HTTP_PROXY","HTTPS_PROXY"]returnany(keyinos.environforkeyinproxy_vars)def_get_no_proxy_string()->str:no_proxy=[k.strip()forkinos.environ.get("no_proxy","localhost").split(",")]return"|".join(no_proxy)