MavenProject.java对实现的方法存有疑虑

时间:2014-03-04 11:15:42

标签: java maven

你好我在maven中有这个。(包org.apache.maven.project; MavenProject.java文件)

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

getModel()的位置是:

public Model getModel() {
    return model;
}

有没有人可以帮我理解为什么我的方法getProperties()会再次调用此方法?

这段代码充满了这种结构。有人能解释一下这个结构吗?

谢谢大家!

我在这里发布完整的代码:

package org.apache.maven.project;


import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.model.Build;
import org.apache.maven.model.CiManagement;
import org.apache.maven.model.Contributor;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Developer;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Extension;
import org.apache.maven.model.IssueManagement;
import org.apache.maven.model.License;
import org.apache.maven.model.MailingList;
import org.apache.maven.model.Model;
import org.apache.maven.model.Organization;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.Prerequisites;
import org.apache.maven.model.Profile;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.ReportSet;
import org.apache.maven.model.Reporting;
import org.apache.maven.model.Repository;
import org.apache.maven.model.Resource;
import org.apache.maven.model.Scm;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.project.artifact.MavenMetadataSource;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.aether.graph.DependencyFilter;
import org.sonatype.aether.repository.RemoteRepository;

public class MavenProject
    implements Cloneable
{
public static final String EMPTY_PROJECT_GROUP_ID = "unknown";

public static final String EMPTY_PROJECT_ARTIFACT_ID = "empty-project";

public static final String EMPTY_PROJECT_VERSION = "0";

private Model model;

private MavenProject parent;

private File file;

private Set<Artifact> resolvedArtifacts;

private ArtifactFilter artifactFilter;

private Set<Artifact> artifacts;

private Artifact parentArtifact;

private Set<Artifact> pluginArtifacts;

private List<ArtifactRepository> remoteArtifactRepositories;

private List<ArtifactRepository> pluginArtifactRepositories;

private List<RemoteRepository> remoteProjectRepositories;

private List<RemoteRepository> remotePluginRepositories;

private List<Artifact> attachedArtifacts;

private MavenProject executionProject;

private List<MavenProject> collectedProjects;

private List<String> compileSourceRoots = new ArrayList<String>();

private List<String> testCompileSourceRoots = new ArrayList<String>();

private List<String> scriptSourceRoots = new ArrayList<String>();

private ArtifactRepository releaseArtifactRepository;

private ArtifactRepository snapshotArtifactRepository;

private List<Profile> activeProfiles = new ArrayList<Profile>();

private Map<String, List<String>> injectedProfileIds = new LinkedHashMap<String, List<String>>();

private Set<Artifact> dependencyArtifacts;

private Artifact artifact;

// calculated.
private Map<String, Artifact> artifactMap;

private Model originalModel;

private Map<String, Artifact> pluginArtifactMap;

private Set<Artifact> reportArtifacts;

private Map<String, Artifact> reportArtifactMap;

private Set<Artifact> extensionArtifacts;

private Map<String, Artifact> extensionArtifactMap;

private Map<String, Artifact> managedVersionMap;

private Map<String, MavenProject> projectReferences = new HashMap<String, MavenProject>();

private boolean executionRoot;

private Map<String, String> moduleAdjustments;

private ProjectBuilder mavenProjectBuilder;

private ProjectBuildingRequest projectBuilderConfiguration;

private RepositorySystem repositorySystem;

private File parentFile;

private Map<String, Object> context;

private ClassRealm classRealm;

private DependencyFilter extensionDependencyFilter;

private final Set<String> lifecyclePhases = Collections.synchronizedSet( new LinkedHashSet<String>() );

private Logger logger;

public MavenProject()
{
    Model model = new Model();

    model.setGroupId( EMPTY_PROJECT_GROUP_ID );
    model.setArtifactId( EMPTY_PROJECT_ARTIFACT_ID );
    model.setVersion( EMPTY_PROJECT_VERSION );

    setModel( model );
}

public MavenProject( Model model )
{
    setModel( model );
}

/**
 * @deprecated use {@link #clone()} so subclasses can provide a copy of the same class
 */
@Deprecated
public MavenProject( MavenProject project )
{
    repositorySystem = project.repositorySystem;
    logger = project.logger;
    mavenProjectBuilder = project.mavenProjectBuilder;
    projectBuilderConfiguration = project.projectBuilderConfiguration;
    deepCopy( project );
}

@Deprecated
public MavenProject( Model model, RepositorySystem repositorySystem )
{        
    this.repositorySystem = repositorySystem;
    setModel( model );
}

public File getParentFile()
{
    return parentFile;
}

public void setParentFile( File parentFile )
{
    this.parentFile = parentFile;
}

/**
 * Constructor
 * 
 * @param repositorySystem - may not be null
 * @param mavenProjectBuilder
 * @param projectBuilderConfiguration
 * @throws InvalidRepositoryException
 */
MavenProject( RepositorySystem repositorySystem, ProjectBuilder mavenProjectBuilder,
              ProjectBuildingRequest projectBuilderConfiguration, Logger logger )
{
    if ( repositorySystem == null )
    {
        throw new IllegalArgumentException( "mavenTools: null" );
    }

    this.mavenProjectBuilder = mavenProjectBuilder;
    this.projectBuilderConfiguration = projectBuilderConfiguration;
    this.repositorySystem = repositorySystem;
    this.logger = logger;
}

@Deprecated
public Set<Artifact> createArtifacts( ArtifactFactory artifactFactory, String inheritedScope, ArtifactFilter filter )
    throws InvalidDependencyVersionException
{
    return MavenMetadataSource.createArtifacts( artifactFactory, getDependencies(), inheritedScope, filter, this );
}

// TODO: Find a way to use <relativePath/> here...it's tricky, because the moduleProject
// usually doesn't have a file associated with it yet.
public String getModulePathAdjustment( MavenProject moduleProject )
    throws IOException
{
    // FIXME: This is hacky. What if module directory doesn't match artifactid, and parent
    // is coming from the repository??
    String module = moduleProject.getArtifactId();

    File moduleFile = moduleProject.getFile();

    if ( moduleFile != null )
    {
        File moduleDir = moduleFile.getCanonicalFile().getParentFile();

        module = moduleDir.getName();
    }

    if ( moduleAdjustments == null )
    {
        moduleAdjustments = new HashMap<String, String>();

        List<String> modules = getModules();
        if ( modules != null )
        {
            for ( Iterator<String> it = modules.iterator(); it.hasNext(); )
            {
                String modulePath = it.next();
                String moduleName = modulePath;

                if ( moduleName.endsWith( "/" ) || moduleName.endsWith( "\\" ) )
                {
                    moduleName = moduleName.substring( 0, moduleName.length() - 1 );
                }

                int lastSlash = moduleName.lastIndexOf( '/' );

                if ( lastSlash < 0 )
                {
                    lastSlash = moduleName.lastIndexOf( '\\' );
                }

                String adjustment = null;

                if ( lastSlash > -1 )
                {
                    moduleName = moduleName.substring( lastSlash + 1 );
                    adjustment = modulePath.substring( 0, lastSlash );
                }

                moduleAdjustments.put( moduleName, adjustment );
            }
        }
    }

    return moduleAdjustments.get( module );
}

// ----------------------------------------------------------------------
// Accessors
// ----------------------------------------------------------------------

public Artifact getArtifact()
{
    return artifact;
}

public void setArtifact( Artifact artifact )
{
    this.artifact = artifact;
}

//@todo I would like to get rid of this. jvz.
public Model getModel()
{
    return model;
}

public MavenProject getParent()
{
    if ( parent == null )
    {
        /*
         * TODO: This is suboptimal. Without a cache in the project builder, rebuilding the parent chain currently
         * causes O(n^2) parser invocations for an inheritance hierarchy of depth n.
         */
        if ( parentFile != null )
        {
            checkProjectBuildingRequest();
            ProjectBuildingRequest request = new DefaultProjectBuildingRequest( projectBuilderConfiguration );
            request.setRemoteRepositories( getRemoteArtifactRepositories() );

            try
            {
                parent = mavenProjectBuilder.build( parentFile, request ).getProject();
            }
            catch ( ProjectBuildingException e )
            {
                throw new IllegalStateException( "Failed to build parent project for " + getId(), e );
            }
        }
        else if ( model.getParent() != null )
        {
            checkProjectBuildingRequest();
            ProjectBuildingRequest request = new DefaultProjectBuildingRequest( projectBuilderConfiguration );
            request.setRemoteRepositories( getRemoteArtifactRepositories() );

            try
            {
                parent = mavenProjectBuilder.build( getParentArtifact(), request ).getProject();
            }
            catch ( ProjectBuildingException e )
            {
                throw new IllegalStateException( "Failed to build parent project for " + getId(), e );
            }
        }
    }
    return parent;
}

public void setParent( MavenProject parent )
{
    this.parent = parent;
}

public boolean hasParent()
{
    return getParent() != null;
}

public File getFile()
{
    return file;
}

public void setFile( File file )
{
    this.file = file;
}

public File getBasedir()
{
    if ( getFile() != null )
    {
        return getFile().getParentFile();
    }
    else
    {
        // repository based POM
        return null;
    }
}

public void setDependencies( List<Dependency> dependencies )
{
    getModel().setDependencies( dependencies );
}

public List<Dependency> getDependencies()
{
    return getModel().getDependencies();
}

public DependencyManagement getDependencyManagement()
{
    return getModel().getDependencyManagement();
}

// ----------------------------------------------------------------------
// Test and compile sourceroots.
// ----------------------------------------------------------------------

private void addPath( List<String> paths, String path )
{
    if ( path != null )
    {
        path = path.trim();
        if ( path.length() > 0 )
        {
            File file = new File( path );
            if ( file.isAbsolute() )
            {
                path = file.getAbsolutePath();
            }
            else
            {
                path = new File( getBasedir(), path ).getAbsolutePath();
            }

            if ( !paths.contains( path ) )
            {
                paths.add( path );
            }
        }
    }
}

public void addCompileSourceRoot( String path )
{
    addPath( getCompileSourceRoots(), path );
}

public void addScriptSourceRoot( String path )
{
    if ( path != null )
    {
        path = path.trim();
        if ( path.length() != 0 )
        {
            if ( !getScriptSourceRoots().contains( path ) )
            {
                getScriptSourceRoots().add( path );
            }
        }
    }
}

public void addTestCompileSourceRoot( String path )
{
    addPath( getTestCompileSourceRoots(), path );
}

public List<String> getCompileSourceRoots()
{
    return compileSourceRoots;
}

public List<String> getScriptSourceRoots()
{
    return scriptSourceRoots;
}

public List<String> getTestCompileSourceRoots()
{
    return testCompileSourceRoots;
}

public List<String> getCompileClasspathElements()
    throws DependencyResolutionRequiredException
{
    List<String> list = new ArrayList<String>( getArtifacts().size() + 1 );

    list.add( getBuild().getOutputDirectory() );

    for ( Artifact a : getArtifacts() )
    {                        
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {
            // TODO: let the scope handler deal with this
            if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) || Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
            {
                addArtifactPath( a, list );
            }
        }
    }

    return list;
}

@Deprecated
public List<Artifact> getCompileArtifacts()
{
    List<Artifact> list = new ArrayList<Artifact>( getArtifacts().size() );

    for ( Artifact a : getArtifacts() )
    {
        // TODO: classpath check doesn't belong here - that's the other method
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {
            // TODO: let the scope handler deal with this
            if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) || Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
            {
                list.add( a );
            }
        }
    }
    return list;
}

@Deprecated
public List<Dependency> getCompileDependencies()
{
    Set<Artifact> artifacts = getArtifacts();

    if ( ( artifacts == null ) || artifacts.isEmpty() )
    {
        return Collections.emptyList();
    }

    List<Dependency> list = new ArrayList<Dependency>( artifacts.size() );

    for ( Artifact a : getArtifacts()  )
    {
        // TODO: let the scope handler deal with this
        if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) || Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
        {
            Dependency dependency = new Dependency();

            dependency.setArtifactId( a.getArtifactId() );
            dependency.setGroupId( a.getGroupId() );
            dependency.setVersion( a.getVersion() );
            dependency.setScope( a.getScope() );
            dependency.setType( a.getType() );
            dependency.setClassifier( a.getClassifier() );

            list.add( dependency );
        }
    }
    return list;
}

//TODO: this checking for file == null happens because the resolver has been confused about the root
// artifact or not. things like the stupid dummy artifact coming from surefire.
public List<String> getTestClasspathElements()
    throws DependencyResolutionRequiredException
{
    List<String> list = new ArrayList<String>( getArtifacts().size() + 2 );

    list.add( getBuild().getTestOutputDirectory() );

    list.add( getBuild().getOutputDirectory() );

    for ( Artifact a : getArtifacts() )
    {            
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {                
            addArtifactPath( a, list );
        }
    }

    return list;
}

@Deprecated
public List<Artifact> getTestArtifacts()
{
    List<Artifact> list = new ArrayList<Artifact>( getArtifacts().size() );

    for ( Artifact a : getArtifacts() )
    {
        // TODO: classpath check doesn't belong here - that's the other method
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {
            list.add( a );
        }
    }
    return list;
}

@Deprecated
public List<Dependency> getTestDependencies()
{
    Set<Artifact> artifacts = getArtifacts();

    if ( ( artifacts == null ) || artifacts.isEmpty() )
    {
        return Collections.emptyList();
    }

    List<Dependency> list = new ArrayList<Dependency>( artifacts.size() );

    for ( Artifact a : getArtifacts()  )
    {
        Dependency dependency = new Dependency();

        dependency.setArtifactId( a.getArtifactId() );
        dependency.setGroupId( a.getGroupId() );
        dependency.setVersion( a.getVersion() );
        dependency.setScope( a.getScope() );
        dependency.setType( a.getType() );
        dependency.setClassifier( a.getClassifier() );

        list.add( dependency );
    }
    return list;
}

public List<String> getRuntimeClasspathElements()
    throws DependencyResolutionRequiredException
{
    List<String> list = new ArrayList<String>( getArtifacts().size() + 1 );

    list.add( getBuild().getOutputDirectory() );

    for ( Artifact a : getArtifacts() )
    {
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {
            // TODO: let the scope handler deal with this
            if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
            {
                addArtifactPath( a, list );
            }
        }
    }
    return list;
}

@Deprecated
public List<Artifact> getRuntimeArtifacts()
{
    List<Artifact> list = new ArrayList<Artifact>( getArtifacts().size() );

    for ( Artifact a : getArtifacts()  )
    {
        // TODO: classpath check doesn't belong here - that's the other method
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {
            // TODO: let the scope handler deal with this
            if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
            {
                list.add( a );
            }
        }
    }
    return list;
}

@Deprecated
public List<Dependency> getRuntimeDependencies()
{
    Set<Artifact> artifacts = getArtifacts();

    if ( ( artifacts == null ) || artifacts.isEmpty() )
    {
        return Collections.emptyList();
    }

    List<Dependency> list = new ArrayList<Dependency>( artifacts.size() );

    for ( Artifact a : getArtifacts()  )
    {
        // TODO: let the scope handler deal with this
        if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
        {
            Dependency dependency = new Dependency();

            dependency.setArtifactId( a.getArtifactId() );
            dependency.setGroupId( a.getGroupId() );
            dependency.setVersion( a.getVersion() );
            dependency.setScope( a.getScope() );
            dependency.setType( a.getType() );
            dependency.setClassifier( a.getClassifier() );

            list.add( dependency );
        }
    }
    return list;
}

public List<String> getSystemClasspathElements()
    throws DependencyResolutionRequiredException
{
    List<String> list = new ArrayList<String>( getArtifacts().size() );

    list.add( getBuild().getOutputDirectory() );

    for ( Artifact a : getArtifacts() )
    {
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {
            // TODO: let the scope handler deal with this
            if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
            {
                addArtifactPath( a, list );
            }
        }
    }
    return list;
}

@Deprecated
public List<Artifact> getSystemArtifacts()
{
    List<Artifact> list = new ArrayList<Artifact>( getArtifacts().size() );

    for ( Artifact a : getArtifacts()  )
    {
        // TODO: classpath check doesn't belong here - that's the other method
        if ( a.getArtifactHandler().isAddedToClasspath() )
        {
            // TODO: let the scope handler deal with this
            if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
            {
                list.add( a );
            }
        }
    }
    return list;
}

@Deprecated
public List<Dependency> getSystemDependencies()
{
    Set<Artifact> artifacts = getArtifacts();

    if ( ( artifacts == null ) || artifacts.isEmpty() )
    {
        return Collections.emptyList();
    }

    List<Dependency> list = new ArrayList<Dependency>( artifacts.size() );

    for ( Artifact a : getArtifacts()  )
    {
        // TODO: let the scope handler deal with this
        if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
        {
            Dependency dependency = new Dependency();

            dependency.setArtifactId( a.getArtifactId() );
            dependency.setGroupId( a.getGroupId() );
            dependency.setVersion( a.getVersion() );
            dependency.setScope( a.getScope() );
            dependency.setType( a.getType() );
            dependency.setClassifier( a.getClassifier() );

            list.add( dependency );
        }
    }
    return list;
}

// ----------------------------------------------------------------------
// Delegate to the model
// ----------------------------------------------------------------------

public void setModelVersion( String pomVersion )
{
    getModel().setModelVersion( pomVersion );
}

public String getModelVersion()
{
    return getModel().getModelVersion();
}

public String getId()
{
    return getModel().getId();
}

public void setGroupId( String groupId )
{
    getModel().setGroupId( groupId );
}

public String getGroupId()
{
    String groupId = getModel().getGroupId();

    if ( ( groupId == null ) && ( getModel().getParent() != null ) )
    {
        groupId = getModel().getParent().getGroupId();
    }

    return groupId;
}

public void setArtifactId( String artifactId )
{
    getModel().setArtifactId( artifactId );
}

public String getArtifactId()
{
    return getModel().getArtifactId();
}

public void setName( String name )
{
    getModel().setName( name );
}

public String getName()
{
    // TODO: this should not be allowed to be null.
    if ( getModel().getName() != null )
    {
        return getModel().getName();
    }
    else
    {
        return getArtifactId();
    }
}

public void setVersion( String version )
{
    getModel().setVersion( version );
}

public String getVersion()
{
    String version = getModel().getVersion();

    if ( ( version == null ) && ( getModel().getParent() != null ) )
    {
        version = getModel().getParent().getVersion();
    }

    return version;
}

public String getPackaging()
{
    return getModel().getPackaging();
}

public void setPackaging( String packaging )
{
    getModel().setPackaging( packaging );
}

public void setInceptionYear( String inceptionYear )
{
    getModel().setInceptionYear( inceptionYear );
}

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

public void setUrl( String url )
{
    getModel().setUrl( url );
}

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

public Prerequisites getPrerequisites()
{
    return getModel().getPrerequisites();
}

public void setIssueManagement( IssueManagement issueManagement )
{
    getModel().setIssueManagement( issueManagement );
}

public CiManagement getCiManagement()
{
    return getModel().getCiManagement();
}

public void setCiManagement( CiManagement ciManagement )
{
    getModel().setCiManagement( ciManagement );
}

public IssueManagement getIssueManagement()
{
    return getModel().getIssueManagement();
}

public void setDistributionManagement( DistributionManagement distributionManagement )
{
    getModel().setDistributionManagement( distributionManagement );
}

public DistributionManagement getDistributionManagement()
{
    return getModel().getDistributionManagement();
}

public void setDescription( String description )
{
    getModel().setDescription( description );
}

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

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

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


////Some extra code here
} 

0 个答案:

没有答案