Example usage for org.apache.maven.model.building ModelProblemCollector add

List of usage examples for org.apache.maven.model.building ModelProblemCollector add

Introduction

In this page you can find the example usage for org.apache.maven.model.building ModelProblemCollector add.

Prototype

void add(ModelProblemCollectorRequest req);

Source Link

Document

Adds the specified problem.

Usage

From source file:com.coutemeier.maven.jsr223.profileactivator.JSR223ProfileActivator.java

License:Open Source License

private boolean evaluateExpression(Profile profile, ProfileActivationContext context,
        ModelProblemCollector problemCollector) {
    Activation activation = profile.getActivation();
    ActivationProperty property = activation.getProperty();
    String engineName = property.getName().substring(1);
    String script = property.getValue();
    if (engineName == null || engineName.trim().length() == 0) {
        engineName = DEFAULT_SCRIPT_ENGINE_NAME;
    }//from  w  w w  .j  a v a  2s  .  co  m

    // create a JavaScript engine
    ScriptEngine engine = ScriptEngineHelper.getEngine(engineName);
    // We haven't foud a script engine, so we can't evaluate the script
    if (engine == null) {
        final String message = "[JSR223ProfileActivator] Script engine unknown " + profile.getId();
        logger.debug(message);
        problemCollector.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE).setMessage(message)
                .setLocation(property.getLocation("")));
        return false;
    }
    logger.debug("[JSR223ProfileActivator] ScriptEngine {}. Trying to evaluate the expression {}", engineName,
            script);

    String name = ScriptEngineHelper.getPropertyNameForJSR223(context);
    engine.put(name, new ExpressionManager(context.getSystemProperties(), context.getProjectProperties(),
            context.getUserProperties()));

    try {
        Object value = engine.eval(script);
        logger.debug("[JSR223ProfileActivator] Expression evaluated value: {}", value);
        return true;

    } catch (ScriptException cause) {
        final String message = "[JSR223ProfileActivator] There has been an error evaluating the expression in the profile "
                + profile.getId() + ": " + cause.getMessage();
        logger.debug(message);
        problemCollector.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE).setMessage(message)
                .setLocation(property.getLocation("")));
    }
    return false;
}

From source file:com.github.sviperll.maven.profiledep.DependenciesProfileSelector.java

@Override
public List<Profile> getActiveProfiles(Collection<Profile> availableProfiles, ProfileActivationContext context,
        ModelProblemCollector problems) {
    List<Profile> activatedProfiles = defaultProfileSelector.getActiveProfiles(availableProfiles, context,
            problems);//w ww .  ja v a2  s. c o m
    try {
        DependencyResolution resolution = DependencyResolution.resolve(availableProfiles, activatedProfiles,
                context.getActiveProfileIds());
        return resolution.activeProfiles();
    } catch (ResolutionValidationException ex) {
        ModelProblemCollectorRequest request = new ModelProblemCollectorRequest(ModelProblem.Severity.FATAL,
                ModelProblem.Version.BASE);
        request.setMessage("\n" + ex.renderResolutionTree());
        problems.add(request);
        return Collections.emptyList();
    }
}