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

org.commonjava.maven.plugins.execroot.HighestBasedirGoal Maven / Gradle / Ivy

Go to download

Provides goals to discover various paths based on the projects currently building. For example, directory of the execution root project, directory of a given project (based on groupId/artifactId), and highest project basedir that hasn't been resolved from the repository.

There is a newer version: 1.0
Show newest version
/*
 * Copyright 2011 Red Hat, Inc.
 * 
 * Licensed 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.
 */

package org.commonjava.maven.plugins.execroot;

import org.apache.maven.plugin.ContextEnabled;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Stack;

/**
 * @goal highest-basedir
 * @requiresProject true 
 * @phase initialize
 * 
 * Find the topmost directory in this Maven execution, and set it as a property.
 */
public class HighestBasedirGoal
    extends AbstractDirectoryGoal
    implements Mojo, ContextEnabled
{

    public static final class PathComparator
        implements Comparator
    {
        public int compare( final File first, final File second )
        {
            return first.getAbsolutePath().compareTo( second.getAbsolutePath() );
        }
    }

    protected static final String HIGHEST_DIR_CONTEXT_KEY = "directories.highestDir";

    /**
     * @parameter default-value="${reactorProjects}"
     * @readonly
     */
    protected List projects;

    /**
     * {@inheritDoc}
     * @throws MojoExecutionException 
     * @see org.commonjava.maven.plugins.execroot.AbstractDirectoryGoal#findDirectory()
     */
    @Override
    protected File findDirectory()
        throws MojoExecutionException
    {
        final Stack toCheck = new Stack();
        toCheck.addAll( projects );

        final List files = new ArrayList();
        while ( !toCheck.isEmpty() )
        {
            final MavenProject p = toCheck.pop();
            if ( p.getBasedir() == null )
            {
                // we've hit a parent that was resolved. Don't bother going higher up the hierarchy.
                continue;
            }

            if ( !files.contains( p.getBasedir() ) )
            {
                // add to zero to maybe help pre-sort the paths...the shortest (parent) paths should end up near the
                // top.
                files.add( 0, p.getBasedir() );
            }

            if ( p.getParent() != null )
            {
                toCheck.add( p.getParent() );
            }
        }

        if ( files.isEmpty() )
        {
            throw new MojoExecutionException( "No project base directories found! Are you sure you're "
                + "executing this on a valid Maven project?" );
        }

        Collections.sort( files, new PathComparator() );
        final File dir = files.get( 0 );

        if ( files.size() > 1 )
        {
            final File next = files.get( 1 );
            if ( !next.getAbsolutePath().startsWith( dir.getAbsolutePath() ) )
            {
                throw new MojoExecutionException( "Cannot find a single highest directory for this project set. "
                    + "First two candidates directories don't share a common root." );
            }
        }

        return dir;
    }

    /**
     * {@inheritDoc}
     * @see org.commonjava.maven.plugins.execroot.AbstractDirectoryGoal#getContextKey()
     */
    @Override
    protected String getContextKey()
    {
        return HIGHEST_DIR_CONTEXT_KEY;
    }

    /**
     * {@inheritDoc}
     * @see org.commonjava.maven.plugins.execroot.AbstractDirectoryGoal#getLogLabel()
     */
    @Override
    protected String getLogLabel()
    {
        return "Highest basedir";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy