I'm writing a maven plugin that basically should do the following:
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With