Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [m2e-dev] Reading a plugin's configuration during project configuration

On 10/28/2011 12:28 PM, Navarro Perez, Antonio wrote:
Hi everybody,

I am building a m2e extension for a code generation maven plugin. That plugin’s configuration allows for a custom directory that contains the generated sources, e.g.

<build>
  <plugins>
    <plugin>
      <groupId>groupID</groupId>
      <artifactId>artifactID</artifactId>
      <version>1.2.3</version>
      <configuration>
        <outputDirectory>target/generated-sources/ast</outputDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>

I need this parameter to add the directory as source directory during project configuration. What is the preferred way to access configuration parameter’s like these?

TIA and Cheers

What I've done (per advice from this list) is:

  ProjectConfigurationRequest request = ...;
  IProgressMonitor monitor = ...;
  IMaven maven = ...;

  IMavenProjectFacade mavenProjectFacade = request.getMavenProjectFacade();

  for (MojoExecution execution: mavenProjectFacade.getMojoExecutions(
    "groupId", "artifactId", monitor, "goal1", "goal2", ..., "goalN")) {
    Object configureMojo = maven.getConfiguredMojo(request.getMavenSession(), execution, Object.class);
    // use reflection to pull fields from the mojo
  }

Where goal1, ... goalN are the goals that your configurator can work
with. The advantage of this approach over parsing the pom directly is
that you don't need to think about all the ways a plugin can get
configured: interpolated properties, parent poms, configuration at the
plugin level or at the pluginExectution level,etc.

The main unpleasantness is getting the field values by reflection. The
class loader for configuredMojo will almost certainly not be the class
loader for your configurator, so reflection will really be your only option.

One other piece of advice: in your lifecycle-mapping-metadata.xml file,
you'll probably want to map your configurator to the
maven-compiler-plugin mojo, so that the eclipse project buildpath is
already set up when your configurator runs. In your plugin.xml, you'll
want your configurator marked as
secondaryTo="org.eclipse.m2e.jdt.javaConfigurator". Finally, in
lifecycle-mapping-metadata.xml, also map the maven plugin you're
actually interested in to <action><ignore/></action>.

  - Ian


CONFIDENTIALITY NOTICE:  This message is intended only for the use and review of the individual or entity to which it is addressed and may contain information that is privileged and confidential.  If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message solely to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited.  If you have received this communication in error, please notify sender immediately by telephone or return email.  Thank you.


Back to the top