/** * Calculates an SCM tag name based on a pattern. This pattern can include every parameter reference that can be * resolved by <a href= * "https://maven.apache.org/ref/3.3.9/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">PluginParameterExpressionEvaluator</a>. * * @param pattern the pattern for the tag name which may contain variables listed above. * @param project the Maven project to be used for version calculation during parameter resolution. * @param evaluator the Maven plugin parameter expression evaluator used to evaluate expressions containing parameter * references. * @return the name of the tag derived from the pattern. */ public static String getTagName(String pattern, MavenProject project, PluginParameterExpressionEvaluator evaluator) { Preconditions.checkArgument(pattern != null, "Need a tag name pattern to calculate the tag name."); Preconditions.checkArgument(evaluator != null, "Need an expression evaluator to calculate the tag name."); try { StringBuilder sb = new StringBuilder(pattern); int start = -1; while ((start = sb.indexOf("@{")) > -1) { int end = sb.indexOf("}"); String var = sb.substring(start + 2, end); String resolved; // the parameter project.version gets a special treatment and will not be resolved by the evaluator but gets the // release version instead if (Objects.equal("project.version", var)) { resolved = MavenVersionUtil.calculateReleaseVersion(project.getVersion()); } else { String expression = "${" + var + "}"; resolved = evaluator.evaluate(expression).toString(); } sb.replace(start, end + 1, resolved); } return sb.toString(); } catch (ExpressionEvaluationException e) { throw new RuntimeException("Could not resolve expressions in pattern: " + pattern, e); } }
public void expandProjectVariables(PluginParameterExpressionEvaluator expressionEvaluator) { if (this.variablesExpanded) { return; } expandUnmappedData(expressionEvaluator); expandMappedData(expressionEvaluator); this.variablesExpanded = true; }
private String expand(String s, PluginParameterExpressionEvaluator expressionEvaluator) { String var = s.replace("@{", "${"); try { String evaluated = expressionEvaluator.evaluate(var).toString(); return evaluated; } catch (ExpressionEvaluationException e) { throw new RuntimeException(e.getMessage(), e); } }
public WorkflowExecutor(ProcessingWorkflow workflow, Map<String, CDIMojoProcessingStep> processingSteps, Log log, PluginParameterExpressionEvaluator expressionEvaluator) { this.workflow = workflow; this.processingSteps = processingSteps; this.log = log; this.expressionEvaluator = expressionEvaluator; }
@Override public final void execute() throws MojoExecutionException, MojoFailureException { if (System.getProperty(SYSPROP_PRINT_WF) != null) { WorkflowUtil.printWorkflow(getGoalName(), getPluginDescriptor(), Optional.fromNullable(this.workflowDescriptor), createLogWrapper()); return; } System.setProperty("org.jboss.logging.provider", "slf4j"); String logLevel = "info"; if (getLog().isDebugEnabled()) { logLevel = "debug"; } System.setProperty("org.slf4j.simpleLogger.log.org.jboss.weld", logLevel); Weld weld = new Weld(); weld.addExtension(this); addPluginDependencies(weld); WeldContainer weldContainer = null; try { weldContainer = weld.initialize(); if (System.getProperty(SYSPROP_PRINT_STEPS) != null) { WorkflowUtil.printAvailableSteps(this.allAvailableProcessingSteps, createLogWrapper()); return; } WorkflowUtil.addExecutionContexts(getWorkflow()); Map<String, CDIMojoProcessingStep> processingSteps = getAllProcessingSteps(weldContainer); PluginParameterExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(this._session, this._mojoExecution); WorkflowExecutor executor = new WorkflowExecutor(getWorkflow(), processingSteps, getLog(), expressionEvaluator); executor.validate(!this._settings.isOffline()); executor.execute(); } finally { if (weldContainer != null && weldContainer.isRunning()) { weldContainer.shutdown(); } } }
@CheckForNull private String getString(MavenProject pom, String parameter) { MavenProject oldProject = session.getCurrentProject(); try { // Switch to the project for which we try to resolve the property. session.setCurrentProject(pom); for (MojoExecution exec : mojoExecutions) { Xpp3Dom configuration = exec.getConfiguration(); PlexusConfiguration pomConfiguration = new XmlPlexusConfiguration(configuration); PlexusConfiguration config = pomConfiguration.getChild(parameter); ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, exec); BasicStringConverter converter = new BasicStringConverter(); String value = converter.fromExpression(config, expressionEvaluator); if (value != null) { return value; } } } catch (Exception e) { log.warn(String.format("Failed to get parameter '%s' for goal '%s': %s", parameter, COMPILE_GOAL, e.getMessage())); } finally { session.setCurrentProject(oldProject); } return null; }
private Object evalPluginParameterExpression(String expression) { PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(mavenSession, mojoExecution); try { return evaluator.evaluate(expression); } catch (ExpressionEvaluationException e) { getLog().error("Failed to evaluate [" + expression + "]", e); } return null; }
@MojoProduces private PluginParameterExpressionEvaluator getExpressionEvaluator() { return new PluginParameterExpressionEvaluator(this.session, this.mojoExecution); }