-1

I write a Mojo Plugin. It has a Pamameter like:

*@Mojo(name = "showModus", requiresProject = false, defaultPhase = LifecyclePhase.PROCESS_RESOURCES)<br>
public class ShowModus extends AbstractMojo { <br>
@Parameter (property = "modus", defaultValue = "${modus}")<br>
private String modus;*

in Pom.xml of Mojo Plugin i define a property for this:

<properties><modus>1.1.1</modus></properties>

It works fine if i call the Mojo goal direct under project order per mvn

But if i call the mojo plugin direct form maven repo or. in directory without mojo plugin the Parameter modus is not set.

I understand that mvn can not find the pom.xml of mojo plugin project and therefore can not set the value to parameter.

Is there any way where i can set the pom properties of the mojo maven plugin to the generated plugin.xml ?

I wourd like to call (use) the mojo maven plugin direct from maven repo with certain parameter whereby their value set or inject from pom.xml of plugin

Greeting

Hidde
  • 11,493
  • 8
  • 43
  • 68
NAT
  • 1

1 Answers1

0

I don't completly understand why you want to write these values as properties in the pom.xml. It is much easier to write the in a Java constant. But maybe there are some aspects for this decision.

Some basics:

  1. You can read and parse a pom.xml with the MavenXpp3Reader (this class is part of the maven-model module)
  2. Maven writes the pom.xml into the jar at /META-INF/maven/<groupId>/<artifactId>/pom.xml
  3. the plugin itself is on classpath while executing a maven goal
  4. you can access classpath resources by using getClass().getResourceAsStream(...)
  5. you can get the Properties by using the method getProperties

Packing up all these parts it looks something like this:

final String pathToPluginPom = "/META-INF/maven/de.example/example-maven-plugin/pom.xml";
try (InputStream stream = getClass().getResourceAsStream(pathToPluginPom)) {
    final MavenXpp3Reader reader = new MavenXpp3Reader();
    final Model model = reader.read(stream);
    final Properties properties = model.getProperties();
    final String myPropertyValue = properties.getProperty("myproperty");
    getLog().info("Property: " + myPropertyValue);
} catch (final IOException | XmlPullParserException ex) {
    getLog().error("ERROR!", ex);
}
Josef Reichardt
  • 2,778
  • 3
  • 22
  • 37