All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.plugin.changes.ChangesValidatorMojo Maven / Gradle / Ivy

Go to download

Creates a release history for inclusion into the site and assists in generating an announcement mail.

There is a newer version: 2.12.1
Show newest version
package org.apache.maven.plugin.changes;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.changes.schema.ChangesSchemaValidator;
import org.apache.maven.plugin.changes.schema.SchemaValidatorException;
import org.apache.maven.plugin.changes.schema.XmlValidationHandler;
import org.xml.sax.SAXParseException;

/**
 * 
 * Goal which validate the changes.xml file.
 * 
 * @goal changes-validate
 * 
 * @author olamy
 * @version $Id: ChangesValidatorMojo.java 1098310 2011-05-01 14:10:27Z dennisl $
 * @since 2.1
 * @threadSafe
 */
public class ChangesValidatorMojo
    extends AbstractMojo
{

    /**
     * @component role="org.apache.maven.plugin.changes.schema.ChangesSchemaValidator" roleHint="default"
     */
    private ChangesSchemaValidator changesSchemaValidator;

    /**
     * The changes xsd version.
     *
     * @parameter expression="${changes.xsdVersion}" default-value="1.0.0"
     */
    private String changesXsdVersion;

    /**
     * Mojo failure if validation failed. If not and validation failed only a warning will be logged.
     *
     * @parameter expression="${changes.validate.failed}" default-value="false"
     */
    private boolean failOnError;

    /**
     * The path of the changes.xml file that will be converted into an HTML report.
     *
     * @parameter expression="${changes.xmlPath}" default-value="src/changes/changes.xml"
     */
    private File xmlPath;

    /** 
     * @see org.apache.maven.plugin.Mojo#execute()
     */
    public void execute()
        throws MojoExecutionException, MojoFailureException
    {

        if ( !xmlPath.exists() )
        {
            getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." );
            return;
        }

        try
        {
            XmlValidationHandler xmlValidationHandler = changesSchemaValidator
                .validateXmlWithSchema( xmlPath, changesXsdVersion, failOnError );
            boolean hasErrors = !xmlValidationHandler.getErrors().isEmpty();
            if ( hasErrors )
            {
                logSchemaValidation( xmlValidationHandler.getErrors() );
                if ( failOnError )
                {
                    throw new MojoExecutionException( "changes.xml file " + xmlPath.getAbsolutePath()
                        + " is not valid, see previous errors." );
                }
                else
                {
                    getLog().info( " skip previous validation errors due to failOnError=false." );
                }
            }
        }
        catch ( SchemaValidatorException e )
        {
            if ( failOnError )
            {
                throw new MojoExecutionException( "failed to validate changes.xml file " + xmlPath.getAbsolutePath()
                    + ": " + e.getMessage(), e );
            }
        }
    }

    private void logSchemaValidation( List errors )
    {
        getLog().warn( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() );
        getLog().warn( "validation errors: " );
        for ( SAXParseException error : errors )
        {
            getLog().warn( error.getMessage() );
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy