Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process project sources in maven plugin

I'm writing a maven plugin that basically should do the following:

  1. process all classes of the project built
  2. create a file describing parts of the source code
  3. 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).

like image 784
Dominik Schreiber Avatar asked May 22 '12 07:05

Dominik Schreiber


2 Answers

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.

like image 153
khmarbaise Avatar answered Nov 01 '22 14:11

khmarbaise


The syntax to inject the MavenProject is now:

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

You will have to add a dependency for maven-project to your plugin's pom:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-project</artifactId>
    <version>2.0.6</version>
</dependency>

Then to manage the source files I'm not sure but it seems likely that the preferred answer is to use the filesystem API against MavenProject.getSourceDirectory(). I don't know what you would expect core maven to tell you about the source beyond that. Of course other plugins -- like for compiling java -- may have more sophisticated domain models for certain types of projects.

like image 27
Partly Cloudy Avatar answered Nov 01 '22 14:11

Partly Cloudy