3

I'm loading a Maven project as described here. I'm trying to figure out how I can retrieve the source roots so I can figure out the Java classes I have so my Mojo can use them.

I tried a couple of the methods in there, like getResources or getScriptSources without luck. Any idea?

Thanks in advance!

Edit:

I was asked to elaborate a little bit in what I'm attempting to do, so here it is:

The plugin I'm developing will take the sources in the project and create test cases from those. Unless configured, I want to generate tests for all the classes, and for that, I need to somehow figure out where are my sources so I can configure properly.

Hope that helps.

Here's the repository. I planned on publishing it later but I provided source as requested.

Community
  • 1
  • 1
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106

2 Answers2

3

Have you read the plugin developers documentation?

That page will link to Plugins Cookbook which links to Mojo Developer Cookbook which has The maven project, or the effective pom. and gives you access to org.apache.maven.project.MavenProject object via

/** @parameter default-value="${project}" */
private org.apache.maven.project.MavenProject mavenProject;

Alternatively via Java 5 annotations

@Component
MavenProject project;

You can call getCompileSourceRoots() to get a list of the directories that will be used for compilation.

You will also need to do more reading about how to setup inclusion/exclusions. You can use other plugins as examples of how to do this, e.g. maven-compiler-plugin

If you want to use annotations, it is very important to make sure your pom is configured as per using annotations and that you use annotations at the class level as well. Mixing javadoc annotations might not work.

Bae
  • 7,516
  • 5
  • 36
  • 42
  • I did read it and tried that as well, but the getCompileSourceRoots() returns empty list. I was expecting the src/main/java at leaset but no luck. – javydreamercsw Nov 26 '12 at 22:54
  • I already took a look at the maven-compiler-plugin and the way they do it but is equivalent to getting the MavenProject and calling getCompileSourceRoots() but also gives an empty list. The project I'm trying has one source file in the default src/main/java path. – javydreamercsw Nov 26 '12 at 22:59
  • Without access to your source (and time/inclination to hack) is as per mailing list response * bottom up - add to your code copy-and-paste from compiler plugin * top down - polymorph compiler plugin into your code You should find where the problem is. Alternatively debug step trace compiler plugin to see how/why getCompileSourceRoots() is set correctly, and do the same for your plugin. – Bae Nov 27 '12 at 00:12
  • Sounds reasonable, I'll give it a try! – javydreamercsw Nov 27 '12 at 12:58
  • Added repository link while I keep looking into the issue. – javydreamercsw Nov 27 '12 at 22:38
  • The above answer does work as seen my bitbucket contributions. What doesn't work is use of Java 1.5 annotations \@Parameter (not sure if it should work) and \@Component (which should work) – Bae Nov 28 '12 at 03:21
0

I think the simplest solution would be to define a mojo parameter:

/**
 * @parameter default-value="${project.build.sourceDirectory}"
 * @required
 */
private File sourceDirectory;

or with new annotation based definition:

@Parameter(required = true, defaultValue="${project.build.sourceDirectory}"}
private File sourceDirectory;

which should give your wished result.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235