pypa-hatch/docs/plugins/build-hook.md

2.5 KiB

Build hook plugins


A build hook provides code that will be executed at various stages of the build process. See the documentation for build hook configuration.

Known third-party

Overview

Build hooks run for every selected version of build targets.

The initialization stage occurs immediately before each build and the finalization stage occurs immediately after. Each stage has the opportunity to view or modify build data.

Build data

Build data is a simple mapping whose contents can influence the behavior of builds. Which fields are recognized depends on each build target.

The following fields are always present and recognized by the build system itself:

Field Type Description
artifacts #!python list[str] This is a list of extra paths to artifacts and should generally only be appended to

Built-in

Custom

This is a custom class in a given Python file that inherits from the BuildHookInterface.

Configuration

The build hook plugin name is custom.

=== ":octicons-file-code-16: pyproject.toml"

```toml
[tool.hatch.build.hooks.custom]
[tool.hatch.build.targets.<TARGET_NAME>.hooks.custom]
```

=== ":octicons-file-code-16: hatch.toml"

```toml
[build.hooks.custom]
[build.targets.<TARGET_NAME>.hooks.custom]
```

An option path is used to specify the path of the Python file, defaulting to build.py.

Example

=== ":octicons-file-code-16: build.py"

```python
from hatchling.builders.hooks.plugin.interface import BuildHookInterface


class CustomBuildHook(BuildHookInterface):
    ...
```

If multiple subclasses are found, you must define a function named get_build_hook that returns the desired build hook.

!!! note Any defined PLUGIN_NAME is ignored and will always be custom.

::: hatchling.builders.hooks.plugin.interface.BuildHookInterface selection: members: - PLUGIN_NAME - app - root - config - target_name - directory - clean - initialize - finalize