[MNG-3400] MavenProject is not extensible

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@627670 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Carlos Sanchez Gonzalez 2008-02-14 06:08:07 +00:00
parent 0fc4704f30
commit 17d401df6c
3 changed files with 172 additions and 126 deletions

View File

@ -29,7 +29,7 @@ public class StartForkedExecutionMojo
if ( project != null )
{
session.addForkedProject( new MavenProject( project ) );
session.addForkedProject( (MavenProject) project.clone() );
}
}

View File

@ -160,103 +160,107 @@ public class MavenProject
model.setArtifactId( EMPTY_PROJECT_ARTIFACT_ID );
model.setVersion( EMPTY_PROJECT_VERSION );
this.model = model;
this.setModel( model );
}
public MavenProject( Model model )
{
this.model = model;
this.setModel( model );
}
/**
* @deprecated use {@link #clone()}
*/
public MavenProject( MavenProject project )
{
// disown the parent
// copy fields
file = project.file;
setFile( project.getFile() );
// don't need a deep copy, they don't get modified or added/removed to/from - but make them unmodifiable to be sure!
if ( project.dependencyArtifacts != null )
// don't need a deep copy, they don't get modified or added/removed to/from - but make them unmodifiable to be
// sure!
if ( project.getDependencyArtifacts() != null )
{
dependencyArtifacts = Collections.unmodifiableSet( project.dependencyArtifacts );
setDependencyArtifacts( Collections.unmodifiableSet( project.getDependencyArtifacts() ) );
}
if ( project.artifacts != null )
if ( project.getArtifacts() != null )
{
artifacts = Collections.unmodifiableSet( project.artifacts );
setArtifacts( Collections.unmodifiableSet( project.getArtifacts() ) );
}
if ( project.pluginArtifacts != null )
if ( project.getPluginArtifacts() != null )
{
pluginArtifacts = Collections.unmodifiableSet( project.pluginArtifacts );
setPluginArtifacts( Collections.unmodifiableSet( project.getPluginArtifacts() ) );
}
if ( project.reportArtifacts != null )
if ( project.getReportArtifacts() != null )
{
reportArtifacts = Collections.unmodifiableSet( project.reportArtifacts );
setReportArtifacts( Collections.unmodifiableSet( project.getReportArtifacts() ) );
}
if ( project.extensionArtifacts != null )
if ( project.getExtensionArtifacts() != null )
{
extensionArtifacts = Collections.unmodifiableSet( project.extensionArtifacts );
setExtensionArtifacts( Collections.unmodifiableSet( project.getExtensionArtifacts() ) );
}
parentArtifact = project.parentArtifact;
setParentArtifact( ( project.getParentArtifact() ) );
if ( project.remoteArtifactRepositories != null )
if ( project.getRemoteArtifactRepositories() != null )
{
remoteArtifactRepositories = Collections.unmodifiableList( project.remoteArtifactRepositories );
setRemoteArtifactRepositories( Collections.unmodifiableList( project.getRemoteArtifactRepositories() ) );
}
if ( project.pluginArtifactRepositories != null )
if ( project.getPluginArtifactRepositories() != null )
{
pluginArtifactRepositories = Collections.unmodifiableList( project.pluginArtifactRepositories );
setPluginArtifactRepositories( ( Collections.unmodifiableList( project.getPluginArtifactRepositories() ) ) );
}
if ( project.collectedProjects != null )
if ( project.getCollectedProjects() != null )
{
collectedProjects = Collections.unmodifiableList( project.collectedProjects );
setCollectedProjects( ( Collections.unmodifiableList( project.getCollectedProjects() ) ) );
}
if ( project.activeProfiles != null )
if ( project.getActiveProfiles() != null )
{
activeProfiles = Collections.unmodifiableList( project.activeProfiles );
setActiveProfiles( ( Collections.unmodifiableList( project.getActiveProfiles() ) ) );
}
if ( project.getAttachedArtifacts() != null )
{
// clone properties modifyable by plugins in a forked lifecycle
attachedArtifacts = new ArrayList( project.getAttachedArtifacts() );
setAttachedArtifacts( new ArrayList( project.getAttachedArtifacts() ) );
}
if ( project.compileSourceRoots != null )
if ( project.getCompileSourceRoots() != null )
{
// clone source roots
compileSourceRoots = new ArrayList( project.compileSourceRoots );
setCompileSourceRoots( ( new ArrayList( project.getCompileSourceRoots() ) ) );
}
if ( project.testCompileSourceRoots != null )
if ( project.getTestCompileSourceRoots() != null )
{
testCompileSourceRoots = new ArrayList( project.testCompileSourceRoots );
setTestCompileSourceRoots( ( new ArrayList( project.getTestCompileSourceRoots() ) ) );
}
if ( project.scriptSourceRoots != null )
if ( project.getScriptSourceRoots() != null )
{
scriptSourceRoots = new ArrayList( project.scriptSourceRoots );
setScriptSourceRoots( ( new ArrayList( project.getScriptSourceRoots() ) ) );
}
model = ModelUtils.cloneModel( project.model );
setModel( ( ModelUtils.cloneModel( project.getModel() ) ) );
if ( project.originalModel != null )
if ( project.getOriginalModel() != null )
{
originalModel = ModelUtils.cloneModel( project.originalModel );
setOriginalModel( ( ModelUtils.cloneModel( project.getOriginalModel() ) ) );
}
executionRoot = project.executionRoot;
setExecutionRoot( project.isExecutionRoot() );
if ( project.artifact != null )
if ( project.getArtifact() != null )
{
artifact = ArtifactUtils.copyArtifact( project.artifact );
setArtifact( ArtifactUtils.copyArtifact( project.getArtifact() ) );
}
if ( project.getManagedVersionMap() != null )
@ -264,14 +268,14 @@ public class MavenProject
setManagedVersionMap( new ManagedVersionMap( project.getManagedVersionMap() ) );
}
if ( project.releaseArtifactRepository != null )
if ( project.getReleaseArtifactRepository() != null )
{
releaseArtifactRepository = project.releaseArtifactRepository;
setReleaseArtifactRepository( project.getReleaseArtifactRepository() );
}
if ( project.snapshotArtifactRepository != null )
if ( project.getSnapshotArtifactRepository() != null )
{
snapshotArtifactRepository = project.snapshotArtifactRepository;
setSnapshotArtifactRepository( project.getSnapshotArtifactRepository() );
}
}
@ -402,17 +406,17 @@ public class MavenProject
public void setDependencies( List dependencies )
{
model.setDependencies( dependencies );
getModel().setDependencies( dependencies );
}
public List getDependencies()
{
return model.getDependencies();
return getModel().getDependencies();
}
public DependencyManagement getDependencyManagement()
{
return model.getDependencyManagement();
return getModel().getDependencyManagement();
}
// ----------------------------------------------------------------------
@ -426,9 +430,9 @@ public class MavenProject
path = path.trim();
if ( path.length() != 0 )
{
if ( !compileSourceRoots.contains( path ) )
if ( !getCompileSourceRoots().contains( path ) )
{
compileSourceRoots.add( path );
getCompileSourceRoots().add( path );
}
}
}
@ -441,9 +445,9 @@ public class MavenProject
path = path.trim();
if ( path.length() != 0 )
{
if ( !scriptSourceRoots.contains( path ) )
if ( !getScriptSourceRoots().contains( path ) )
{
scriptSourceRoots.add( path );
getScriptSourceRoots().add( path );
}
}
}
@ -456,9 +460,9 @@ public class MavenProject
path = path.trim();
if ( path.length() != 0 )
{
if ( !testCompileSourceRoots.contains( path ) )
if ( !getTestCompileSourceRoots().contains( path ) )
{
testCompileSourceRoots.add( path );
getTestCompileSourceRoots().add( path );
}
}
}
@ -820,31 +824,31 @@ public class MavenProject
public void setModelVersion( String pomVersion )
{
model.setModelVersion( pomVersion );
getModel().setModelVersion( pomVersion );
}
public String getModelVersion()
{
return model.getModelVersion();
return getModel().getModelVersion();
}
public String getId()
{
return model.getId();
return getModel().getId();
}
public void setGroupId( String groupId )
{
model.setGroupId( groupId );
getModel().setGroupId( groupId );
}
public String getGroupId()
{
String groupId = model.getGroupId();
String groupId = getModel().getGroupId();
if ( ( groupId == null ) && ( model.getParent() != null ) )
if ( ( groupId == null ) && ( getModel().getParent() != null ) )
{
groupId = model.getParent().getGroupId();
groupId = getModel().getParent().getGroupId();
}
return groupId;
@ -852,25 +856,25 @@ public class MavenProject
public void setArtifactId( String artifactId )
{
model.setArtifactId( artifactId );
getModel().setArtifactId( artifactId );
}
public String getArtifactId()
{
return model.getArtifactId();
return getModel().getArtifactId();
}
public void setName( String name )
{
model.setName( name );
getModel().setName( name );
}
public String getName()
{
// TODO: this should not be allowed to be null.
if ( model.getName() != null )
if ( getModel().getName() != null )
{
return model.getName();
return getModel().getName();
}
else
{
@ -880,16 +884,16 @@ public class MavenProject
public void setVersion( String version )
{
model.setVersion( version );
getModel().setVersion( version );
}
public String getVersion()
{
String version = model.getVersion();
String version = getModel().getVersion();
if ( ( version == null ) && ( model.getParent() != null ) )
if ( ( version == null ) && ( getModel().getParent() != null ) )
{
version = model.getParent().getVersion();
version = getModel().getParent().getVersion();
}
return version;
@ -897,147 +901,147 @@ public class MavenProject
public String getPackaging()
{
return model.getPackaging();
return getModel().getPackaging();
}
public void setPackaging( String packaging )
{
model.setPackaging( packaging );
getModel().setPackaging( packaging );
}
public void setInceptionYear( String inceptionYear )
{
model.setInceptionYear( inceptionYear );
getModel().setInceptionYear( inceptionYear );
}
public String getInceptionYear()
{
return model.getInceptionYear();
return getModel().getInceptionYear();
}
public void setUrl( String url )
{
model.setUrl( url );
getModel().setUrl( url );
}
public String getUrl()
{
return model.getUrl();
return getModel().getUrl();
}
public Prerequisites getPrerequisites()
{
return model.getPrerequisites();
return getModel().getPrerequisites();
}
public void setIssueManagement( IssueManagement issueManagement )
{
model.setIssueManagement( issueManagement );
getModel().setIssueManagement( issueManagement );
}
public CiManagement getCiManagement()
{
return model.getCiManagement();
return getModel().getCiManagement();
}
public void setCiManagement( CiManagement ciManagement )
{
model.setCiManagement( ciManagement );
getModel().setCiManagement( ciManagement );
}
public IssueManagement getIssueManagement()
{
return model.getIssueManagement();
return getModel().getIssueManagement();
}
public void setDistributionManagement( DistributionManagement distributionManagement )
{
model.setDistributionManagement( distributionManagement );
getModel().setDistributionManagement( distributionManagement );
}
public DistributionManagement getDistributionManagement()
{
return model.getDistributionManagement();
return getModel().getDistributionManagement();
}
public void setDescription( String description )
{
model.setDescription( description );
getModel().setDescription( description );
}
public String getDescription()
{
return model.getDescription();
return getModel().getDescription();
}
public void setOrganization( Organization organization )
{
model.setOrganization( organization );
getModel().setOrganization( organization );
}
public Organization getOrganization()
{
return model.getOrganization();
return getModel().getOrganization();
}
public void setScm( Scm scm )
{
model.setScm( scm );
getModel().setScm( scm );
}
public Scm getScm()
{
return model.getScm();
return getModel().getScm();
}
public void setMailingLists( List mailingLists )
{
model.setMailingLists( mailingLists );
getModel().setMailingLists( mailingLists );
}
public List getMailingLists()
{
return model.getMailingLists();
return getModel().getMailingLists();
}
public void addMailingList( MailingList mailingList )
{
model.addMailingList( mailingList );
getModel().addMailingList( mailingList );
}
public void setDevelopers( List developers )
{
model.setDevelopers( developers );
getModel().setDevelopers( developers );
}
public List getDevelopers()
{
return model.getDevelopers();
return getModel().getDevelopers();
}
public void addDeveloper( Developer developer )
{
model.addDeveloper( developer );
getModel().addDeveloper( developer );
}
public void setContributors( List contributors )
{
model.setContributors( contributors );
getModel().setContributors( contributors );
}
public List getContributors()
{
return model.getContributors();
return getModel().getContributors();
}
public void addContributor( Contributor contributor )
{
model.addContributor( contributor );
getModel().addContributor( contributor );
}
public void setBuild( Build build )
{
model.setBuild( build );
getModel().setBuild( build );
}
public Build getBuild()
@ -1067,27 +1071,27 @@ public class MavenProject
public void setReporting( Reporting reporting )
{
model.setReporting( reporting );
getModel().setReporting( reporting );
}
public Reporting getReporting()
{
return model.getReporting();
return getModel().getReporting();
}
public void setLicenses( List licenses )
{
model.setLicenses( licenses );
getModel().setLicenses( licenses );
}
public List getLicenses()
{
return model.getLicenses();
return getModel().getLicenses();
}
public void addLicense( License license )
{
model.addLicense( license );
getModel().addLicense( license );
}
public void setArtifacts( Set artifacts )
@ -1198,7 +1202,7 @@ public class MavenProject
public List getRepositories()
{
return model.getRepositories();
return getModel().getRepositories();
}
// ----------------------------------------------------------------------
@ -1207,33 +1211,33 @@ public class MavenProject
public List getReportPlugins()
{
if ( model.getReporting() == null )
if ( getModel().getReporting() == null )
{
return null;
}
return model.getReporting().getPlugins();
return getModel().getReporting().getPlugins();
}
public List getBuildPlugins()
{
if ( model.getBuild() == null )
if ( getModel().getBuild() == null )
{
return null;
}
return model.getBuild().getPlugins();
return getModel().getBuild().getPlugins();
}
public List getModules()
{
return model.getModules();
return getModel().getModules();
}
public PluginManagement getPluginManagement()
{
PluginManagement pluginMgmt = null;
Build build = model.getBuild();
Build build = getModel().getBuild();
if ( build != null )
{
pluginMgmt = build.getPluginManagement();
@ -1244,13 +1248,13 @@ public class MavenProject
private Build getModelBuild()
{
Build build = model.getBuild();
Build build = getModel().getBuild();
if ( build == null )
{
build = new Build();
model.setBuild( build );
getModel().setBuild( build );
}
return build;
@ -1315,14 +1319,14 @@ public class MavenProject
public ArtifactRepository getDistributionManagementArtifactRepository()
{
return getArtifact().isSnapshot() && ( snapshotArtifactRepository != null ) ? snapshotArtifactRepository
: releaseArtifactRepository;
return getArtifact().isSnapshot() && ( getSnapshotArtifactRepository() != null ) ? getSnapshotArtifactRepository()
: getReleaseArtifactRepository();
}
public List getPluginRepositories()
{
// return model.getPluginRepositories();
return model.getRepositories();
return getModel().getRepositories();
}
public void setActiveProfiles( List activeProfiles )
@ -1623,6 +1627,42 @@ public class MavenProject
return getBuild() != null ? getBuild().getDefaultGoal() : null;
}
protected void setModel( Model model )
{
this.model = model;
}
protected void setAttachedArtifacts( List attachedArtifacts )
{
this.attachedArtifacts = attachedArtifacts;
}
protected void setCompileSourceRoots( List compileSourceRoots )
{
this.compileSourceRoots = compileSourceRoots;
}
protected void setTestCompileSourceRoots( List testCompileSourceRoots )
{
this.testCompileSourceRoots = testCompileSourceRoots;
}
protected void setScriptSourceRoots( List scriptSourceRoots )
{
this.scriptSourceRoots = scriptSourceRoots;
}
protected ArtifactRepository getReleaseArtifactRepository()
{
return releaseArtifactRepository;
}
protected ArtifactRepository getSnapshotArtifactRepository()
{
return snapshotArtifactRepository;
}
public Artifact replaceWithActiveArtifact( Artifact pluginArtifact )
{
if ( ( getProjectReferences() != null ) && !getProjectReferences().isEmpty() )
@ -1784,4 +1824,12 @@ public class MavenProject
return sb.toString();
}
/**
* @since 2.0.9
*/
public Object clone()
{
return new MavenProject( this );
}
}

View File

@ -19,17 +19,15 @@ package org.apache.maven.project;
* under the License.
*/
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Dependency;
import org.apache.maven.artifact.versioning.ManagedVersionMap;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.versioning.ManagedVersionMap;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
public class MavenProjectTest
extends AbstractMavenProjectTestCase
@ -90,20 +88,20 @@ public class MavenProjectTest
+ MavenProject.EMPTY_PROJECT_VERSION, project.getId() );
}
public void testCopyConstructor()
public void testClone()
throws Exception
{
File f = getFileForClasspathResource( "canonical-pom.xml" );
MavenProject projectToClone = getProject( f );
MavenProject clonedProject = new MavenProject( projectToClone );
MavenProject clonedProject = (MavenProject) projectToClone.clone();
assertEquals( "maven-core", clonedProject.getArtifactId() );
Map clonedMap = clonedProject.getManagedVersionMap();
assertNotNull( "ManagedVersionMap not copied", clonedMap );
assertTrue( "ManagedVersionMap is not empty", clonedMap.isEmpty() );
}
public void testCopyConstructorWithDependencyManagement()
public void testCloneWithDependencyManagement()
throws Exception
{
File f = getFileForClasspathResource( "dependencyManagement-pom.xml" );
@ -118,7 +116,7 @@ public class MavenProjectTest
assertNotNull( "No ManagedVersionMap", map );
assertTrue( "ManagedVersionMap is empty", !map.isEmpty() );
MavenProject clonedProject = new MavenProject( projectToClone );
MavenProject clonedProject = (MavenProject) projectToClone.clone();
assertEquals( "maven-core", clonedProject.getArtifactId() );
Map clonedMap = clonedProject.getManagedVersionMap();
assertNotNull( "ManagedVersionMap not copied", clonedMap );
@ -147,13 +145,13 @@ public class MavenProjectTest
assertEquals( "..", pathAdjustment );
}
public void testCopyConstructorWithDistributionManagement() throws Exception
public void testCloneWithDistributionManagement() throws Exception
{
File f = getFileForClasspathResource( "distributionManagement-pom.xml" );
MavenProject projectToClone = getProject( f );
MavenProject clonedProject = new MavenProject( projectToClone );
MavenProject clonedProject = (MavenProject) projectToClone.clone();
assertNotNull( "clonedProject - distributionManagement", clonedProject.getDistributionManagementArtifactRepository() );
}
}