Using the SiteTool in a Mojo

SiteTool is a JSR-330 component. Inject it into your Mojo's constructor, then use it to read the site descriptor of the current project with inheritance and interpolation applied, the same way the Maven Site Plugin does:

import javax.inject.Inject;

import java.io.File;
import java.util.List;
import java.util.Locale;

import org.apache.maven.doxia.site.SiteModel;
import org.apache.maven.doxia.tools.SiteTool;
import org.apache.maven.doxia.tools.SiteToolException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

@Mojo(name = "your-own")
public class YourOwnMojo extends AbstractMojo {

    @Parameter(defaultValue = "${session}", readonly = true, required = true)
    private MavenSession session;

    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject project;

    @Parameter(defaultValue = "${project.basedir}/src/site", required = true)
    private File siteDirectory;

    @Parameter(property = "locales")
    private String locales;

    private final SiteTool siteTool;

    @Inject
    public YourOwnMojo(SiteTool siteTool) {
        this.siteTool = siteTool;
    }

    @Override
    public void execute() throws MojoExecutionException {
        try {
            for (Locale locale : siteTool.getSiteLocales(locales)) {
                SiteModel siteModel = siteTool.getSiteModel(
                        siteDirectory,
                        locale,
                        session.getRequest(),
                        project,
                        session.getProjects(),
                        session.getRepositorySession(),
                        project.getRemoteProjectRepositories());
                getLog().info("Site " + siteModel.getName() + " for locale " + locale);
            }
        } catch (SiteToolException e) {
            throw new MojoExecutionException("Cannot read the site descriptor", e);
        }
    }
}

getSiteLocales(null) returns only SiteTool.DEFAULT_LOCALE, the default site locale: Locale.ROOT unless the maven.doxiaSitetools.defaultLocale system property names another one. getRelativePath(to, from) computes a relative link between two locations and treats from as a directory.

References