下面列出了怎么用org.apache.maven.plugin.descriptor.MojoDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。
private ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor )
{
String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
List<String> scopes = new ArrayList<String>( 2 );
if ( StringUtils.isNotEmpty( scopeToCollect ) )
{
scopes.add( scopeToCollect );
}
if ( StringUtils.isNotEmpty( scopeToResolve ) )
{
scopes.add( scopeToResolve );
}
if ( scopes.isEmpty() )
{
return null;
}
else
{
return new CumulativeScopeArtifactFilter( scopes );
}
}
private ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor )
{
String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
List<String> scopes = new ArrayList<>( 2 );
if ( StringUtils.isNotEmpty( scopeToCollect ) )
{
scopes.add( scopeToCollect );
}
if ( StringUtils.isNotEmpty( scopeToResolve ) )
{
scopes.add( scopeToResolve );
}
if ( scopes.isEmpty() )
{
return null;
}
else
{
return new CumulativeScopeArtifactFilter( scopes );
}
}
private ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor )
{
String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
List<String> scopes = new ArrayList<>( 2 );
if ( StringUtils.isNotEmpty( scopeToCollect ) )
{
scopes.add( scopeToCollect );
}
if ( StringUtils.isNotEmpty( scopeToResolve ) )
{
scopes.add( scopeToResolve );
}
if ( scopes.isEmpty() )
{
return null;
}
else
{
return new CumulativeScopeArtifactFilter( scopes );
}
}
private ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor )
{
String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
List<String> scopes = new ArrayList<>( 2 );
if ( StringUtils.isNotEmpty( scopeToCollect ) )
{
scopes.add( scopeToCollect );
}
if ( StringUtils.isNotEmpty( scopeToResolve ) )
{
scopes.add( scopeToResolve );
}
if ( scopes.isEmpty() )
{
return null;
}
else
{
return new CumulativeScopeArtifactFilter( scopes );
}
}
private ArtifactFilter getArtifactFilter(MojoDescriptor mojoDescriptor) {
String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
List<String> scopes = new ArrayList<String>(2);
if (StringUtils.isNotEmpty(scopeToCollect)) {
scopes.add(scopeToCollect);
}
if (StringUtils.isNotEmpty(scopeToResolve)) {
scopes.add(scopeToResolve);
}
if (scopes.isEmpty()) {
return null;
} else {
return new CumulativeScopeArtifactFilter(scopes);
}
}
private ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor )
{
String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
List<String> scopes = new ArrayList<String>( 2 );
if ( StringUtils.isNotEmpty( scopeToCollect ) )
{
scopes.add( scopeToCollect );
}
if ( StringUtils.isNotEmpty( scopeToResolve ) )
{
scopes.add( scopeToResolve );
}
if ( scopes.isEmpty() )
{
return null;
}
else
{
return new CumulativeScopeArtifactFilter( scopes );
}
}
private ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor )
{
String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
List<String> scopes = new ArrayList<>( 2 );
if ( StringUtils.isNotEmpty( scopeToCollect ) )
{
scopes.add( scopeToCollect );
}
if ( StringUtils.isNotEmpty( scopeToResolve ) )
{
scopes.add( scopeToResolve );
}
if ( scopes.isEmpty() )
{
return null;
}
else
{
return new CumulativeScopeArtifactFilter( scopes );
}
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
// get plugin descriptor
InputStream inputStream = AbstractHelmMojo.class.getResourceAsStream("/META-INF/maven/plugin.xml");
assertNotNull(inputStream, "Plugin descriptor not found.");
plugin = new PluginDescriptorBuilder().build(new InterpolationFilterReader(new BufferedReader(new XmlStreamReader(inputStream)), new HashMap<>()));
// get mojos
mojoDescriptors = new HashMap<>();
for (MojoDescriptor mojoDescriptor : plugin.getMojos()) {
mojoDescriptors.put((Class<? extends AbstractHelmMojo>) Class.forName(mojoDescriptor.getImplementation()), mojoDescriptor);
}
}
@Test
public void extractionOfMojoSpecificConfigurationAndMergingwithDefaultMojoConfiguration() throws Exception {
InputStream is = getClass().getResourceAsStream("/META-INF/maven/plugin.xml");
assertNotNull(is);
PluginDescriptor pluginDescriptor = pluginDescriptorBuilder.build(new InputStreamReader(is, "UTF-8"));
String goal = merger.determineGoal("io.takari.maven.plugins.jar.Jar", pluginDescriptor);
assertEquals("We expect the goal name to be 'jar'", "jar", goal);
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal);
PlexusConfiguration defaultMojoConfiguration = mojoDescriptor.getMojoConfiguration();
System.out.println(defaultMojoConfiguration);
PlexusConfiguration configurationFromMaven = builder("configuration") //
.es("jar") //
.es("sourceJar").v("true").ee() //
.ee() //
.buildPlexusConfiguration();
PlexusConfiguration mojoConfiguration = merger.extractAndMerge(goal, configurationFromMaven, defaultMojoConfiguration);
String xml = mojoConfiguration.toString();
assertXpathEvaluatesTo("java.io.File", "/configuration/classesDirectory/@implementation", xml);
assertXpathEvaluatesTo("${project.build.outputDirectory}", "/configuration/classesDirectory/@default-value", xml);
assertXpathEvaluatesTo("java.util.List", "/configuration/reactorProjects/@implementation", xml);
assertXpathEvaluatesTo("${reactorProjects}", "/configuration/reactorProjects/@default-value", xml);
assertXpathEvaluatesTo("true", "/configuration/sourceJar", xml);
}
/**
* Taken from MojoExecutor of Don Brown. Make it working with Maven 3.1.
*
* @param plugin
* @param goal
* @param configuration
* @param env
* @throws MojoExecutionException
* @throws PluginResolutionException
* @throws PluginDescriptorParsingException
* @throws InvalidPluginDescriptorException
* @throws PluginManagerException
* @throws PluginConfigurationException
* @throws MojoFailureException
*/
private void executeMojo( Plugin plugin, String goal, Xpp3Dom configuration )
throws MojoExecutionException, PluginResolutionException, PluginDescriptorParsingException,
InvalidPluginDescriptorException, MojoFailureException, PluginConfigurationException, PluginManagerException
{
if ( configuration == null )
{
throw new NullPointerException( "configuration may not be null" );
}
PluginDescriptor pluginDescriptor = getPluginDescriptor( plugin );
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
if ( mojoDescriptor == null )
{
throw new MojoExecutionException( "Could not find goal '" + goal + "' in plugin " + plugin.getGroupId()
+ ":" + plugin.getArtifactId() + ":" + plugin.getVersion() );
}
MojoExecution exec = mojoExecution( mojoDescriptor, configuration );
pluginManager.executeMojo( getMavenSession(), exec );
}
/**
* Extracts the subset of the given configuration containing only the values accepted by the plugin/goal. The
* configuration is modified in-place. The the extraction fail the configuration stays unchanged.
*
* @param mojo the Wisdom mojo
* @param plugin the plugin object
* @param goal the goal / mojo
* @param configuration the global configuration
*/
public static void extractEligibleConfigurationForGoal(AbstractWisdomMojo mojo,
Plugin plugin, String goal, Xpp3Dom configuration) {
try {
MojoDescriptor descriptor = mojo.pluginManager.getMojoDescriptor(plugin, goal,
mojo.remoteRepos, mojo.repoSession);
final List<Parameter> parameters = descriptor.getParameters();
Xpp3Dom[] children = configuration.getChildren();
if (children != null) {
for (int i = children.length - 1; i >= 0; i--) {
Xpp3Dom child = children[i];
if (!contains(parameters, child.getName())) {
configuration.removeChild(i);
}
}
}
} catch (Exception e) {
mojo.getLog().warn("Cannot extract the eligible configuration for goal " + goal + " from the " +
"configuration");
mojo.getLog().debug(e);
// The configuration is not changed.
}
}
public void callPluginGoal(String fullGoal) {
String[] parts = splitGoalSpec(fullGoal);
Plugin plugin = project.getPlugin(parts[0]);
String goal = parts[1];
if (plugin == null) {
throw new IllegalStateException("No goal " + fullGoal + " found in pom.xml");
}
try {
String executionId = null;
if (goal != null && goal.length() > 0 && goal.indexOf('#') > -1) {
int pos = goal.indexOf('#');
executionId = goal.substring(pos + 1);
goal = goal.substring(0, pos);
}
PluginDescriptor pluginDescriptor = getPluginDescriptor(project, plugin);
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal);
if (mojoDescriptor == null) {
throw new MojoExecutionException("Could not find goal '" + goal + "' in plugin "
+ plugin.getGroupId() + ":"
+ plugin.getArtifactId() + ":"
+ plugin.getVersion());
}
MojoExecution exec = getMojoExecution(executionId, mojoDescriptor);
pluginManager.executeMojo(session, exec);
} catch (Exception e) {
throw new IllegalStateException("Unable to execute mojo", e);
}
}
private MojoExecution getMojoExecution(String executionId, MojoDescriptor mojoDescriptor) {
if (executionId != null) {
return new MojoExecution(mojoDescriptor, executionId);
} else {
return new MojoExecution(mojoDescriptor, toXpp3Dom(mojoDescriptor.getMojoConfiguration()));
}
}
private void collectDependencyRequirements( Set<String> scopesToResolve, Set<String> scopesToCollect,
Collection<MojoExecution> mojoExecutions )
{
for ( MojoExecution mojoExecution : mojoExecutions )
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
scopesToResolve.addAll( toScopes( mojoDescriptor.getDependencyResolutionRequired() ) );
scopesToCollect.addAll( toScopes( mojoDescriptor.getDependencyCollectionRequired() ) );
}
}
private void collectDependencyRequirements( Set<String> scopesToResolve, Set<String> scopesToCollect,
Collection<MojoExecution> mojoExecutions )
{
for ( MojoExecution mojoExecution : mojoExecutions )
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
scopesToResolve.addAll( toScopes( mojoDescriptor.getDependencyResolutionRequired() ) );
scopesToCollect.addAll( toScopes( mojoDescriptor.getDependencyCollectionRequired() ) );
}
}
private void collectDependencyRequirements( Set<String> scopesToResolve, Set<String> scopesToCollect,
Collection<MojoExecution> mojoExecutions )
{
for ( MojoExecution mojoExecution : mojoExecutions )
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
scopesToResolve.addAll( toScopes( mojoDescriptor.getDependencyResolutionRequired() ) );
scopesToCollect.addAll( toScopes( mojoDescriptor.getDependencyCollectionRequired() ) );
}
}
private void collectDependencyRequirements( Set<String> scopesToResolve, Set<String> scopesToCollect,
Collection<MojoExecution> mojoExecutions )
{
for ( MojoExecution mojoExecution : mojoExecutions )
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
scopesToResolve.addAll( toScopes( mojoDescriptor.getDependencyResolutionRequired() ) );
scopesToCollect.addAll( toScopes( mojoDescriptor.getDependencyCollectionRequired() ) );
}
}
private void collectDependencyRequirements(Set<String> scopesToResolve, Set<String> scopesToCollect,
Collection<MojoExecution> mojoExecutions) {
for (MojoExecution mojoExecution : mojoExecutions) {
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
scopesToResolve.addAll(toScopes(mojoDescriptor.getDependencyResolutionRequired()));
scopesToCollect.addAll(toScopes(mojoDescriptor.getDependencyCollectionRequired()));
}
}
private void collectDependencyRequirements( Set<String> scopesToResolve, Set<String> scopesToCollect,
Collection<MojoExecution> mojoExecutions )
{
for ( MojoExecution mojoExecution : mojoExecutions )
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
scopesToResolve.addAll( toScopes( mojoDescriptor.getDependencyResolutionRequired() ) );
scopesToCollect.addAll( toScopes( mojoDescriptor.getDependencyCollectionRequired() ) );
}
}
private void collectDependencyRequirements( Set<String> scopesToResolve, Set<String> scopesToCollect,
Collection<MojoExecution> mojoExecutions )
{
for ( MojoExecution mojoExecution : mojoExecutions )
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
scopesToResolve.addAll( toScopes( mojoDescriptor.getDependencyResolutionRequired() ) );
scopesToCollect.addAll( toScopes( mojoDescriptor.getDependencyCollectionRequired() ) );
}
}
@SuppressWarnings("unchecked")
protected void startProject(MavenProject project, String executionId, XmlPlexusConfiguration process) throws InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, MojoFailureException, MojoExecutionException, PluginManagerException {
Plugin plugin = this.project.getPlugin("org.wildfly.swarm:wildfly-swarm-plugin");
Xpp3Dom config = getConfiguration(project, executionId);
Xpp3Dom processConfig = getProcessConfiguration(process);
Xpp3Dom globalConfig = getGlobalConfig();
Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(processConfig, config);
mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, globalConfig);
PluginDescriptor pluginDescriptor = this.pluginManager.loadPlugin(plugin, project.getRemotePluginRepositories(), this.repositorySystemSession);
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo("start");
MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, mergedConfig);
mavenSession.setCurrentProject(project);
this.pluginManager.executeMojo(mavenSession, mojoExecution);
List<SwarmProcess> launched = (List<SwarmProcess>) mavenSession.getPluginContext(pluginDescriptor, project).get("swarm-process");
List<SwarmProcess> procs = (List<SwarmProcess>) getPluginContext().get("swarm-process");
if (procs == null) {
procs = new ArrayList<>();
getPluginContext().put("swarm-process", procs);
}
procs.addAll(launched);
mavenSession.setCurrentProject(this.project);
}
@SuppressWarnings("unchecked")
protected void startProject(MavenProject project, String executionId, XmlPlexusConfiguration process) throws InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, MojoFailureException, MojoExecutionException, PluginManagerException {
Plugin plugin = this.project.getPlugin(THORNTAIL_MAVEN_PLUGIN);
Xpp3Dom config = getConfiguration(project, executionId);
Xpp3Dom processConfig = getProcessConfiguration(process);
Xpp3Dom globalConfig = getGlobalConfig();
Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(processConfig, config);
mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, globalConfig);
PluginDescriptor pluginDescriptor = this.pluginManager.loadPlugin(plugin, project.getRemotePluginRepositories(), this.repositorySystemSession);
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo("start");
MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, mergedConfig);
mavenSession.setCurrentProject(project);
this.pluginManager.executeMojo(mavenSession, mojoExecution);
List<SwarmProcess> launched = (List<SwarmProcess>) mavenSession.getPluginContext(pluginDescriptor, project).get(THORNTAIL_PROCESS);
List<SwarmProcess> procs = (List<SwarmProcess>) getPluginContext().get(THORNTAIL_PROCESS);
if (procs == null) {
procs = new ArrayList<>();
getPluginContext().put(THORNTAIL_PROCESS, procs);
}
procs.addAll(launched);
mavenSession.setCurrentProject(this.project);
}
@SuppressWarnings("unchecked")
protected void startProject(MavenProject project, String executionId, XmlPlexusConfiguration process) throws InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, MojoFailureException, MojoExecutionException, PluginManagerException {
Plugin plugin = this.project.getPlugin("org.wildfly.swarm:wildfly-swarm-plugin");
Xpp3Dom config = getConfiguration(project, executionId);
Xpp3Dom processConfig = getProcessConfiguration(process);
Xpp3Dom globalConfig = getGlobalConfig();
Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(processConfig, config);
mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, globalConfig);
PluginDescriptor pluginDescriptor = this.pluginManager.loadPlugin(plugin, project.getRemotePluginRepositories(), this.repositorySystemSession);
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo("start");
MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, mergedConfig);
mavenSession.setCurrentProject(project);
this.pluginManager.executeMojo(mavenSession, mojoExecution);
List<SwarmProcess> launched = (List<SwarmProcess>) mavenSession.getPluginContext(pluginDescriptor, project).get("swarm-process");
List<SwarmProcess> procs = (List<SwarmProcess>) getPluginContext().get("swarm-process");
if (procs == null) {
procs = new ArrayList<>();
getPluginContext().put("swarm-process", procs);
}
procs.addAll(launched);
mavenSession.setCurrentProject(this.project);
}
public void callPluginGoal(String fullGoal) throws MojoFailureException, MojoExecutionException {
String[] parts = splitGoalSpec(fullGoal);
Plugin plugin = project.getPlugin(parts[0]);
String goal = parts[1];
if (plugin == null) {
throw new MojoFailureException("No goal " + fullGoal + " found in pom.xml");
}
try {
String executionId = null;
if (goal != null && goal.length() > 0 && goal.indexOf('#') > -1) {
int pos = goal.indexOf('#');
executionId = goal.substring(pos + 1);
goal = goal.substring(0, pos);
}
PluginDescriptor pluginDescriptor = getPluginDescriptor(project, plugin);
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal);
if (mojoDescriptor == null) {
throw new MojoExecutionException("Could not find goal '" + goal + "' in plugin "
+ plugin.getGroupId() + ":"
+ plugin.getArtifactId() + ":"
+ plugin.getVersion());
}
MojoExecution exec = getMojoExecution(executionId, mojoDescriptor);
pluginManager.executeMojo(session, exec);
} catch (Exception e) {
throw new MojoExecutionException("Unable to execute mojo", e);
}
}
private MojoExecution getMojoExecution(String executionId, MojoDescriptor mojoDescriptor) {
if (executionId != null) {
return new MojoExecution(mojoDescriptor, executionId);
} else {
return new MojoExecution(mojoDescriptor, toXpp3Dom(mojoDescriptor.getMojoConfiguration()));
}
}
String determineGoal(String className, PluginDescriptor pluginDescriptor) throws ComponentConfigurationException {
List<MojoDescriptor> mojos = pluginDescriptor.getMojos();
for (MojoDescriptor mojo : mojos) {
if (className.equals(mojo.getImplementation())) {
return mojo.getGoal();
}
}
throw new ComponentConfigurationException("Cannot find the goal implementation with " + className);
}
private void whenCheckingForUpdate() {
PluginDescriptor pluginDescriptor = new PluginDescriptor();
pluginDescriptor.setPluginArtifact(new DefaultArtifact("de.is24", "junit", "23", null, "maven-plugin", "", null));
MojoDescriptor mojoDescriptor = new MojoDescriptor();
mojoDescriptor.setPluginDescriptor(pluginDescriptor);
result = objectUnderTest.checkForUpdate(new MojoExecution(mojoDescriptor));
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) throws ParameterResolutionException {
try {
// get descriptor
Class<? extends AbstractHelmMojo> mojoType = (Class<AbstractHelmMojo>) parameterContext.getParameter().getType();
MojoDescriptor descriptor = mojoDescriptors.get(mojoType);
assertNotNull(descriptor, "Plugin descriptor for " + mojoType.getSimpleName() + " not found, run 'maven-plugin-plugin:descriptor'.");
// create mojo with default values
AbstractHelmMojo mojo = spy(mojoType);
for (Parameter parameter : descriptor.getParameters()) {
if (parameter.getDefaultValue() == null || !parameter.isEditable() || parameter.getType().equals("boolean")) {
continue;
}
getField(mojoType, parameter.getName()).set(mojo, resolve(context, parameter.getDefaultValue()));
}
// read mojo values from annotations
MojoProperty[] mojoProperties = ArrayUtils.addAll(
context.getRequiredTestClass().getAnnotationsByType(MojoProperty.class),
context.getRequiredTestMethod().getAnnotationsByType(MojoProperty.class));
for (MojoProperty mojoProperty : mojoProperties) {
getField(mojoType, mojoProperty.name()).set(mojo, resolve(context, mojoProperty.value()));
}
// settings
getField(mojoType, "settings").set(mojo, new Settings());
// plexus SecDispatcher
SecDispatcher secDispatcher = spy(DefaultSecDispatcher.class);
FieldSetter.setField(secDispatcher, DefaultSecDispatcher.class.getDeclaredField("_cipher"), new DefaultPlexusCipher());
getField(mojoType, "securityDispatcher").set(mojo, secDispatcher);
// validate that every parameter is set
for (Parameter paramter : descriptor.getParameters()) {
if (paramter.isRequired()) {
assertNotNull(
getField(mojoType, paramter.getName()).get(mojo),
"Parameter '" + paramter.getName() + "' not set for mojo '" + mojoType.getSimpleName() + "'.");
}
}
return mojo;
} catch (ReflectiveOperationException | PlexusCipherException e) {
throw new ParameterResolutionException("Failed to setup mockito.", e);
}
}
public Goal(MojoDescriptor mojoDescriptor) {
this.descriptor = mojoDescriptor;
}
public VersionsExpressionEvaluator( MavenSession mavenSession, PathTranslator pathTranslator,
MavenProject mavenProject )
{
super( mavenSession, new MojoExecution( new MojoDescriptor() ), pathTranslator, null, mavenProject,
mavenSession.getExecutionProperties() );
}