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: MainSourcesManageMojo.java 5228 2016-04-24 11:08:06Z schulte $
029 *
030 */
031package org.jomc.mojo;
032
033import java.io.File;
034import java.util.logging.Level;
035import org.apache.maven.plugin.MojoExecutionException;
036import org.apache.maven.plugins.annotations.LifecyclePhase;
037import org.apache.maven.plugins.annotations.Mojo;
038import org.apache.maven.plugins.annotations.Parameter;
039import org.apache.maven.plugins.annotations.ResolutionScope;
040
041/**
042 * Manages a projects' main source files.
043 *
044 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
045 * @version $JOMC: MainSourcesManageMojo.java 5228 2016-04-24 11:08:06Z schulte $
046 */
047@Mojo( name = "manage-main-sources",
048       defaultPhase = LifecyclePhase.PROCESS_RESOURCES,
049       requiresDependencyResolution = ResolutionScope.TEST,
050       threadSafe = true )
051public final class MainSourcesManageMojo extends AbstractSourcesManageMojo
052{
053
054    /**
055     * Execution strategy of the goal ({@code always} or {@code once-per-session}).
056     *
057     * @since 1.1
058     */
059    @Parameter( name = "manageMainSourcesExecutionStrategy",
060                property = "jomc.manageMainSourcesExecutionStrategy",
061                defaultValue = "once-per-session" )
062    private String manageMainSourcesExecutionStrategy;
063
064    /**
065     * Creates a new {@code MainSourcesManageMojo} instance.
066     */
067    public MainSourcesManageMojo()
068    {
069        super();
070    }
071
072    @Override
073    protected String getSourcesModuleName() throws MojoExecutionException
074    {
075        return this.getModuleName();
076    }
077
078    @Override
079    protected ClassLoader getSourcesClassLoader() throws MojoExecutionException
080    {
081        return this.getMainClassLoader();
082    }
083
084    @Override
085    protected File getSourcesDirectory() throws MojoExecutionException
086    {
087        final File sourcesDirectory = this.getSourceDirectory();
088        boolean compileSourceRoot = false;
089
090        for ( int i = 0, l0 = this.getMavenProject().getCompileSourceRoots().size(); i < l0; i++ )
091        {
092            final String element = (String) this.getMavenProject().getCompileSourceRoots().get( i );
093
094            if ( sourcesDirectory.equals( this.getAbsoluteFile( element ) ) )
095            {
096                compileSourceRoot = true;
097                break;
098            }
099        }
100
101        if ( !compileSourceRoot )
102        {
103            if ( !sourcesDirectory.exists() && !sourcesDirectory.mkdirs() )
104            {
105                throw new MojoExecutionException( Messages.getMessage(
106                    "failedCreatingDirectory", sourcesDirectory.getAbsolutePath() ) );
107
108            }
109
110            this.getMavenProject().addCompileSourceRoot( sourcesDirectory.getAbsolutePath() );
111            this.log( Level.INFO, Messages.getMessage(
112                      "addedCompileSourceRoot", sourcesDirectory.getAbsolutePath() ), null );
113
114        }
115
116        return sourcesDirectory;
117    }
118
119    @Override
120    protected String getGoal() throws MojoExecutionException
121    {
122        return "manage-main-sources";
123    }
124
125    @Override
126    protected String getExecutionStrategy() throws MojoExecutionException
127    {
128        return this.manageMainSourcesExecutionStrategy;
129    }
130
131}