How to process project sources in maven plugin

java, maven, maven-plugin

Solution

You know the Mojo API which contains the information about the current project for example. This can be simply injected by the plexus contains automatically by using the appropriate markers like the following:

public class WhatEverMojo extends AbstractMojo {

    /**
     * The Maven project.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;
}

May be i misunderstand your question but i recommend to read the Introduction to Plugin development.

Update: It might help to take a look into other plugins like the apt-maven-plugin:

A thing like the following could help:

/**
 * The source directories containing the sources to be processed.
 * 
 * @parameter expression="${project.compileSourceRoots}"
 * @required
 * @readonly
 */
private List<String> compileSourceRoots;

The complete source code is available via SVN where you can take a deep look into.

Problem

I'm writing a maven plugin that basically should do the following: - process all classes of the project built - create a file describing parts of the source code - add that file to the jar built (either as addition to the `MANIFEST` or as a new file in the `META-INF` directory) As I'm just making my first steps in creating maven plugins here is my (possibly dumb) question: How can I access the source code of a project from a plugin that is executed when the project is built (best way: as packages on the built path that I can easily process)? My only approach until now is to get the project's source with something like ``` // assuming the project exists (to exclude instance checks etc.) MavenProject project = (MavenProject) getPluginContext().get("project"); String projectSource = project.getSourceDirectory(); ``` and then processing the contents of this directory with file manipulation. But this seems so ugly to me that I am quite sure a better solution exists (and I just wan't able to find it with google, the maven pages and stackoverflow).

Original source