001/*
002 *   Copyright (C) 2005 Christian Schulte <cs@schulte.it>
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: MainModelValidateMojo.java 5228 2016-04-24 11:08:06Z schulte $
029 *
030 */
031package org.jomc.mojo;
032
033import java.util.logging.Level;
034import org.apache.maven.plugin.MojoExecutionException;
035import org.apache.maven.plugins.annotations.LifecyclePhase;
036import org.apache.maven.plugins.annotations.Mojo;
037import org.apache.maven.plugins.annotations.Parameter;
038import org.apache.maven.plugins.annotations.ResolutionScope;
039import org.jomc.modlet.ModelContext;
040import org.jomc.modlet.ModelValidationReport;
041
042/**
043 * Validates a project's main model.
044 *
045 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
046 * @version $JOMC: MainModelValidateMojo.java 5228 2016-04-24 11:08:06Z schulte $
047 */
048@Mojo( name = "validate-main-model",
049       defaultPhase = LifecyclePhase.PROCESS_CLASSES,
050       requiresDependencyResolution = ResolutionScope.TEST,
051       threadSafe = true )
052public final class MainModelValidateMojo extends AbstractJomcMojo
053{
054
055    /**
056     * Constant for the name of the tool backing the mojo.
057     */
058    private static final String TOOLNAME = "ModelValidator";
059
060    /**
061     * Execution strategy of the goal ({@code always} or {@code once-per-session}).
062     *
063     * @since 1.1
064     */
065    @Parameter( name = "validateMainModelExecutionStrategy",
066                property = "jomc.validateMainModelExecutionStrategy",
067                defaultValue = "once-per-session" )
068    private String validateMainModelExecutionStrategy;
069
070    /**
071     * Creates a new {@code MainModelValidateMojo} instance.
072     */
073    public MainModelValidateMojo()
074    {
075        super();
076    }
077
078    @Override
079    protected void executeTool() throws Exception
080    {
081        this.logSeparator();
082        this.logProcessingModel( TOOLNAME, this.getModel() );
083
084        final ModelContext context = this.createModelContext( this.getMainClassLoader() );
085        final ModelValidationReport validationReport = context.validateModel( this.getModel( context ) );
086
087        this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
088
089        if ( !validationReport.isModelValid() )
090        {
091            throw new MojoExecutionException( Messages.getMessage( "failedValidatingMainModel" ) );
092        }
093
094        this.logToolSuccess( TOOLNAME );
095    }
096
097    @Override
098    protected String getGoal() throws MojoExecutionException
099    {
100        return "validate-main-model";
101    }
102
103    @Override
104    protected String getExecutionStrategy() throws MojoExecutionException
105    {
106        return this.validateMainModelExecutionStrategy;
107    }
108
109}