# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-## Copyright 2015-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/>."""The maven plugin."""importrefromtypingimportLiteral,castfromoverridesimportoverridefromcraft_partsimporterrorsfromcraft_parts.utils.mavenimportcreate_maven_settingsfrom.importvalidatorfrom.java_pluginimportJavaPluginfrom.propertiesimportPluginProperties
[docs]classMavenPluginProperties(PluginProperties,frozen=True):"""The part properties used by the maven plugin."""plugin:Literal["maven"]="maven"maven_parameters:list[str]=[]maven_use_wrapper:bool=False# part properties required by the pluginsource:str# pyright: ignore[reportGeneralTypeIssues]
[docs]classMavenPluginEnvironmentValidator(validator.PluginEnvironmentValidator):"""Check the execution environment for the maven plugin. :param part_name: The part whose build environment is being validated. :param env: A string containing the build step environment setup. """
[docs]@overridedefvalidate_environment(self,*,part_dependencies:list[str]|None=None)->None:"""Ensure the environment contains dependencies needed by the plugin. :param part_dependencies: A list of the parts this part depends on. :raises PluginEnvironmentValidationError: If maven is invalid and there are no parts named maven-deps. """options=cast(MavenPluginProperties,self._options)ifoptions.maven_use_wrapper:returnversion=self.validate_dependency(dependency="mvn",plugin_name="maven",part_dependencies=part_dependencies,)ifnotre.match(r"(\x1b\[1m)?Apache Maven ",version)and(part_dependenciesisNoneor"maven-deps"notinpart_dependencies):raiseerrors.PluginEnvironmentValidationError(part_name=self._part_name,reason=f"invalid maven version {version!r}",)
[docs]classMavenPlugin(JavaPlugin):"""A plugin to build parts that use Maven. The Maven build system is commonly used to build Java projects. This plugin requires a pom.xml in the root of the source tree. This plugin uses the common plugin keywords as well as those for "sources". For more information check the 'plugins' topic for the former and the 'sources' topic for the latter. Additionally, this plugin uses the following plugin-specific keywords: - maven-parameters: (list of strings) Flags to pass to the build using the maven semantics for parameters. - maven-use-wrapper: (boolean) Whether to use project's Maven wrapper (mvnw) to execute the build. """properties_class=MavenPluginPropertiesvalidator_class=MavenPluginEnvironmentValidator
[docs]@overridedefget_build_snaps(self)->set[str]:"""Return a set of required snaps to install in the build environment."""returnset()
[docs]@overridedefget_build_packages(self)->set[str]:"""Return a set of required packages to install in the build environment."""returnset()
@propertydef_maven_executable(self)->str:"""Return the maven executable to be used for build."""options=cast(MavenPluginProperties,self._options)return"${CRAFT_PART_BUILD_WORK}/mvnw"ifoptions.maven_use_wrapperelse"mvn"
[docs]@overridedefget_build_commands(self)->list[str]:"""Return a list of commands to run during the build step."""options=cast(MavenPluginProperties,self._options)mvn_cmd=[self._maven_executable,"package"]ifsettings_path:=create_maven_settings(part_info=self._part_info):mvn_cmd.extend(["-s",str(settings_path)])return[*self._get_mvnw_validation_commands(options=options)," ".join(mvn_cmd+options.maven_parameters),*self._get_java_post_build_commands(),]
def_get_mvnw_validation_commands(self,options:MavenPluginProperties)->list[str]:"""Validate mvnw file before execution."""ifnotoptions.maven_use_wrapper:return[]return["""[ -e ${CRAFT_PART_BUILD_WORK}/mvnw ] || {>&2 echo 'mvnw file not found, refer to plugin documentation: \https://canonical-craft-parts.readthedocs-hosted.com/en/latest/\common/craft-parts/reference/plugins/maven_plugin.html'; exit 1;}"""]