Normally you're not supposed to call a Maven plugin's mojo from another mojo but it may happen times when you have this need. In my case I've developed a plugin and I needed to call the Maven License Plugin from within my mojo.
I succeeded in achieving this through the Maven Executor plugin.
Without further ado here's the code that worked for me:
[...]
* @requiresProject
* @requiresDependencyResolution compile
* @threadSafe
*/
public class FormatMojo extends AbstractVerifyMojo
{
/**
* The project currently being build.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject mavenProject;
/**
* The current Maven session.
*
* @parameter expression="${session}"
* @required
* @readonly
*/
private MavenSession mavenSession;
/**
* The Maven BuildPluginManager component.
*
* @component
* @required
*/
private BuildPluginManager pluginManager;
[...]
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
[...]
// I needed to add a plugin dependency where my license.txt file is located.
Dependency dep = new Dependency();
dep.setGroupId("org.xwiki.commons");
dep.setArtifactId("xwiki-commons-tool-verification-resources");
dep.setVersion("5.4");
Plugin licensePlugin = plugin(
groupId("com.mycila"),
artifactId("license-maven-plugin"),
version("2.6"));
licensePlugin.setDependencies(Collections.singletonList(dep));
executeMojo(
licensePlugin,
goal("format"),
configuration(
element(name("header"), "license.txt"),
element(name("strictCheck"), "true"),
element(name("headerDefinitions"),
element(name("headerDefinition"), "license-xml-definition.xml")),
element(name("includes"),
element(name("include"), "src/main/resources/**/*.xml"))
),
executionEnvironment(
this.project,
this.mavenSession,
this.pluginManager
)
);
}
[...]
}
* @requiresProject
* @requiresDependencyResolution compile
* @threadSafe
*/
public class FormatMojo extends AbstractVerifyMojo
{
/**
* The project currently being build.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject mavenProject;
/**
* The current Maven session.
*
* @parameter expression="${session}"
* @required
* @readonly
*/
private MavenSession mavenSession;
/**
* The Maven BuildPluginManager component.
*
* @component
* @required
*/
private BuildPluginManager pluginManager;
[...]
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
[...]
// I needed to add a plugin dependency where my license.txt file is located.
Dependency dep = new Dependency();
dep.setGroupId("org.xwiki.commons");
dep.setArtifactId("xwiki-commons-tool-verification-resources");
dep.setVersion("5.4");
Plugin licensePlugin = plugin(
groupId("com.mycila"),
artifactId("license-maven-plugin"),
version("2.6"));
licensePlugin.setDependencies(Collections.singletonList(dep));
executeMojo(
licensePlugin,
goal("format"),
configuration(
element(name("header"), "license.txt"),
element(name("strictCheck"), "true"),
element(name("headerDefinitions"),
element(name("headerDefinition"), "license-xml-definition.xml")),
element(name("includes"),
element(name("include"), "src/main/resources/**/*.xml"))
),
executionEnvironment(
this.project,
this.mavenSession,
this.pluginManager
)
);
}
[...]
}
And boom it worked!
Hope this can be useful to someone.