3

I'm developing a maven plugin, and I need to obtain the root directory of multi-module project.

With mavenProject.getBuild().getDirectory() I can get the path to directory of current module, alright.

So thought I'd try to get root MavenProject, but with getRootProject() defined as below, getRootProject().getBuild().getDirectory() returns "${project.basedir}" (literally that String value... which I don't know how to resolve, and even if I did resolve it it would resolve to current module, which is not what I want).

@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject mavenProject;
// ...
private MavenProject getRootProject() {
    MavenProject project = mavenProject;
    while (project.getParent() != null) {
        project = project.getParent();
    }
    return project;
}

How do I get (resolved & absolute) path to root module?

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
  • Found a solution by myself (shared below), but still curious about behavior with `${project.basedir}` described above... and would also appreciate if other solutions were offered :) – Hugues M. Jun 21 '17 at 15:37
  • I would ask why do you need he root level of a multi module build within a plugin? – khmarbaise Jun 21 '17 at 16:17
  • It's a reporting plugin. For reasons that pertain to my organization, I need to collect details on each module (and their dependencies) and write that info under the "target" directory for the root module of the whole tree. The info I need is easily obtained by maven and difficult to obtain outside of maven, so a maven plugin seemed like a good idea. – Hugues M. Jun 21 '17 at 17:44
  • you know that already such plugin for site generation and reporting already exist which also produce reports about the depdencies? – khmarbaise Jun 22 '17 at 09:03
  • Yes but it does not give me the same control to do what I want (not that kind of reporting). I strongly prefer a simple & straightforward custom maven plugin to chaining an existent plugin with custom script(s) to extract the bits I want from a ton of difficult-to-parse details (& format that might change). If that practice is discouraged, perhaps I should consider Gradle instead, but for now that works well for me. – Hugues M. Jun 22 '17 at 09:21
  • Can you give more details about what you like to get reported ? So maybe the Maven community can create such things if it might be intersting for others in the community? – khmarbaise Jun 23 '17 at 07:13

1 Answers1

3

Thanks to this answer which deals with the same issue from a different perspective (not from a Mojo), I found a solution:

Add the following field to my Mojo:

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

Then use it this way:

mavenSession.getExecutionRootDirectory()
Hugues M.
  • 19,846
  • 6
  • 37
  • 65