Package org.apache.maven.project

Source Code of org.apache.maven.project.Project

package org.apache.maven.project;

/* ====================================================================
*   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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.maven.ArtifactListBuilder;
import org.apache.maven.DependencyClasspathBuilder;
import org.apache.maven.MavenConstants;
import org.apache.maven.MavenUtils;
import org.apache.maven.jelly.MavenJellyContext;
import org.apache.maven.plugin.PluginManager;
import org.apache.maven.plugin.UnknownPluginException;
import org.apache.maven.project.io.stax.MavenStaxReader;
import org.apache.maven.project.io.stax.MavenStaxWriter;
import org.apache.maven.verifier.ChecksumVerificationException;
import org.apache.maven.verifier.DependencyVerifier;
import org.apache.maven.verifier.RepoConfigException;
import org.apache.maven.verifier.UnsatisfiedDependencyException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

/**
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
* @author <a href="mailto:glennm@apache.org">Glenn McAllister</a>
* @version $Id: Project.java 517014 2007-03-11 21:15:50Z ltheussl $
* @todo Change the file reference for the source POM to an URL.
*/
public class Project
{
    /**
     * LOGGER for debug and output
     */
    private static final Log LOGGER = LogFactory.getLog( Project.class );

    /**
     * Distributions map that associates the distribution ids
     * with the distribution objects.
     */
    private HashMap versionMap;

    /**
     * Parent project.
     */
    private Project parent;

    /**
     * File that this POM object was derived from.
     */
    private File file;

    /**
     * Map of dependency ids to their real paths in the system.
     */
    private Map dependencyPaths = new HashMap();

    /**
     * Dependencies Map so that an individual dependency can
     * be retrieved by id.
     */
    private Map dependencyMap = new HashMap();

    /**
     * Artifact list.
     */
    private List artifactList;

    /**
     * This project's Jelly context.
     */
    private MavenJellyContext context;

    /**
     * TODO: use commons-collections.
     */
    private List contextStack = new ArrayList();

    /**
     * the maven.xml file for this project
     */
    private File mavenXml;

    private final Model model;

    private String originalGroupId;

    /**
     * Default constructor.
     */
    public Project()
    {
        model = new Model();
    }

    /**
     * Constructor with prebuilt model.
     *
     * @param in the input reader
     * @throws Exception an error occured reading the file
     * @todo should not have to throw Exception
     */
    public Project( Reader in )
        throws Exception
    {
        MavenStaxReader reader = new MavenStaxReader();
        model = reader.read( in );
        resolveDependencies();
        resolveVersions();
    }

    /**
     * Constructor with prebuilt model.
     *
     * @param url the input url
     * @throws Exception an error occured reading the file
     * @todo should not have to throw Exception
     */
    public Project( URL url )
        throws Exception
    {
        MavenStaxReader reader = new MavenStaxReader();
        model = reader.read( url.getFile() );
        resolveDependencies();
        resolveVersions();
    }

    /**
     * Constructor with prebuilt model.
     *
     * @param path the input path
     * @throws Exception an error occured reading the file
     * @todo should not have to throw Exception
     */
    public Project( String path )
        throws Exception
    {
        MavenStaxReader reader = new MavenStaxReader();
        model = reader.read( path );
        resolveDependencies();
        resolveVersions();
    }

    /**
     * @return the maven.xml file
     */
    public File getMavenXml()
    {
        // The getFile() is needed because interpolation calls this before the file is set
        if ( ( mavenXml == null ) && ( getFile() != null ) )
        {
            mavenXml = new File( getFile().getParentFile(), MavenConstants.BUILD_FILE_NAME );
        }
        return mavenXml;
    }

    /**
     * @return true if the maven.xml file for the project exists
     */
    public boolean hasMavenXml()
    {
        File mavenXml = getMavenXml();
        if ( mavenXml != null )
        {
            return mavenXml.exists();
        }
        else
        {
            return false;
        }
    }

    // ----------------------------------------------------------------------
    // A C C E S S O R S
    // ----------------------------------------------------------------------

    // trying to align dep.id with project.id
    /**
     * @return the id of the project
     */
    public String getId()
    {
        // The id has already been set.
        if ( ( model.getId() != null ) && ( model.getId().indexOf( ":" ) > 0 ) )
        {
            return model.getId();
        }
        return getGroupId() + ":" + getArtifactId();
    }

    public void setId( String id )
    {
        int i = id.indexOf( "+" );
        int j = id.indexOf( ":" );

        if ( i > 0 )
        {
            setGroupId( id.substring( 0, i ) );
            setArtifactId( id.replace( '+', '-' ) );
        }
        else if ( j > 0 )
        {
            setGroupId( id.substring( 0, j ) );
            setArtifactId( id.substring( j + 1 ) );
        }
        else
        {
            setGroupId( id );
            setArtifactId( id );
        }
    }

    /**
     * Get the artifact id.
     *
     * @return The artifactId
     */
    public String getArtifactId()
    {
        return model.getArtifactId();
    }

    /**
     * Get the plugin context using the plugin name.
     *
     * @param pluginId Plugin name.
     * @return The plugin context create for the named plugin when it was loaded for
     *         this project.
     * @throws UnknownPluginException if the plugin could not be found
     * @todo [1.0] - when we are caching plugins, this should load on demand, also move to plugin manager
     * @deprecated use the tag instead
     */
    public MavenJellyContext getPluginContext( String pluginId )
        throws Exception
    {
        PluginManager pluginManager = context.getMavenSession().getPluginManager();
        return pluginManager.getPluginContext( pluginId );
    }

    /**
     * Set the context attribute.
     *
     * @param context the context to use
     */
    public void setContext( MavenJellyContext context )
    {
        if ( contextStack.size() > 0 )
        {
            LOGGER.warn( "replacing a context when the stack is not empty" );
        }
        this.context = context;
    }

    /**
     * Push a new context onto the stack.
     *
     * @param context
     */
    public void pushContext( MavenJellyContext context )
    {
        LOGGER.debug( "pushing on " + context + " over " + this.context + " in " + getId() );
        contextStack.add( this.context );
        this.context = context;
    }

    public void popContext()
    {
        MavenJellyContext context = (MavenJellyContext) contextStack.get( contextStack.size() - 1 );
        contextStack.remove( contextStack.size() - 1 );
        LOGGER.debug( "popping off " + this.context + " for " + context + " in " + getId() );
        this.context = context;
    }

    /**
     * Get the context attribute.
     *
     * @return The
     */
    public MavenJellyContext getContext()
    {
        return context;
    }

    /**
     * Set the artifactList attribute.
     *
     * @param artifactList list of artifacts for the project
     */
    public void setArtifacts( List artifactList )
    {
        this.artifactList = artifactList;
    }

    /**
     * Get the atrifact attribute.
     *
     * @return The list of artifacts for the project
     */
    public List getArtifacts()
    {
        return artifactList;
    }

    /**
     * Set the file that this POM was derived from.
     *
     * @param file POM file
     */
    public void setFile( File file )
    {
        this.file = file;
    }

    /**
     * Get the file this POM was derived from
     *
     * @return POM file
     */
    public File getFile()
    {
        return file;
    }

    /**
     * @return the project that this one extends, if any
     */
    public Project getParent()
    {
        return parent;
    }

    /**
     * Determine whether this project has a parent.
     *
     * @return State of ancestry.
     */
    public boolean hasParent()
    {
        return getParent() != null;
    }

    /**
     * Set the project that this one extends
     *
     * @param parent the parent project
     */
    public void setParent( Project parent )
    {
        this.parent = parent;
    }

    /**
     * Get the parent's basedir.
     *
     * @return Parent's basedir.
     */
    public File parentBasedir()
    {
        return new File( getExtend() ).getParentFile();
    }

    /**
     * Get parent maven.xml file.
     *
     * @return Parent's maven.xml file.
     */
    public File parentMavenXml()
    {
        return new File( parentBasedir(), MavenConstants.BUILD_FILE_NAME );
    }

    /**
     * @return the directory name for this proejct's artifacts
     */
    public String getArtifactDirectory()
    {
        if ( getGroupId() != null )
        {
            return getGroupId();
        }

        // Now we want the artifact directory to be the legacy form
        // of the id which contains no ":" character so lets flip
        // it back to legacy form.
        return standardToLegacyId( getId() );
    }

    /**
     * Add a unique dependency for this project.
     *
     * @param dependency Dependency for this project.
     */
    public void addDependency( Dependency dependency )
    {
        if ( !getDependencies().contains( dependency ) )
        {
            model.addDependency( dependency );
            dependencyMap.put( dependency.getKey(), dependency );
        }
    }

    /**
     * @param srcList    source list to add items from
     * @param targetList target list to add unique items to
     * @todo eventually, just use sets instead.
     */
    static void mergeLists( List srcList, List targetList )
    {
        if ( srcList != null )
        {
            for ( Iterator i = srcList.iterator(); i.hasNext(); )
            {
                Object o = i.next();
                if ( !targetList.contains( o ) )
                {
                    targetList.add( o );
                }
            }
        }
    }

    /**
     * @param dependencies parent dependencies
     * @todo eventually, just use the map
     */
    private void addParentDependencies( List dependencies )
    {
        for ( Iterator i = dependencies.iterator(); i.hasNext(); )
        {
            addDependency( (Dependency) i.next() );
        }
    }

    /**
     * Get the list of dependent projects ids.
     *
     * @return the set of all dependencies' project ids
     */
    public Set getDependentProjectIds()
    {
        Set projectIds = new TreeSet();
        List dependencies = getDependencies();
        Dependency dependency;
        for ( int i = 0; i < dependencies.size(); i++ )
        {
            dependency = (Dependency) dependencies.get( i );
            projectIds.add( dependency.getId() );
        }
        return projectIds;
    }

    /**
     * Set an individual dependency's classpath entry.
     *
     * @param depId Dependency id.
     * @param path  Classpath for the given dependency.
     */
    public void setDependencyPath( String depId, String path )
    {
        dependencyPaths.put( depId, path );
    }

    /**
     * Get an individual dependencies classpath entry.
     *
     * @param depId Dependency id.
     * @return Classpath for the given dependency.
     */
    public String getDependencyPath( String depId )
    {
        return (String) dependencyPaths.get( legacyToDependencyKey( depId ) );
    }

    /**
     * Get an individual dependency by id.
     *
     * @param depId Dependency id.
     * @return Dependency for the given id.
     */
    public Dependency getDependency( String depId )
    {
        return (Dependency) dependencyMap.get( legacyToDependencyKey( depId ) );
    }

    /**
     * Get flag to indicate the presence of a source repository.
     *
     * @return <code>true</code> if the project is part of a repository,
     *         <code>false</code> otherwise
     */
    public boolean hasRepository()
    {
        return ( getRepository() != null );
    }

    /**
     * Indicate whether the POM is of the latest version.
     *
     * @return <code>true</code> if the pom version is the same as the
     *         currently running version of maven specified by {@link MavenConstants},
     *         or <code>false</code> otherwise
     */
    public boolean isPomCurrent()
    {
        return Integer.parseInt( getPomVersion() ) == MavenConstants.POM_VERSION;
    }

    /**
     * Return a distribution for this project.
     *
     * @param versionId the id of the version to return
     * @return List of distributions.
     */
    public Version getVersionById( String versionId )
    {
        return (Version) versionMap.get( versionId );
    }

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    /**
     * Flag to indicate the project has been verified.
     */
    private boolean initialized = false;

    /**
     * Dependency Verifier
     */
    private DependencyVerifier dependencyVerifier;

    /**
     * Dependency classpath used for compilation.
     */
    private String dependencyClasspath;

    /**
     * Initialize the project. This consists of the following stages.
     * <p/>
     * 1) Build the Ant project that can be used within plugin.jelly scripts and
     * other Jelly scripts.
     * <p/>
     * 2) Build the artifact list.
     * <p/>
     * 3) Build the dependency classpath.
     * <p/>
     * 4) Create the dependency verifier.
     * <p/>
     * 5) Initialize the driver for this project.
     *
     * @throws IOException If an error occurs during project initialization.
     */
    public void initialize()
        throws IOException
    {
        if ( initialized )
        {
            return;
        }

        buildArtifactList();
        dependencyClasspath = DependencyClasspathBuilder.build( this );
        setDependencyVerifier( new DependencyVerifier( this ) );
        if ( getBuild() != null )
        {
            resolveDirectories( getBuild(), file.getParentFile() );
        }
        initialized = true;
    }

    /**
     * Verify the dependencies for this project.
     *
     * @throws RepoConfigException            If an error occurs checking the local settings.
     * @throws UnsatisfiedDependencyException If an error occurs due to missing exceptions.
     * @throws ChecksumVerificationException  if the downloaded files fail verification
     */
    public void verifyDependencies()
        throws RepoConfigException, UnsatisfiedDependencyException, ChecksumVerificationException
    {
        LOGGER.debug( "Verifying dependencies for " + getId() );

        // Only attempt to verify the dependencies if a project.xml file exists, and it is fully-fledged (not an uninitialized parent project).
        if ( ( getFile() != null ) && getFile().exists() && initialized )
        {
            getDependencyVerifier().verify();
        }
    }

    /**
     * Set the dependency verifier.
     *
     * @param dependencyVerifier Dependency Verifier.
     */
    public void setDependencyVerifier( DependencyVerifier dependencyVerifier )
    {
        this.dependencyVerifier = dependencyVerifier;
    }

    /**
     * Get the dependency verifier for this project. We can probably share a
     * dependency verifier.
     *
     * @return The DependencyVerifier for this project.
     */
    public DependencyVerifier getDependencyVerifier()
    {
        return dependencyVerifier;
    }

    /**
     * Get the dependency classpath.
     *
     * @return The dependency classpath.
     */
    public String getDependencyClasspath()
    {
        return dependencyClasspath;
    }

    /**
     * Build the artifact list.
     */
    public void buildArtifactList()
    {
        artifactList = ArtifactListBuilder.build( this );
    }

    // ----------------------------------------------------------------------
    // L E G A C Y  I D  S U P P O R T
    // ----------------------------------------------------------------------

    /**
     * This is to support methods that are using the legacy form of
     * the project id. Currently the id is <groupId>:<artifactId> but the
     * following methods assume <groupId>:
     * <p/>
     * Project::getDependencyPath( <groupId> )
     * Project::getDependency( <groupId> )
     * <p/>
     * We don't want users to have to alter any usage until we have properly
     * deprecated the use of the <groupId> form.
     *
     * @param id the legacy id to convert
     * @return the id in standard format
     */
    public static String legacyToStandardId( String id )
    {
        String newId = id;
        if ( id.indexOf( "+" ) != -1 )
        {
            // legacy format is groupId "+" partial artifactId
            // standard format is groupId ":" groupId "-" partialArtifactId
            int plusPos = id.indexOf( "+" );
            String groupId = id.substring( 0, plusPos );
            String partialArtifactId = id.substring( plusPos + 1 );
            newId = groupId + ":" + groupId + "-" + partialArtifactId;
        }
        else if ( id.indexOf( ":" ) == -1 )
        {
            // if no :, set groupId as artifactId
            newId += ":" + id;
        }
        return newId;
    }

    public String legacyToDependencyKey( String id )
    {
        String newId = legacyToStandardId( id );
        int sepIndex = newId.indexOf( ':' );

        // check for type
        sepIndex = newId.indexOf( ':', sepIndex + 1 );
        if ( sepIndex < 0 )
        {
            for ( Iterator i = dependencyMap.keySet().iterator(); i.hasNext(); )
            {
                String depId = (String) i.next();
                if ( depId.equals( newId ) )
                {
                    return depId;
                }
                if ( depId.startsWith( newId + ":" ) )
                {
                    return depId;
                }
            }
            newId += ":jar";
        }
        return newId;
    }

    /**
     * This method is to support methods are expecting legacy ids. The following
     * methods expect legacy ids.
     * <p/>
     * MavenJellyContext::getMavenJarOverride( <groupId> )
     * Project::getArtifactDirectory( <groupId> )
     * <p/>
     * We don't want users to have to alter any usage until we have properly
     * deprecated the use of the <groupId> form.
     *
     * @param id the standard id to convert
     * @return the id in legacy format
     */
    public static String standardToLegacyId( String id )
    {
        int i = id.indexOf( ":" );

        if ( i > 0 )
        {
            id = id.substring( i + 1 );
        }

        return id;
    }

    /**
     * Setup inheritance from a parent project.
     *
     * @param parent the parent project
     */
    public void mergeParent( Project parent )
    {
        setParent( parent );

        if ( parent == null )
        {
            return;
        }

        if ( getRepository() == null )
        {
            setRepository( parent.getRepository() );
        }

        if ( getName() == null )
        {
            setName( parent.getName() );
        }

        if ( getUrl() == null )
        {
            setUrl( parent.getUrl() );
        }

        if ( getLogo() == null )
        {
            setLogo( parent.getLogo() );
        }

        if ( getDescription() == null )
        {
            setDescription( parent.getDescription() );
        }

        if ( getIssueTrackingUrl() == null )
        {
            setIssueTrackingUrl( parent.getIssueTrackingUrl() );
        }

        if ( getCurrentVersion() == null )
        {
            setCurrentVersion( parent.getCurrentVersion() );
        }

        if ( getOrganization() == null )
        {
            setOrganization( parent.getOrganization() );
        }

        if ( getInceptionYear() == null )
        {
            setInceptionYear( parent.getInceptionYear() );
        }

        if ( getPackage() == null )
        {
            setPackage( parent.getPackage() );
        }

        if ( getSiteAddress() == null )
        {
            setSiteAddress( parent.getSiteAddress() );
        }

        if ( getSiteDirectory() == null )
        {
            setSiteDirectory( parent.getSiteDirectory() );
        }

        if ( getDistributionSite() == null )
        {
            setDistributionSite( parent.getDistributionSite() );
        }

        if ( getDistributionDirectory() == null )
        {
            setDistributionDirectory( parent.getDistributionDirectory() );
        }

        if ( getPomVersion() == null )
        {
            setPomVersion( parent.getPomVersion() );
        }

        if ( getGumpRepositoryId() == null )
        {
            setGumpRepositoryId( parent.getGumpRepositoryId() );
        }

        if ( getShortDescription() == null )
        {
            setShortDescription( parent.getShortDescription() );
        }

        /*
         if ( model.getGroupId() == null )
         {
         // Don't inherit if it was from an id element
         if ( parent.originalGroupId != null || model.getId() == null )
         {
         // Don't inherit if only artifactId is given, see MAVEN-1704
         if ( !( parent.originalGroupId == null && model.getId() == null ) )
         {
         model.setGroupId( parent.model.getGroupId() );
         }
         }
         }
         */

        // This is equivalent to the above
        if ( model.getGroupId() == null )
        {
            if ( parent.originalGroupId != null )
            {
                model.setGroupId( parent.model.getGroupId() );
            }
        }

        if ( getBuild() == null )
        {
            // Must copy as resolveDirs changes entries
            setBuild( new Build() );
            mergeParent( parent.getBuild(), getBuild() );
        }
        else
        {
            mergeParent( parent.getBuild(), getBuild() );
        }

        // The following replaces the list merges
        if ( ( getMailingLists() == null ) || getMailingLists().isEmpty() )
        {
            setMailingLists( parent.getMailingLists() );
        }
        if ( ( getContributors() == null ) || getContributors().isEmpty() )
        {
            setContributors( parent.getContributors() );
        }
        if ( ( getDevelopers() == null ) || getDevelopers().isEmpty() )
        {
            setDevelopers( parent.getDevelopers() );
        }
        if ( ( getVersions() == null ) || getVersions().isEmpty() )
        {
            model.setVersions( parent.getVersions() );
            resolveVersions();
        }
        if ( ( getLicenses() == null ) || getLicenses().isEmpty() )
        {
            setLicenses( parent.getLicenses() );
        }
        if ( ( getBranches() == null ) || getBranches().isEmpty() )
        {
            setBranches( parent.getBranches() );
        }
        if ( ( getReports() == null ) || getReports().isEmpty() )
        {
            setReports( parent.getReports() );
        }
        if ( ( getPackageGroups() == null ) || getPackageGroups().isEmpty() )
        {
            setPackageGroups( parent.getPackageGroups() );
        }

        //mergeLists( parent.mailingLists, mailingLists );
        //mergeLists( parent.contributors, contributors );
        //mergeLists( parent.developers, developers );
        //mergeLists( parent.licenses, licenses );
        //mergeLists( parent.branches, branches );
        //mergeLists( parent.reports, reports );
        //mergeLists( parent.packageGroups, packageGroups );

        // Now let's do some magic with versions and dependencies as they have maps.
        // We take all the parent's and put them in with the child's.
        //addParentVersions( parent.versions );
        addParentDependencies( parent.getDependencies() );
    }

    /**
     * Resolve relative directories against a base directory.
     *
     * @param build
     * @param basedir the base directory
     * @throws java.io.IOException file resolution error
     */
    private static void resolveDirectories( Build build, File basedir )
        throws IOException
    {
        for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
        {
            Resource r = (Resource) i.next();
            resolveDirectory( r, basedir );
        }

        if ( build.getUnitTest() != null )
        {
            for ( Iterator i = build.getUnitTest().getResources().iterator(); i.hasNext(); )
            {
                Resource r = (Resource) i.next();
                resolveDirectory( r, basedir );
            }
        }

        if ( ( build.getSourceDirectory() != null ) && ( build.getSourceDirectory().length() > 0 ) )
        {
            build.setSourceDirectory( MavenUtils.makeAbsolutePath( basedir, build.getSourceDirectory() ) );
        }

        if ( ( build.getAspectSourceDirectory() != null ) && ( build.getAspectSourceDirectory().length() > 0 ) )
        {
            build.setAspectSourceDirectory( MavenUtils.makeAbsolutePath( basedir, build.getAspectSourceDirectory() ) );
        }

        if ( ( build.getUnitTestSourceDirectory() != null ) && ( build.getUnitTestSourceDirectory().length() > 0 ) )
        {
            build
                .setUnitTestSourceDirectory( MavenUtils.makeAbsolutePath( basedir, build.getUnitTestSourceDirectory() ) );
        }

        if ( ( build.getIntegrationUnitTestSourceDirectory() != null )
            && ( build.getIntegrationUnitTestSourceDirectory().length() > 0 ) )
        {
            build.setIntegrationUnitTestSourceDirectory( MavenUtils.makeAbsolutePath( basedir, build
                .getIntegrationUnitTestSourceDirectory() ) );
        }
    }

    /**
     * Setup inheritance from a parent project build element.
     *
     * @param parent the parent build element
     * @param child
     */
    private static void mergeParent( Build parent, Build child )
    {
        if ( parent == null )
        {
            return;
        }

        if ( child.getDefaultGoal() == null )
        {
            child.setDefaultGoal( parent.getDefaultGoal() );
        }

        if ( child.getSourceDirectory() == null )
        {
            child.setSourceDirectory( parent.getSourceDirectory() );
        }

        if ( child.getUnitTestSourceDirectory() == null )
        {
            child.setUnitTestSourceDirectory( parent.getUnitTestSourceDirectory() );
        }

        if ( child.getIntegrationUnitTestSourceDirectory() == null )
        {
            child.setIntegrationUnitTestSourceDirectory( parent.getIntegrationUnitTestSourceDirectory() );
        }

        if ( child.getAspectSourceDirectory() == null )
        {
            child.setAspectSourceDirectory( parent.getAspectSourceDirectory() );
        }

        if ( child.getNagEmailAddress() == null )
        {
            child.setNagEmailAddress( parent.getNagEmailAddress() );
        }

        if ( ( child.getResources() == null ) || child.getResources().isEmpty() )
        {
            child.setResources( parent.getResources() );
        }
        if ( ( child.getSourceModifications() == null ) || child.getSourceModifications().isEmpty() )
        {
            child.setSourceModifications( parent.getSourceModifications() );
        }

        if ( child.getUnitTest() == null )
        {
            child.setUnitTest( parent.getUnitTest() );
        }
        else
        {
            mergeUnitTestParent( parent.getUnitTest(), child.getUnitTest() );
        }
    }

    private static void mergeUnitTestParent( UnitTest parent, UnitTest child )
    {
        if ( parent == null )
        {
            return;
        }

        // The following replaces the list merges
        if ( ( child.getResources() == null ) || child.getResources().isEmpty() )
        {
            child.setResources( parent.getResources() );
        }
        if ( ( child.getIncludes() == null ) || child.getIncludes().isEmpty() )
        {
            child.setIncludes( parent.getIncludes() );
        }
        if ( ( child.getExcludes() == null ) || child.getExcludes().isEmpty() )
        {
            child.setExcludes( parent.getExcludes() );
        }
    }

    private static void resolveDirectory( Resource resource, File basedir )
        throws IOException
    {
        if ( resource.getDirectory() != null )
        {
            resource.setDirectory( MavenUtils.makeAbsolutePath( basedir, resource.getDirectory() ) );
        }
        else
        {
            resource.setDirectory( basedir.getCanonicalPath() );
        }
    }

    public void addBranch( Branch branch )
    {
        model.addBranch( branch );
    }

    public void addContributor( Contributor contributor )
    {
        model.addContributor( contributor );
    }

    public void addDeveloper( Developer developer )
    {
        model.addDeveloper( developer );
    }

    public void addLicense( License license )
    {
        model.addLicense( license );
    }

    public void addMailingList( MailingList mailingList )
    {
        model.addMailingList( mailingList );
    }

    public void addPackageGroup( PackageGroup packageGroup )
    {
        model.addPackageGroup( packageGroup );
    }

    public void addProperty( String s, String s1 )
    {
        model.addProperty( s, s1 );
    }

    public void addReport( String s )
    {
        model.addReport( s );
    }

    public List getBranches()
    {
        return model.getBranches();
    }

    public Build getBuild()
    {
        return model.getBuild();
    }

    public List getContributors()
    {
        return model.getContributors();
    }

    public String getCurrentVersion()
    {
        return model.getCurrentVersion();
    }

    public List getDependencies()
    {
        return model.getDependencies();
    }

    public String getDescription()
    {
        return model.getDescription();
    }

    public List getDevelopers()
    {
        return model.getDevelopers();
    }

    public String getDistributionDirectory()
    {
        return model.getDistributionDirectory();
    }

    public String getDistributionSite()
    {
        return model.getDistributionSite();
    }

    public String getExtend()
    {
        return model.getExtend();
    }

    public String getGroupId()
    {
        return model.getGroupId();
    }

    public String getGumpRepositoryId()
    {
        return model.getGumpRepositoryId();
    }

    public String getInceptionYear()
    {
        return model.getInceptionYear();
    }

    public String getIssueTrackingUrl()
    {
        return model.getIssueTrackingUrl();
    }

    public List getLicenses()
    {
        return model.getLicenses();
    }

    public String getLogo()
    {
        return model.getLogo();
    }

    public List getMailingLists()
    {
        return model.getMailingLists();
    }

    public String getName()
    {
        return model.getName();
    }

    public Organization getOrganization()
    {
        return model.getOrganization();
    }

    public String getPackage()
    {
        return model.getPackage();
    }

    public List getPackageGroups()
    {
        return model.getPackageGroups();
    }

    public String getPomVersion()
    {
        return model.getPomVersion();
    }

    public Properties getProperties()
    {
        return model.getProperties();
    }

    public List getReports()
    {
        return model.getReports();
    }

    public Repository getRepository()
    {
        return model.getRepository();
    }

    public String getShortDescription()
    {
        return model.getShortDescription();
    }

    public String getSiteAddress()
    {
        return model.getSiteAddress();
    }

    public String getSiteDirectory()
    {
        return model.getSiteDirectory();
    }

    public String getUrl()
    {
        return model.getUrl();
    }

    public List getVersions()
    {
        return model.getVersions();
    }

    public void setVersions( List list )
    {
        model.setVersions( list );
        resolveVersions();
    }

    public void setArtifactId( String s )
    {
        model.setArtifactId( s );
    }

    public void setBranches( List list )
    {
        model.setBranches( list );
    }

    public void setBuild( Build build )
    {
        model.setBuild( build );
    }

    public void setContributors( List list )
    {
        model.setContributors( list );
    }

    public void setCurrentVersion( String s )
    {
        model.setCurrentVersion( s );
    }

    public void setDependencies( List list )
    {
        model.setDependencies( list );
        dependencyMap.clear();
        resolveDependencies();
    }

    public void setDescription( String s )
    {
        model.setDescription( s );
    }

    public void setDevelopers( List list )
    {
        model.setDevelopers( list );
    }

    public void setDistributionDirectory( String s )
    {
        model.setDistributionDirectory( s );
    }

    public void setDistributionSite( String s )
    {
        model.setDistributionSite( s );
    }

    public void setExtend( String s )
    {
        model.setExtend( s );
    }

    public void setGroupId( String s )
    {
        model.setGroupId( s );
    }

    public void setGumpRepositoryId( String s )
    {
        model.setGumpRepositoryId( s );
    }

    public void setInceptionYear( String s )
    {
        model.setInceptionYear( s );
    }

    public void setIssueTrackingUrl( String s )
    {
        model.setIssueTrackingUrl( s );
    }

    public void setLicenses( List list )
    {
        model.setLicenses( list );
    }

    public void setLogo( String s )
    {
        model.setLogo( s );
    }

    public void setMailingLists( List list )
    {
        model.setMailingLists( list );
    }

    public void setName( String s )
    {
        model.setName( s );
    }

    public void setOrganization( Organization organization )
    {
        model.setOrganization( organization );
    }

    public void setPackage( String s )
    {
        model.setPackage( s );
    }

    public void setPackageGroups( List list )
    {
        model.setPackageGroups( list );
    }

    public void setPomVersion( String s )
    {
        model.setPomVersion( s );
    }

    public void setProperties( Properties properties )
    {
        model.setProperties( properties );
    }

    public void setReports( List list )
    {
        model.setReports( list );
    }

    public void setRepository( Repository repository )
    {
        model.setRepository( repository );
    }

    public void setShortDescription( String s )
    {
        model.setShortDescription( s );
    }

    public void setSiteAddress( String s )
    {
        model.setSiteAddress( s );
    }

    public void setSiteDirectory( String s )
    {
        model.setSiteDirectory( s );
    }

    public void setUrl( String s )
    {
        model.setUrl( s );
    }

    /**
     * Add a distribution to this project.
     *
     * @param version Distribution for this project.
     */
    public void addVersion( Version version )
    {
        if ( versionMap != null )
        {
            // if we've already lazy iitialised, keep it up to date
            versionMap.put( version.getId(), version );
        }
        model.addVersion( version );
    }

    /**
     * @todo hopefully can be replaced by appropraite getters in modello
     */
    public void resolveIds()
    {
        originalGroupId = model.getGroupId();
        if ( model.getId() != null )
        {
            if ( ( model.getGroupId() == null ) && ( model.getArtifactId() == null ) )
            {
                setId( model.getId() );
            }
            else
            {
                String id = model.getId();
                if ( model.getGroupId() == null )
                {
                    int j = id.indexOf( ":" );
                    if ( j > 0 )
                    {
                        setGroupId( id.substring( 0, j ) );
                    }
                    else
                    {
                        setGroupId( id );
                    }
                }
                if ( model.getArtifactId() == null )
                {
                    int j = id.indexOf( ":" );
                    if ( j > 0 )
                    {
                        setArtifactId( id.substring( j + 1 ) );
                    }
                    else
                    {
                        setArtifactId( id );
                    }
                }
            }
        }
        else if ( model.getGroupId() == null )
        {
            model.setGroupId( model.getArtifactId() );
            LOGGER.debug( "No groupId found, setting to: " + model.getArtifactId() );
        }
    }

    private void resolveVersions()
    {
        versionMap = new HashMap();
        for ( Iterator i = getVersions().iterator(); i.hasNext(); )
        {
            Version version = (Version) i.next();
            versionMap.put( version.getId(), version );
        }
    }

    /**
     * @todo hopefully can be replaced by appropraite getters in modello
     */
    private void resolveDependencies()
    {
        for ( Iterator i = getDependencies().iterator(); i.hasNext(); )
        {
            Dependency dependency = (Dependency) i.next();
            if ( dependency.getId() != null )
            {
                if ( dependency.getGroupId() == null )
                {
                    String id = dependency.getId();
                    int k = id.indexOf( "+" );
                    int j = id.indexOf( ":" );

                    if ( k > 0 )
                    {
                        dependency.setGroupId( id.substring( 0, k ) );
                        dependency.setArtifactId( id.replace( '+', '-' ) );
                    }
                    else if ( j > 0 )
                    {
                        dependency.setGroupId( id.substring( 0, j ) );
                        dependency.setArtifactId( id.substring( j + 1 ) );
                    }
                    else
                    {
                        dependency.setGroupId( id );
                    }
                }
                if ( dependency.getArtifactId() == null )
                {
                    dependency.setArtifactId( dependency.getId() );
                }
            }
            else if ( dependency.getGroupId() == null )
            {
                dependency.setGroupId( dependency.getArtifactId() );
                LOGGER.debug( "Dependency has no groupId, setting to: " + dependency.getArtifactId() );
            }
            dependency.setId( dependency.getGroupId() + ":" + dependency.getArtifactId() );
            dependencyMap.put( dependency.getKey(), dependency );
        }
    }

    /**
     * Create an XML string from the project.
     *
     * @return XML representation of the project
     * @throws Exception FIXME
     */
    public String getProjectAsString()
        throws Exception
    {
        ByteArrayOutputStream projectStream = new ByteArrayOutputStream();
        if ( model.getModelEncoding() == null )
            model.setModelEncoding( "UTF-8" );
        OutputStreamWriter w = new OutputStreamWriter( projectStream, model.getModelEncoding() );
        try
        {
            MavenStaxWriter writer = new MavenStaxWriter();
            writer.write( w, model );
        }
        finally
        {
            w.close();
        }
        return projectStream.toString( System.getProperty( "file.encoding" ) );
    }

    public String toString()
    {
        return getName() == null ? super.toString() : getName();
    }

    public boolean equals( Object o )
    {
        if ( o.equals( this ) )
        {
            return true;
        }

        if ( !( o instanceof Project ) )
        {
            return false;
        }

        Project project = (Project) o;
        return getId().equals( project.getId() );
    }

    public int hashCode()
    {
        return getId().hashCode();
    }
}
TOP

Related Classes of org.apache.maven.project.Project

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.