in maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java [147:225]
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
try {
Class<?> holder = parameterContext.getTarget().get().getClass();
PluginDescriptor descriptor = extensionContext
.getStore(ExtensionContext.Namespace.GLOBAL)
.get(PluginDescriptor.class, PluginDescriptor.class);
Model model =
extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(Model.class, Model.class);
InjectMojo parameterInjectMojo =
parameterContext.getAnnotatedElement().getAnnotation(InjectMojo.class);
String goal;
if (parameterInjectMojo != null) {
String pom = parameterInjectMojo.pom();
if (pom != null && !pom.isEmpty()) {
try (Reader r = openPomUrl(holder, pom, new Path[1])) {
Model localModel = new MavenStaxReader().read(r);
model = new MavenMerger().merge(localModel, model, false, null);
model = new DefaultModelPathTranslator(new DefaultPathTranslator())
.alignToBaseDirectory(model, Paths.get(getBasedir()), null);
}
}
goal = parameterInjectMojo.goal();
} else {
InjectMojo methodInjectMojo = AnnotationSupport.findAnnotation(
parameterContext.getDeclaringExecutable(), InjectMojo.class)
.orElse(null);
if (methodInjectMojo != null) {
goal = methodInjectMojo.goal();
} else {
goal = getGoalFromMojoImplementationClass(
parameterContext.getParameter().getType());
}
}
Set<MojoParameter> mojoParameters = new LinkedHashSet<>();
for (AnnotatedElement ae :
Arrays.asList(parameterContext.getDeclaringExecutable(), parameterContext.getAnnotatedElement())) {
mojoParameters.addAll(AnnotationSupport.findRepeatableAnnotations(ae, MojoParameter.class));
}
String[] coord = mojoCoordinates(goal);
XmlNode pluginConfiguration = model.getBuild().getPlugins().stream()
.filter(p ->
Objects.equals(p.getGroupId(), coord[0]) && Objects.equals(p.getArtifactId(), coord[1]))
.map(ConfigurationContainer::getConfiguration)
.findFirst()
.orElseGet(() -> new XmlNodeImpl("config"));
List<XmlNode> children = mojoParameters.stream()
.map(mp -> new XmlNodeImpl(mp.name(), mp.value()))
.collect(Collectors.toList());
XmlNode config = new XmlNodeImpl("configuration", null, null, children, null);
pluginConfiguration = XmlNode.merge(config, pluginConfiguration);
// load default config
// pluginkey = groupId : artifactId : version : goal
Mojo mojo = lookup(Mojo.class, coord[0] + ":" + coord[1] + ":" + coord[2] + ":" + coord[3]);
for (MojoDescriptor mojoDescriptor : descriptor.getMojos()) {
if (Objects.equals(mojoDescriptor.getGoal(), coord[3])) {
if (pluginConfiguration != null) {
pluginConfiguration = finalizeConfig(pluginConfiguration, mojoDescriptor);
}
}
}
Session session = getInjector().getInstance(Session.class);
Project project = getInjector().getInstance(Project.class);
MojoExecution mojoExecution = getInjector().getInstance(MojoExecution.class);
ExpressionEvaluator evaluator = new WrapEvaluator(
getInjector(), new PluginParameterExpressionEvaluatorV4(session, project, mojoExecution));
EnhancedComponentConfigurator configurator = new EnhancedComponentConfigurator();
configurator.configureComponent(
mojo, new XmlPlexusConfiguration(pluginConfiguration), evaluator, null, null);
return mojo;
} catch (Exception e) {
throw new ParameterResolutionException("Unable to resolve mojo", e);
}
}