Skip to content

Check for unknown elements in the maven configuration section #782

@thomaswue

Description

@thomaswue

Unknown elements in the maven configuration section of the plugin should lead to errors instead of silently ignoring the parameter (as would be the default). Here is example code how this could be done:

@Mojo(name = "check-config", defaultPhase = LifecyclePhase.VALIDATE)
public class CheckConfigMojo extends AbstractMojo {

    @Parameter(property = "myplugin.someParam")
    private String someParam;

    @Component
    private MavenProject project;

    @Parameter(defaultValue = "${mojoExecution}", readonly = true)
    private MojoExecution mojoExecution;

    public void execute() throws MojoExecutionException {
        Set<String> knownParams = Arrays.stream(this.getClass().getDeclaredFields())
            .filter(f -> f.isAnnotationPresent(Parameter.class))
            .map(Field::getName)
            .collect(Collectors.toSet());

        Xpp3Dom cfg = (Xpp3Dom) mojoExecution.getConfiguration();
        if (cfg != null) {
            for (Xpp3Dom child : cfg.getChildren()) {
                if (!knownParams.contains(child.getName())) {
                    getLog().warn("Unknown configuration parameter: " + child.getName());
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions