mirror of https://github.com/apache/maven.git
Splitting model-lineage loading and profile-application out into separate components...profile-application, so the model-lineage builder can run properly, and the model-lineage builder, so we can pre-scan for models with extensions that may contain things like profile activators, which would be used during project-loading.
git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@496910 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b61fba1d5f
commit
c7738df560
|
@ -73,5 +73,10 @@
|
|||
<groupId>org.codehaus.plexus</groupId>
|
||||
<artifactId>plexus-container-default</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.wagon</groupId>
|
||||
<artifactId>wagon-file</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -246,7 +246,7 @@ private boolean isActive( Profile profile )
|
|||
throws ProfileActivationException
|
||||
{
|
||||
List activators = null;
|
||||
container.addContextValue("SystemProperties", systemProperties);
|
||||
|
||||
try
|
||||
{
|
||||
activators = container.lookupList( ProfileActivator.ROLE );
|
||||
|
|
|
@ -6,6 +6,9 @@
|
|||
import org.apache.maven.model.Profile;
|
||||
import org.codehaus.plexus.context.Context;
|
||||
import org.codehaus.plexus.context.ContextException;
|
||||
import org.codehaus.plexus.logging.LogEnabled;
|
||||
import org.codehaus.plexus.logging.Logger;
|
||||
import org.codehaus.plexus.logging.console.ConsoleLogger;
|
||||
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
|
||||
|
@ -26,13 +29,14 @@
|
|||
*/
|
||||
|
||||
public class SystemPropertyProfileActivator
|
||||
extends DetectedProfileActivator implements Contextualizable
|
||||
extends DetectedProfileActivator implements Contextualizable, LogEnabled
|
||||
{
|
||||
private Properties properties;
|
||||
private Context context;
|
||||
private Logger logger;
|
||||
|
||||
public void contextualize(Context context) throws ContextException
|
||||
{
|
||||
properties = (Properties)context.get("SystemProperties");
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
protected boolean canDetectActivation( Profile profile )
|
||||
|
@ -42,6 +46,24 @@ protected boolean canDetectActivation( Profile profile )
|
|||
|
||||
public boolean isActive( Profile profile )
|
||||
{
|
||||
Properties properties = null;
|
||||
if ( context.contains( "SystemProperties" ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
properties = (Properties) context.get("SystemProperties");
|
||||
}
|
||||
catch ( ContextException e )
|
||||
{
|
||||
getLogger().debug( "Failed to get system properties cache from context.", e );
|
||||
}
|
||||
}
|
||||
|
||||
if ( properties == null )
|
||||
{
|
||||
properties = System.getProperties();
|
||||
}
|
||||
|
||||
Activation activation = profile.getActivation();
|
||||
|
||||
ActivationProperty property = activation.getProperty();
|
||||
|
@ -57,7 +79,7 @@ public boolean isActive( Profile profile )
|
|||
name = name.substring( 1 );
|
||||
}
|
||||
|
||||
String sysValue = properties.getProperty( name );
|
||||
String sysValue = properties != null ? properties.getProperty( name ) : null;
|
||||
|
||||
String propValue = property.getValue();
|
||||
if ( StringUtils.isNotEmpty( propValue ) )
|
||||
|
@ -98,5 +120,20 @@ public boolean isActive( Profile profile )
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected Logger getLogger()
|
||||
{
|
||||
if ( logger == null )
|
||||
{
|
||||
logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "SystemPropertyProfileActivator:internal" );
|
||||
}
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
public void enableLogging( Logger logger )
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.apache.maven.MavenTools;
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.ArtifactStatus;
|
||||
import org.apache.maven.artifact.ArtifactUtils;
|
||||
|
@ -38,11 +39,9 @@
|
|||
import org.apache.maven.model.DistributionManagement;
|
||||
import org.apache.maven.model.Extension;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.Parent;
|
||||
import org.apache.maven.model.Plugin;
|
||||
import org.apache.maven.model.Profile;
|
||||
import org.apache.maven.model.ReportPlugin;
|
||||
import org.apache.maven.model.Repository;
|
||||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
||||
import org.apache.maven.profiles.DefaultProfileManager;
|
||||
import org.apache.maven.profiles.MavenProfilesBuilder;
|
||||
|
@ -51,6 +50,10 @@
|
|||
import org.apache.maven.profiles.ProfilesRoot;
|
||||
import org.apache.maven.profiles.activation.ProfileActivationException;
|
||||
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
|
||||
import org.apache.maven.project.build.model.DefaultModelLineage;
|
||||
import org.apache.maven.project.build.model.ModelLineage;
|
||||
import org.apache.maven.project.build.model.ModelLineageBuilder;
|
||||
import org.apache.maven.project.build.profile.ProfileAdvisor;
|
||||
import org.apache.maven.project.inheritance.ModelInheritanceAssembler;
|
||||
import org.apache.maven.project.injection.ModelDefaultsInjector;
|
||||
import org.apache.maven.project.injection.ProfileInjector;
|
||||
|
@ -60,7 +63,6 @@
|
|||
import org.apache.maven.project.validation.ModelValidationResult;
|
||||
import org.apache.maven.project.validation.ModelValidator;
|
||||
import org.apache.maven.wagon.events.TransferListener;
|
||||
import org.apache.maven.MavenTools;
|
||||
import org.codehaus.plexus.PlexusConstants;
|
||||
import org.codehaus.plexus.PlexusContainer;
|
||||
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
|
||||
|
@ -166,6 +168,10 @@ public class DefaultMavenProjectBuilder
|
|||
private ModelInterpolator modelInterpolator;
|
||||
|
||||
private ArtifactRepositoryFactory artifactRepositoryFactory;
|
||||
|
||||
private ModelLineageBuilder modelLineageBuilder;
|
||||
|
||||
private ProfileAdvisor profileAdvisor;
|
||||
|
||||
private MavenTools mavenTools;
|
||||
|
||||
|
@ -420,6 +426,12 @@ private MavenProject buildFromSourceFileInternal( File projectDescriptor,
|
|||
boolean checkDistributionManagementStatus )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
// TODO: Remove this once we have build-context stuff working...
|
||||
if ( !container.getContext().contains( "SystemProperties" ) )
|
||||
{
|
||||
container.addContextValue("SystemProperties", System.getProperties());
|
||||
}
|
||||
|
||||
Model model = readModel( "unknown", projectDescriptor, STRICT_MODEL_PARSING );
|
||||
|
||||
MavenProject project = buildInternal( projectDescriptor.getAbsolutePath(),
|
||||
|
@ -613,76 +625,44 @@ private MavenProject buildInternal( String pomLocation,
|
|||
|
||||
Model superModel = getSuperModel();
|
||||
|
||||
//TODO mkleint - use the (Container, Properties) constructor to make system properties embeddable
|
||||
// shall the ProfileManager intefrace expose the properties?
|
||||
|
||||
ProfileManager superProjectProfileManager;
|
||||
if (externalProfileManager instanceof DefaultProfileManager) {
|
||||
superProjectProfileManager = new DefaultProfileManager( container, ((DefaultProfileManager) externalProfileManager).getSystemProperties() );
|
||||
} else {
|
||||
superProjectProfileManager = new DefaultProfileManager( container );
|
||||
}
|
||||
|
||||
List activeProfiles;
|
||||
|
||||
superProjectProfileManager.addProfiles( superModel.getProfiles() );
|
||||
|
||||
activeProfiles = injectActiveProfiles( superProjectProfileManager, superModel );
|
||||
|
||||
MavenProject superProject = new MavenProject( superModel );
|
||||
|
||||
String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
|
||||
|
||||
superProject.setActiveProfiles( activeProfiles );
|
||||
List explicitlyActive;
|
||||
List explicitlyInactive;
|
||||
|
||||
if ( externalProfileManager != null )
|
||||
{
|
||||
// used to trigger the caching of SystemProperties in the container context...
|
||||
try
|
||||
{
|
||||
externalProfileManager.getActiveProfiles();
|
||||
}
|
||||
catch ( ProfileActivationException e )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, "Failed to activate external profiles.", e );
|
||||
}
|
||||
|
||||
explicitlyActive = externalProfileManager.getExplicitlyActivatedIds();
|
||||
explicitlyInactive = externalProfileManager.getExplicitlyDeactivatedIds();
|
||||
}
|
||||
else
|
||||
{
|
||||
explicitlyActive = Collections.EMPTY_LIST;
|
||||
explicitlyInactive = Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
superProject.setActiveProfiles( profileAdvisor.applyActivatedProfiles( superModel, null, explicitlyActive, explicitlyInactive ) );
|
||||
|
||||
//noinspection CollectionDeclaredAsConcreteClass
|
||||
LinkedList lineage = new LinkedList();
|
||||
|
||||
// TODO: the aRWR can get out of sync with project.model.repositories. We should do all the processing of
|
||||
// profiles, etc on the models then recreate the aggregated sets at the end from the project repositories (they
|
||||
// must still be created along the way so that parent poms can be discovered, however)
|
||||
// Use a TreeSet to ensure ordering is retained
|
||||
Set aggregatedRemoteWagonRepositories = new LinkedHashSet();
|
||||
|
||||
String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
|
||||
|
||||
List activeExternalProfiles;
|
||||
try
|
||||
{
|
||||
if ( externalProfileManager != null )
|
||||
{
|
||||
activeExternalProfiles = externalProfileManager.getActiveProfiles();
|
||||
}
|
||||
else
|
||||
{
|
||||
activeExternalProfiles = Collections.EMPTY_LIST;
|
||||
}
|
||||
}
|
||||
catch ( ProfileActivationException e )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, "Failed to calculate active external profiles.", e );
|
||||
}
|
||||
|
||||
for ( Iterator i = activeExternalProfiles.iterator(); i.hasNext(); )
|
||||
{
|
||||
Profile externalProfile = (Profile) i.next();
|
||||
|
||||
for ( Iterator repoIterator = externalProfile.getRepositories().iterator(); repoIterator.hasNext(); )
|
||||
{
|
||||
Repository mavenRepo = (Repository) repoIterator.next();
|
||||
|
||||
ArtifactRepository artifactRepo = null;
|
||||
try
|
||||
{
|
||||
artifactRepo = mavenTools.buildArtifactRepository( mavenRepo );
|
||||
}
|
||||
catch ( InvalidRepositoryException e )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, e.getMessage(), e );
|
||||
}
|
||||
|
||||
aggregatedRemoteWagonRepositories.add( artifactRepo );
|
||||
}
|
||||
}
|
||||
|
||||
LinkedHashSet aggregatedRemoteWagonRepositories = collectInitialRepositories( model, superModel,
|
||||
parentSearchRepositories,
|
||||
projectDir, explicitlyActive,
|
||||
explicitlyInactive );
|
||||
|
||||
Model originalModel = ModelUtils.cloneModel( model );
|
||||
|
||||
MavenProject project = null;
|
||||
|
@ -801,6 +781,62 @@ private MavenProject buildInternal( String pomLocation,
|
|||
return project;
|
||||
}
|
||||
|
||||
/*
|
||||
* Order is:
|
||||
*
|
||||
* 1. model profile repositories
|
||||
* 2. model repositories
|
||||
* 3. superModel profile repositories
|
||||
* 4. superModel repositories
|
||||
* 5. parentSearchRepositories
|
||||
*/
|
||||
private LinkedHashSet collectInitialRepositories( Model model, Model superModel, List parentSearchRepositories,
|
||||
File projectDir, List explicitlyActive, List explicitlyInactive )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
LinkedHashSet collected = new LinkedHashSet();
|
||||
|
||||
collectInitialRepositoriesFromModel( collected, model, projectDir, explicitlyActive, explicitlyInactive );
|
||||
|
||||
collectInitialRepositoriesFromModel( collected, superModel, projectDir, explicitlyActive, explicitlyInactive );
|
||||
|
||||
if ( parentSearchRepositories != null && !parentSearchRepositories.isEmpty() )
|
||||
{
|
||||
collected.addAll( parentSearchRepositories );
|
||||
}
|
||||
|
||||
return collected;
|
||||
}
|
||||
|
||||
private void collectInitialRepositoriesFromModel( LinkedHashSet collected, Model model, File projectDir,
|
||||
List explicitlyActive, List explicitlyInactive )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
Set reposFromProfiles = profileAdvisor.getArtifactRepositoriesFromActiveProfiles( model, projectDir,
|
||||
explicitlyActive,
|
||||
explicitlyInactive );
|
||||
|
||||
if ( reposFromProfiles != null && !reposFromProfiles.isEmpty() )
|
||||
{
|
||||
collected.addAll( reposFromProfiles );
|
||||
}
|
||||
|
||||
List modelRepos = model.getRepositories();
|
||||
if ( modelRepos != null && !modelRepos.isEmpty() )
|
||||
{
|
||||
try
|
||||
{
|
||||
collected.addAll( mavenTools.buildArtifactRepositories( modelRepos ) );
|
||||
}
|
||||
catch ( InvalidRepositoryException e )
|
||||
{
|
||||
throw new ProjectBuildingException( safeVersionlessKey( model.getGroupId(), model.getArtifactId() ),
|
||||
"Failed to construct ArtifactRepository instances for repositories declared in: "
|
||||
+ model.getId(), e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String safeVersionlessKey( String groupId, String artifactId )
|
||||
{
|
||||
String gid = groupId;
|
||||
|
@ -851,16 +887,29 @@ private MavenProject processProjectLogic( String pomLocation,
|
|||
{
|
||||
Model model = project.getModel();
|
||||
|
||||
List activeProfiles = project.getActiveProfiles();
|
||||
|
||||
if ( activeProfiles == null )
|
||||
List explicitlyActive;
|
||||
List explicitlyInactive;
|
||||
|
||||
if ( profileMgr != null )
|
||||
{
|
||||
activeProfiles = new ArrayList();
|
||||
explicitlyActive = profileMgr.getExplicitlyActivatedIds();
|
||||
explicitlyInactive = profileMgr.getExplicitlyDeactivatedIds();
|
||||
}
|
||||
|
||||
List injectedProfiles = injectActiveProfiles( profileMgr, model );
|
||||
|
||||
activeProfiles.addAll( injectedProfiles );
|
||||
else
|
||||
{
|
||||
explicitlyActive = Collections.EMPTY_LIST;
|
||||
explicitlyInactive = Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List active = profileAdvisor.applyActivatedProfiles( model, projectDir, explicitlyActive, explicitlyInactive );
|
||||
|
||||
LinkedHashSet activated = new LinkedHashSet();
|
||||
|
||||
activated.addAll( project.getActiveProfiles() );
|
||||
activated.addAll( active );
|
||||
|
||||
List activeProfiles = new ArrayList( activated );
|
||||
project.setActiveProfiles( activeProfiles );
|
||||
|
||||
// TODO: Clean this up...we're using this to 'jump' the interpolation step for model properties not expressed in XML.
|
||||
// [BP] - Can this above comment be explained?
|
||||
|
@ -971,251 +1020,69 @@ private MavenProject assembleLineage( Model model,
|
|||
boolean strict )
|
||||
throws ProjectBuildingException, InvalidRepositoryException
|
||||
{
|
||||
if ( !model.getRepositories().isEmpty() )
|
||||
{
|
||||
List respositories = buildArtifactRepositories( model );
|
||||
|
||||
for ( Iterator it = respositories.iterator(); it.hasNext(); )
|
||||
{
|
||||
ArtifactRepository repository = (ArtifactRepository) it.next();
|
||||
|
||||
if ( !aggregatedRemoteWagonRepositories.contains( repository ) )
|
||||
{
|
||||
aggregatedRemoteWagonRepositories.add( repository );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TODO mkleint - use the (Container, Properties constructor to make system properties embeddable
|
||||
ProfileManager profileManager;
|
||||
if (externalProfileManager != null && externalProfileManager instanceof DefaultProfileManager ) {
|
||||
profileManager = new DefaultProfileManager( container, ((DefaultProfileManager)externalProfileManager).getSystemProperties() );
|
||||
} else {
|
||||
profileManager = new DefaultProfileManager( container );
|
||||
}
|
||||
|
||||
ModelLineage modelLineage = new DefaultModelLineage();
|
||||
modelLineage.setOrigin( model, new File( projectDir, "pom.xml" ), new ArrayList( aggregatedRemoteWagonRepositories ) );
|
||||
|
||||
modelLineageBuilder.resumeBuildingModelLineage( modelLineage, localRepository, externalProfileManager );
|
||||
|
||||
List explicitlyActive;
|
||||
List explicitlyInactive;
|
||||
|
||||
if ( externalProfileManager != null )
|
||||
{
|
||||
profileManager.explicitlyActivate( externalProfileManager.getExplicitlyActivatedIds() );
|
||||
|
||||
profileManager.explicitlyDeactivate( externalProfileManager.getExplicitlyDeactivatedIds() );
|
||||
explicitlyActive = externalProfileManager.getExplicitlyActivatedIds();
|
||||
explicitlyInactive = externalProfileManager.getExplicitlyDeactivatedIds();
|
||||
}
|
||||
|
||||
List activeProfiles;
|
||||
|
||||
try
|
||||
else
|
||||
{
|
||||
profileManager.addProfiles( model.getProfiles() );
|
||||
|
||||
loadProjectExternalProfiles( profileManager, projectDir );
|
||||
|
||||
activeProfiles = injectActiveProfiles( profileManager, model );
|
||||
explicitlyActive = Collections.EMPTY_LIST;
|
||||
explicitlyInactive = Collections.EMPTY_LIST;
|
||||
}
|
||||
catch ( ProfileActivationException e )
|
||||
|
||||
List models = modelLineage.getModelsInDescendingOrder();
|
||||
List poms = modelLineage.getFilesInDescendingOrder();
|
||||
|
||||
MavenProject lastProject = null;
|
||||
for( int i = 0; i < models.size(); i++ )
|
||||
{
|
||||
String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
|
||||
|
||||
throw new ProjectBuildingException( projectId, "Failed to activate local (project-level) build profiles: " + e.getMessage(), e );
|
||||
}
|
||||
|
||||
MavenProject project = new MavenProject( model );
|
||||
|
||||
project.setActiveProfiles( activeProfiles );
|
||||
|
||||
lineage.addFirst( project );
|
||||
|
||||
Parent parentModel = model.getParent();
|
||||
|
||||
if ( parentModel != null )
|
||||
{
|
||||
String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
|
||||
|
||||
if ( StringUtils.isEmpty( parentModel.getGroupId() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, "Missing groupId element from parent element" );
|
||||
}
|
||||
else if ( StringUtils.isEmpty( parentModel.getArtifactId() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, "Missing artifactId element from parent element" );
|
||||
}
|
||||
else if ( parentModel.getGroupId().equals( model.getGroupId() ) &&
|
||||
parentModel.getArtifactId().equals( model.getArtifactId() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, "Parent element is a duplicate of " + "the current project " );
|
||||
}
|
||||
else if ( StringUtils.isEmpty( parentModel.getVersion() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, "Missing version element from parent element" );
|
||||
}
|
||||
|
||||
// the only way this will have a value is if we find the parent on disk...
|
||||
File parentDescriptor = null;
|
||||
|
||||
model = null;
|
||||
Model currentModel = (Model) models.get( i );
|
||||
File currentPom = (File) poms.get( i );
|
||||
|
||||
String parentKey = createCacheKey( parentModel.getGroupId(), parentModel.getArtifactId(), parentModel.getVersion() );
|
||||
MavenProject parentProject = (MavenProject) rawProjectCache.get( parentKey );
|
||||
|
||||
if ( parentProject != null )
|
||||
MavenProject project = new MavenProject( currentModel );
|
||||
project.setFile( currentPom );
|
||||
|
||||
project.setActiveProfiles( profileAdvisor.applyActivatedProfiles( model, projectDir, explicitlyActive,
|
||||
explicitlyInactive ) );
|
||||
|
||||
if ( lastProject != null )
|
||||
{
|
||||
model = ModelUtils.cloneModel( parentProject.getModel() );
|
||||
project.setParent( lastProject );
|
||||
|
||||
parentDescriptor = parentProject.getFile();
|
||||
project.setParentArtifact( artifactFactory.createParentArtifact( lastProject.getGroupId(), lastProject
|
||||
.getArtifactId(), lastProject.getVersion() ) );
|
||||
}
|
||||
|
||||
String parentRelativePath = parentModel.getRelativePath();
|
||||
|
||||
// if we can't find a cached model matching the parent spec, then let's try to look on disk using
|
||||
// <relativePath/>
|
||||
if ( model == null && projectDir != null && StringUtils.isNotEmpty( parentRelativePath ) )
|
||||
{
|
||||
parentDescriptor = new File( projectDir, parentRelativePath );
|
||||
|
||||
if ( getLogger().isDebugEnabled() )
|
||||
{
|
||||
getLogger().debug( "Searching for parent-POM: " + parentModel.getId() + " of project: " + project.getId() + " in relative path: " + parentRelativePath );
|
||||
}
|
||||
|
||||
if ( parentDescriptor.isDirectory() )
|
||||
{
|
||||
if ( getLogger().isDebugEnabled() )
|
||||
{
|
||||
getLogger().debug( "Path specified in <relativePath/> (" + parentRelativePath +
|
||||
") is a directory. Searching for 'pom.xml' within this directory." );
|
||||
}
|
||||
|
||||
parentDescriptor = new File( parentDescriptor, "pom.xml" );
|
||||
|
||||
if ( !parentDescriptor.exists() )
|
||||
{
|
||||
if ( getLogger().isDebugEnabled() )
|
||||
{
|
||||
getLogger().debug( "Parent-POM: " + parentModel.getId() + " for project: " + project.getId() + " cannot be loaded from relative path: " + parentDescriptor + "; path does not exist." );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( parentDescriptor != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
parentDescriptor = parentDescriptor.getCanonicalFile();
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
getLogger().debug( "Failed to canonicalize potential parent POM: \'" + parentDescriptor + "\'",
|
||||
e );
|
||||
|
||||
parentDescriptor = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ( parentDescriptor != null && parentDescriptor.exists() )
|
||||
{
|
||||
Model candidateParent = readModel( projectId, parentDescriptor, strict );
|
||||
|
||||
String candidateParentGroupId = candidateParent.getGroupId();
|
||||
if ( candidateParentGroupId == null && candidateParent.getParent() != null )
|
||||
{
|
||||
candidateParentGroupId = candidateParent.getParent().getGroupId();
|
||||
}
|
||||
|
||||
String candidateParentVersion = candidateParent.getVersion();
|
||||
if ( candidateParentVersion == null && candidateParent.getParent() != null )
|
||||
{
|
||||
candidateParentVersion = candidateParent.getParent().getVersion();
|
||||
}
|
||||
|
||||
if ( parentModel.getGroupId().equals( candidateParentGroupId ) &&
|
||||
parentModel.getArtifactId().equals( candidateParent.getArtifactId() ) &&
|
||||
parentModel.getVersion().equals( candidateParentVersion ) )
|
||||
{
|
||||
model = candidateParent;
|
||||
|
||||
getLogger().debug( "Using parent-POM from the project hierarchy at: \'" +
|
||||
parentModel.getRelativePath() + "\' for project: " + project.getId() );
|
||||
}
|
||||
else
|
||||
{
|
||||
getLogger().debug( "Invalid parent-POM referenced by relative path '" +
|
||||
parentModel.getRelativePath() + "' in parent specification in " + project.getId() + ":" +
|
||||
"\n Specified: " + parentModel.getId() + "\n Found: " + candidateParent.getId() );
|
||||
}
|
||||
}
|
||||
else if ( getLogger().isDebugEnabled() )
|
||||
{
|
||||
getLogger().debug( "Parent-POM: " + parentModel.getId() + " not found in relative path: " + parentRelativePath );
|
||||
}
|
||||
}
|
||||
|
||||
Artifact parentArtifact = null;
|
||||
|
||||
// only resolve the parent model from the repository system if we didn't find it on disk...
|
||||
if ( model == null )
|
||||
{
|
||||
// MNG-2302: parent's File was being populated incorrectly when parent is loaded from repo.
|
||||
// keep this in line with other POMs loaded from the repository...the file should be null.
|
||||
parentDescriptor = null;
|
||||
|
||||
//!! (**)
|
||||
// ----------------------------------------------------------------------
|
||||
// Do we have the necessary information to actually find the parent
|
||||
// POMs here?? I don't think so ... Say only one remote repository is
|
||||
// specified and that is ibiblio then this model that we just read doesn't
|
||||
// have any repository information ... I think we might have to inherit
|
||||
// as we go in order to do this.
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
// we must add the repository this POM was found in too, by chance it may be located where the parent is
|
||||
// we can't query the parent to ask where it is :)
|
||||
List remoteRepositories = new ArrayList( aggregatedRemoteWagonRepositories );
|
||||
remoteRepositories.addAll( parentSearchRepositories );
|
||||
|
||||
if ( getLogger().isDebugEnabled() )
|
||||
{
|
||||
getLogger().debug(
|
||||
"Retrieving parent-POM: " + parentModel.getId() + " for project: "
|
||||
+ project.getId() + " from the repository." );
|
||||
}
|
||||
|
||||
parentArtifact = artifactFactory.createParentArtifact( parentModel.getGroupId(),
|
||||
parentModel.getArtifactId(),
|
||||
parentModel.getVersion() );
|
||||
|
||||
try
|
||||
{
|
||||
model = findModelFromRepository( parentArtifact, remoteRepositories, localRepository, false );
|
||||
}
|
||||
catch( ProjectBuildingException e )
|
||||
{
|
||||
throw new ProjectBuildingException( project.getId(), "Cannot find parent: " + e.getProjectId() + " for project: " + project.getId(), e );
|
||||
}
|
||||
}
|
||||
|
||||
if ( model != null && !"pom".equals( model.getPackaging() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( projectId, "Parent: " + model.getId() + " of project: " +
|
||||
projectId + " has wrong packaging: " + model.getPackaging() + ". Must be 'pom'." );
|
||||
}
|
||||
|
||||
File parentProjectDir = null;
|
||||
if ( parentDescriptor != null )
|
||||
{
|
||||
parentProjectDir = parentDescriptor.getParentFile();
|
||||
}
|
||||
lineage.addLast( project );
|
||||
|
||||
MavenProject parent = assembleLineage( model, lineage, localRepository, parentProjectDir,
|
||||
parentSearchRepositories, aggregatedRemoteWagonRepositories,
|
||||
externalProfileManager, strict );
|
||||
|
||||
parent.setFile( parentDescriptor );
|
||||
|
||||
project.setParent( parent );
|
||||
|
||||
project.setParentArtifact( parentArtifact );
|
||||
lastProject = project;
|
||||
}
|
||||
|
||||
return project;
|
||||
MavenProject result = (MavenProject) lineage.get( lineage.size() - 1 );
|
||||
|
||||
if ( externalProfileManager != null )
|
||||
{
|
||||
LinkedHashSet active = new LinkedHashSet();
|
||||
|
||||
List existingActiveProfiles = result.getActiveProfiles();
|
||||
if ( existingActiveProfiles != null && !existingActiveProfiles.isEmpty() )
|
||||
{
|
||||
active.addAll( existingActiveProfiles );
|
||||
}
|
||||
|
||||
profileAdvisor.applyActivatedExternalProfiles( result.getModel(), projectDir, externalProfileManager );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List injectActiveProfiles( ProfileManager profileManager, Model model )
|
||||
|
|
|
@ -0,0 +1,331 @@
|
|||
package org.apache.maven.project.build.model;
|
||||
|
||||
import org.apache.maven.model.Model;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage
|
||||
*/
|
||||
public class DefaultModelLineage
|
||||
implements ModelLineage
|
||||
{
|
||||
|
||||
private List tuples = new ArrayList();
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage#addParent(org.apache.maven.model.Model, java.io.File, java.util.List)
|
||||
*/
|
||||
public void addParent( Model model, File pomFile, List artifactRepositories )
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
throw new IllegalStateException( "You must call setOrigin(..) before adding a parent to the lineage." );
|
||||
}
|
||||
|
||||
tuples.add( new ModelLineageTuple( model, pomFile, artifactRepositories ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage#artifactRepositoryListIterator()
|
||||
*/
|
||||
public Iterator artifactRepositoryListIterator()
|
||||
{
|
||||
return new Iterator()
|
||||
{
|
||||
|
||||
private int idx = 0;
|
||||
|
||||
public boolean hasNext()
|
||||
{
|
||||
return tuples.size() > idx;
|
||||
}
|
||||
|
||||
public Object next()
|
||||
{
|
||||
return ( (ModelLineageTuple) tuples.get( idx++ ) ).remoteRepositories;
|
||||
}
|
||||
|
||||
public void remove()
|
||||
{
|
||||
tuples.remove( idx );
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage#fileIterator()
|
||||
*/
|
||||
public Iterator fileIterator()
|
||||
{
|
||||
return new Iterator()
|
||||
{
|
||||
|
||||
private int idx = 0;
|
||||
|
||||
public boolean hasNext()
|
||||
{
|
||||
return tuples.size() > idx;
|
||||
}
|
||||
|
||||
public Object next()
|
||||
{
|
||||
return ( (ModelLineageTuple) tuples.get( idx++ ) ).file;
|
||||
}
|
||||
|
||||
public void remove()
|
||||
{
|
||||
tuples.remove( idx );
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage#getArtifactRepositories(org.apache.maven.model.Model)
|
||||
*/
|
||||
public List getArtifactRepositories( Model model )
|
||||
{
|
||||
int index = tuples.indexOf( new ModelLineageTuple( model ) );
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( index );
|
||||
|
||||
return tuple != null ? tuple.remoteRepositories : null;
|
||||
}
|
||||
|
||||
public List getArtifactRepositoryListsInDescendingOrder()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List tuplesInReverse = new ArrayList( tuples );
|
||||
Collections.reverse( tuplesInReverse );
|
||||
|
||||
List results = new ArrayList( tuplesInReverse.size() );
|
||||
for ( Iterator it = tuplesInReverse.iterator(); it.hasNext(); )
|
||||
{
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) it.next();
|
||||
|
||||
results.add( tuple.remoteRepositories );
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage#getFile(org.apache.maven.model.Model)
|
||||
*/
|
||||
public File getFile( Model model )
|
||||
{
|
||||
int index = tuples.indexOf( new ModelLineageTuple( model ) );
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( index );
|
||||
|
||||
return tuple != null ? tuple.file : null;
|
||||
}
|
||||
|
||||
public List getFilesInDescendingOrder()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List tuplesInReverse = new ArrayList( tuples );
|
||||
Collections.reverse( tuplesInReverse );
|
||||
|
||||
List results = new ArrayList( tuplesInReverse.size() );
|
||||
for ( Iterator it = tuplesInReverse.iterator(); it.hasNext(); )
|
||||
{
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) it.next();
|
||||
|
||||
results.add( tuple.file );
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public List getModelsInDescendingOrder()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List tuplesInReverse = new ArrayList( tuples );
|
||||
Collections.reverse( tuplesInReverse );
|
||||
|
||||
List results = new ArrayList( tuplesInReverse.size() );
|
||||
for ( Iterator it = tuplesInReverse.iterator(); it.hasNext(); )
|
||||
{
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) it.next();
|
||||
|
||||
results.add( tuple.model );
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public List getOriginatingArtifactRepositoryList()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( 0 );
|
||||
|
||||
return tuple.remoteRepositories;
|
||||
}
|
||||
|
||||
public Model getOriginatingModel()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( 0 );
|
||||
|
||||
return tuple.model;
|
||||
}
|
||||
|
||||
public File getOriginatingPOMFile()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( 0 );
|
||||
|
||||
return tuple.file;
|
||||
}
|
||||
|
||||
public List getDeepestArtifactRepositoryList()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( tuples.size() - 1 );
|
||||
|
||||
return tuple.remoteRepositories;
|
||||
}
|
||||
|
||||
public File getDeepestFile()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( tuples.size() - 1 );
|
||||
|
||||
return tuple.file;
|
||||
}
|
||||
|
||||
public Model getDeepestModel()
|
||||
{
|
||||
if ( tuples.isEmpty() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ModelLineageTuple tuple = (ModelLineageTuple) tuples.get( tuples.size() - 1 );
|
||||
|
||||
return tuple.model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage#modelIterator()
|
||||
*/
|
||||
public Iterator modelIterator()
|
||||
{
|
||||
return new Iterator()
|
||||
{
|
||||
|
||||
private int idx = 0;
|
||||
|
||||
public boolean hasNext()
|
||||
{
|
||||
return tuples.size() > idx;
|
||||
}
|
||||
|
||||
public Object next()
|
||||
{
|
||||
return ( (ModelLineageTuple) tuples.get( idx++ ) ).model;
|
||||
}
|
||||
|
||||
public void remove()
|
||||
{
|
||||
tuples.remove( idx );
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public void setOrigin( Model model, File pomFile, List artifactRepositories )
|
||||
{
|
||||
if ( !tuples.isEmpty() )
|
||||
{
|
||||
throw new IllegalStateException( "Origin already set; you must use addParent(..) for successive additions to the lineage." );
|
||||
}
|
||||
|
||||
tuples.add( new ModelLineageTuple( model, pomFile, artifactRepositories ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineage#size()
|
||||
*/
|
||||
public int size()
|
||||
{
|
||||
return tuples.size();
|
||||
}
|
||||
|
||||
private static final class ModelLineageTuple
|
||||
{
|
||||
private Model model;
|
||||
|
||||
private File file;
|
||||
|
||||
private List remoteRepositories;
|
||||
|
||||
private ModelLineageTuple( Model model )
|
||||
{
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
private ModelLineageTuple( Model model, File file, List remoteRepositories )
|
||||
{
|
||||
this.model = model;
|
||||
this.file = file;
|
||||
this.remoteRepositories = remoteRepositories;
|
||||
}
|
||||
|
||||
public boolean equals( Object other )
|
||||
{
|
||||
if ( this == other )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if ( other instanceof ModelLineageTuple )
|
||||
{
|
||||
ModelLineageTuple otherTuple = (ModelLineageTuple) other;
|
||||
|
||||
return model.getId().equals( otherTuple.model.getId() );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,361 @@
|
|||
package org.apache.maven.project.build.model;
|
||||
|
||||
import org.apache.maven.MavenTools;
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
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.ArtifactNotFoundException;
|
||||
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
|
||||
import org.apache.maven.artifact.resolver.ArtifactResolver;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.Parent;
|
||||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
||||
import org.apache.maven.profiles.ProfileManager;
|
||||
import org.apache.maven.project.ProjectBuildingException;
|
||||
import org.apache.maven.project.build.profile.ProfileAdvisor;
|
||||
import org.codehaus.plexus.logging.LogEnabled;
|
||||
import org.codehaus.plexus.logging.Logger;
|
||||
import org.codehaus.plexus.logging.console.ConsoleLogger;
|
||||
import org.codehaus.plexus.util.IOUtil;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineageBuilder
|
||||
*/
|
||||
public class DefaultModelLineageBuilder
|
||||
implements ModelLineageBuilder, LogEnabled
|
||||
{
|
||||
|
||||
public static final String ROLE_HINT = "default";
|
||||
|
||||
private ArtifactFactory artifactFactory;
|
||||
|
||||
private ArtifactResolver artifactResolver;
|
||||
|
||||
private MavenTools mavenTools;
|
||||
|
||||
private ProfileAdvisor profileAdvisor;
|
||||
|
||||
private Logger logger;
|
||||
|
||||
public DefaultModelLineageBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
public DefaultModelLineageBuilder( ArtifactResolver resolver, ArtifactFactory artifactFactory )
|
||||
{
|
||||
this.artifactResolver = resolver;
|
||||
this.artifactFactory = artifactFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.project.build.model.ModelLineageBuilder#buildModelLineage(java.io.File, org.apache.maven.artifact.repository.ArtifactRepository, java.util.List)
|
||||
*/
|
||||
public ModelLineage buildModelLineage( File pom, ArtifactRepository localRepository, List remoteRepositories,
|
||||
ProfileManager profileManager )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
ModelLineage lineage = new DefaultModelLineage();
|
||||
|
||||
File pomFile = pom;
|
||||
List currentRemoteRepositories = remoteRepositories == null ? new ArrayList()
|
||||
: new ArrayList( remoteRepositories );
|
||||
|
||||
while ( pomFile != null )
|
||||
{
|
||||
Model model = readModel( pomFile );
|
||||
|
||||
if ( lineage.size() == 0 )
|
||||
{
|
||||
lineage.setOrigin( model, pomFile, currentRemoteRepositories );
|
||||
}
|
||||
else
|
||||
{
|
||||
lineage.addParent( model, pomFile, currentRemoteRepositories );
|
||||
}
|
||||
|
||||
currentRemoteRepositories = updateRepositorySet( model, currentRemoteRepositories, pomFile, profileManager );
|
||||
|
||||
pomFile = resolveParentPom( model, currentRemoteRepositories, localRepository, pomFile );
|
||||
}
|
||||
|
||||
return lineage;
|
||||
}
|
||||
|
||||
public void resumeBuildingModelLineage( ModelLineage lineage, ArtifactRepository localRepository,
|
||||
ProfileManager profileManager )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
File pomFile = lineage.getDeepestFile();
|
||||
List currentRemoteRepositories = lineage.getDeepestArtifactRepositoryList();
|
||||
|
||||
if ( currentRemoteRepositories == null )
|
||||
{
|
||||
currentRemoteRepositories = new ArrayList();
|
||||
}
|
||||
|
||||
Model model = lineage.getDeepestModel();
|
||||
|
||||
// use the above information to re-bootstrap the resolution chain...
|
||||
pomFile = resolveParentPom( model, currentRemoteRepositories, localRepository, pomFile );
|
||||
|
||||
while ( pomFile != null )
|
||||
{
|
||||
model = readModel( pomFile );
|
||||
|
||||
if ( lineage.size() == 0 )
|
||||
{
|
||||
lineage.setOrigin( model, pomFile, currentRemoteRepositories );
|
||||
}
|
||||
else
|
||||
{
|
||||
lineage.addParent( model, pomFile, currentRemoteRepositories );
|
||||
}
|
||||
|
||||
currentRemoteRepositories = updateRepositorySet( model, currentRemoteRepositories, pomFile, profileManager );
|
||||
|
||||
pomFile = resolveParentPom( model, currentRemoteRepositories, localRepository, pomFile );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the Model instance from the given POM file.
|
||||
*/
|
||||
private Model readModel( File pomFile )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
Model model;
|
||||
FileReader reader = null;
|
||||
|
||||
try
|
||||
{
|
||||
reader = new FileReader( pomFile );
|
||||
model = new MavenXpp3Reader().read( reader );
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
throw new ProjectBuildingException( "unknown", "Failed to read model from: " + pomFile, e );
|
||||
}
|
||||
catch ( XmlPullParserException e )
|
||||
{
|
||||
throw new ProjectBuildingException( "unknown", "Failed to parse model from: " + pomFile, e );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( reader );
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the remote repository set used to resolve parent POMs, by adding those declared in
|
||||
* the given model to the HEAD of a new list, then appending the old remote repositories list.
|
||||
* The specified pomFile is used for error reporting.
|
||||
* @param profileManager
|
||||
*/
|
||||
private List updateRepositorySet( Model model, List oldArtifactRepositories, File pomFile,
|
||||
ProfileManager externalProfileManager )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
List repositories = model.getRepositories();
|
||||
|
||||
loadActiveProfileRepositories( repositories, model, externalProfileManager, pomFile.getParentFile() );
|
||||
|
||||
Set artifactRepositories = null;
|
||||
|
||||
if ( repositories != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
List lastRemoteRepos = oldArtifactRepositories;
|
||||
List remoteRepos = mavenTools.buildArtifactRepositories( repositories );
|
||||
|
||||
artifactRepositories = new LinkedHashSet( remoteRepos.size() + oldArtifactRepositories.size() );
|
||||
|
||||
artifactRepositories.addAll( remoteRepos );
|
||||
artifactRepositories.addAll( lastRemoteRepos );
|
||||
}
|
||||
catch ( InvalidRepositoryException e )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(), "Failed to create ArtifactRepository list for: "
|
||||
+ pomFile, e );
|
||||
}
|
||||
}
|
||||
|
||||
return new ArrayList( artifactRepositories );
|
||||
}
|
||||
|
||||
private void loadActiveProfileRepositories( List repositories, Model model, ProfileManager profileManager,
|
||||
File projectDir )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
List explicitlyActive;
|
||||
List explicitlyInactive;
|
||||
|
||||
if ( profileManager != null )
|
||||
{
|
||||
explicitlyActive = profileManager.getExplicitlyActivatedIds();
|
||||
explicitlyInactive = profileManager.getExplicitlyDeactivatedIds();
|
||||
}
|
||||
else
|
||||
{
|
||||
explicitlyActive = Collections.EMPTY_LIST;
|
||||
explicitlyInactive = Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
LinkedHashSet profileRepos = profileAdvisor.getArtifactRepositoriesFromActiveProfiles( model, projectDir, explicitlyActive, explicitlyInactive );
|
||||
|
||||
if ( !profileRepos.isEmpty() )
|
||||
{
|
||||
repositories.addAll( profileRepos );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pull the parent specification out of the given model, construct an Artifact instance, and
|
||||
* resolve that artifact...then, return the resolved POM file for the parent.
|
||||
*/
|
||||
private File resolveParentPom( Model model, List remoteRepositories, ArtifactRepository localRepository,
|
||||
File modelPomFile )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
Parent modelParent = model.getParent();
|
||||
|
||||
File pomFile = null;
|
||||
|
||||
if ( modelParent != null )
|
||||
{
|
||||
validateParentDeclaration( modelParent, model );
|
||||
|
||||
pomFile = resolveParentWithRelativePath( modelParent, modelPomFile );
|
||||
|
||||
if ( pomFile == null )
|
||||
{
|
||||
pomFile = resolveParentFromRepositories( modelParent, localRepository, remoteRepositories, modelPomFile );
|
||||
}
|
||||
}
|
||||
|
||||
return pomFile;
|
||||
}
|
||||
|
||||
private void validateParentDeclaration( Parent modelParent, Model model )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
if ( StringUtils.isEmpty( modelParent.getGroupId() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(), "Missing groupId element from parent element" );
|
||||
}
|
||||
else if ( StringUtils.isEmpty( modelParent.getArtifactId() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(), "Missing artifactId element from parent element" );
|
||||
}
|
||||
else if ( modelParent.getGroupId().equals( model.getGroupId() )
|
||||
&& modelParent.getArtifactId().equals( model.getArtifactId() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(), "Parent element is a duplicate of "
|
||||
+ "the current project " );
|
||||
}
|
||||
else if ( StringUtils.isEmpty( modelParent.getVersion() ) )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(), "Missing version element from parent element" );
|
||||
}
|
||||
}
|
||||
|
||||
private File resolveParentFromRepositories( Parent modelParent, ArtifactRepository localRepository,
|
||||
List remoteRepositories, File pomFile )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
Artifact parentPomArtifact = artifactFactory.createBuildArtifact( modelParent.getGroupId(), modelParent
|
||||
.getArtifactId(), modelParent.getVersion(), "pom" );
|
||||
|
||||
getLogger().debug( "Looking for parent: " + modelParent.getId() + " using artifact: " + parentPomArtifact );
|
||||
getLogger().debug( "\tLocal repository: " + localRepository.getBasedir() + "\n" );
|
||||
getLogger().debug( "\tRemote repositories:\n" + remoteRepositories.toString().replace( ',', '\n' ) + "\n" );
|
||||
|
||||
try
|
||||
{
|
||||
artifactResolver.resolve( parentPomArtifact, remoteRepositories, localRepository );
|
||||
}
|
||||
catch ( ArtifactResolutionException e )
|
||||
{
|
||||
throw new ProjectBuildingException( "Parent: " + modelParent.getId(),
|
||||
"Failed to resolve POM for parent of: " + pomFile, e );
|
||||
}
|
||||
catch ( ArtifactNotFoundException e )
|
||||
{
|
||||
throw new ProjectBuildingException( "Parent: " + modelParent.getId(), "Cannot find parent: " + parentPomArtifact.getId() + " of: "
|
||||
+ pomFile, e );
|
||||
}
|
||||
|
||||
if ( parentPomArtifact.isResolved() )
|
||||
{
|
||||
return parentPomArtifact.getFile();
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private File resolveParentWithRelativePath( Parent modelParent, File modelPomFile )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
String relativePath = modelParent.getRelativePath();
|
||||
File modelDir = modelPomFile.getParentFile();
|
||||
|
||||
File parentPomFile = new File( modelDir, relativePath );
|
||||
|
||||
if ( parentPomFile.isDirectory() )
|
||||
{
|
||||
getLogger().debug( "Parent relative-path is a directory; assuming \'pom.xml\' file exists within." );
|
||||
parentPomFile = new File( parentPomFile, "pom.xml" );
|
||||
}
|
||||
|
||||
getLogger().debug( "Looking for parent: " + modelParent.getId() + " in: " + parentPomFile );
|
||||
|
||||
if ( parentPomFile.exists() )
|
||||
{
|
||||
Model parentModel = readModel( parentPomFile );
|
||||
|
||||
boolean groupsMatch = parentModel.getGroupId() == null
|
||||
|| parentModel.getGroupId().equals( modelParent.getGroupId() );
|
||||
boolean versionsMatch = parentModel.getVersion() == null
|
||||
|| parentModel.getVersion().equals( modelParent.getVersion() );
|
||||
|
||||
if ( groupsMatch && versionsMatch && parentModel.getArtifactId().equals( modelParent.getArtifactId() ) )
|
||||
{
|
||||
return parentPomFile;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Logger getLogger()
|
||||
{
|
||||
if ( logger == null )
|
||||
{
|
||||
logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "DefaultModelLineageBuilder:internal" );
|
||||
}
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
public void enableLogging( Logger logger )
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
package org.apache.maven.project.build.model;
|
||||
|
||||
import org.apache.maven.model.Model;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Tracks information from a current POM and its ancestors, including Model instances, associated
|
||||
* POM files, and repository lists used to resolve each model.
|
||||
*
|
||||
* @author jdcasey
|
||||
*
|
||||
*/
|
||||
public interface ModelLineage
|
||||
{
|
||||
|
||||
/**
|
||||
* Retrieve the Model instance for the deepest ancestor which has been resolved so far in this
|
||||
* lineage.
|
||||
*/
|
||||
Model getDeepestModel();
|
||||
|
||||
/**
|
||||
* Retrieve the POM file for the deepest ancestor which has been resolved so far in this
|
||||
* lineage.
|
||||
*/
|
||||
File getDeepestFile();
|
||||
|
||||
/**
|
||||
* Retrieve the remote-repository list for the deepest ancestor which has been resolved so far
|
||||
* in this lineage.
|
||||
*/
|
||||
List getDeepestArtifactRepositoryList();
|
||||
|
||||
/**
|
||||
* Retrieve the Model instance for the POM from which this lineage was constructed. This is the
|
||||
* "leaf" of the inheritance hierarchy, or the current POM, or the child (all means the same
|
||||
* thing).
|
||||
*/
|
||||
Model getOriginatingModel();
|
||||
|
||||
/**
|
||||
* Retrieve the File for the POM from which this lineage was constructed. This is the
|
||||
* "leaf" of the inheritance hierarchy, or the current POM, or the child (all means the same
|
||||
* thing).
|
||||
*/
|
||||
File getOriginatingPOMFile();
|
||||
|
||||
/**
|
||||
* Retrieve the List of ArtifactRepository instances used to resolve the first parent POM of the
|
||||
* POM from which this lineage was constructed. This is the "leaf" of the inheritance hierarchy,
|
||||
* or the current POM, or the child (all means the same thing).
|
||||
*/
|
||||
List getOriginatingArtifactRepositoryList();
|
||||
|
||||
/**
|
||||
* Setup the originating POM information from which this lineage is constructed. This is the
|
||||
* "child" POM that is the starting point of the build.
|
||||
*
|
||||
* @throws IllegalStateException When the originating POM information has already been set.
|
||||
*/
|
||||
void setOrigin( Model model, File pomFile, List artifactRepositories );
|
||||
|
||||
/**
|
||||
* Add a parent model, along with its file and the repositories used to resolve it.
|
||||
* NOTE: If setOrigin(..) hasn't been called, this method will result in an IllegalStateException.
|
||||
*
|
||||
* @throws IllegalStateException When the originating POM information has not yet been set.
|
||||
*/
|
||||
void addParent( Model model, File pomFile, List artifactRepositories );
|
||||
|
||||
/**
|
||||
* Retrieve the models in this lineage, with the deepest parent at the zero index, and the current
|
||||
* POM at the last index.
|
||||
*/
|
||||
List getModelsInDescendingOrder();
|
||||
|
||||
/**
|
||||
* Retrieve the files used to construct this lineage, with that of the deepest parent at the
|
||||
* zero index, and that of the current POM at the last index.
|
||||
*/
|
||||
List getFilesInDescendingOrder();
|
||||
|
||||
/**
|
||||
* Retrieve the remote-artifact repository lists used to construct this lineage, with
|
||||
* that of the deepest parent at the zero index, and that of the current POM at the last index.
|
||||
*/
|
||||
List getArtifactRepositoryListsInDescendingOrder();
|
||||
|
||||
/**
|
||||
* Iterate over the lineage of Model instances, starting with the child (current) Model,
|
||||
* and ending with the deepest ancestor.
|
||||
*/
|
||||
Iterator modelIterator();
|
||||
|
||||
/**
|
||||
* Iterate over the lineage of POM Files, starting with the child (current) POM and ending with
|
||||
* the deepest ancestor.
|
||||
*/
|
||||
Iterator fileIterator();
|
||||
|
||||
/**
|
||||
* Iterate over the remote-repository Lists used to resolve the lineage, starting with the
|
||||
* child (current) remote-repository List and ending with the deepest ancestor.
|
||||
*/
|
||||
Iterator artifactRepositoryListIterator();
|
||||
|
||||
/**
|
||||
* Retrieve the File from which the given Model instance was read. If the model itself doesn't
|
||||
* belong to this lineage, match it in the lineage by Model.getId().
|
||||
*/
|
||||
File getFile( Model model );
|
||||
|
||||
/**
|
||||
* Retrieve the List of remote repositories from which the given Model instance was resolved.
|
||||
* If the model itself doesn't belong to this lineage, match it in the lineage by Model.getId().
|
||||
*/
|
||||
List getArtifactRepositories( Model model );
|
||||
|
||||
/**
|
||||
* Retrieve the number of entries (POMs) in this lineage.
|
||||
*/
|
||||
int size();
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package org.apache.maven.project.build.model;
|
||||
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.profiles.ProfileManager;
|
||||
import org.apache.maven.project.ProjectBuildingException;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Builds the lineage of Model instances, starting from a given POM file, and stretching back through
|
||||
* all of the parent POMs that are defined in the respective <parent/> sections.
|
||||
*
|
||||
* NOTE: In all of the build/resume methods below, each Model MUST have its active profiles searched
|
||||
* for new repositories from which to discover parent POMs.
|
||||
*/
|
||||
public interface ModelLineageBuilder
|
||||
{
|
||||
|
||||
String ROLE = ModelLineageBuilder.class.getName();
|
||||
|
||||
/**
|
||||
* Construct a lineage of the current POM plus all of its ancestors.
|
||||
*
|
||||
* COMING: Also, set ProjectBuildContext.currentModelLineage build-context to the result of this
|
||||
* method before returning.
|
||||
*
|
||||
* @param pom The current POM, whose Model will terminate the constructed lineage
|
||||
* @param localRepository The local repository against which parent POMs should be resolved
|
||||
* @param remoteRepositories List of ArtifactRepository instances against which parent POMs
|
||||
* should be resolved
|
||||
*/
|
||||
ModelLineage buildModelLineage( File pom, ArtifactRepository localRepository, List remoteRepositories, ProfileManager profileManager )
|
||||
throws ProjectBuildingException;
|
||||
|
||||
/**
|
||||
* Resume the process of constructing a lineage of inherited models, picking up using the deepest
|
||||
* parent already in the lineage.
|
||||
*
|
||||
*/
|
||||
void resumeBuildingModelLineage( ModelLineage lineage, ArtifactRepository localRepository, ProfileManager profileManager )
|
||||
throws ProjectBuildingException;
|
||||
|
||||
}
|
|
@ -0,0 +1,222 @@
|
|||
package org.apache.maven.project.build.profile;
|
||||
|
||||
import org.apache.maven.MavenTools;
|
||||
import org.apache.maven.artifact.ArtifactUtils;
|
||||
import org.apache.maven.artifact.InvalidRepositoryException;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.Profile;
|
||||
import org.apache.maven.model.Repository;
|
||||
import org.apache.maven.profiles.DefaultProfileManager;
|
||||
import org.apache.maven.profiles.MavenProfilesBuilder;
|
||||
import org.apache.maven.profiles.ProfileManager;
|
||||
import org.apache.maven.profiles.ProfilesConversionUtils;
|
||||
import org.apache.maven.profiles.ProfilesRoot;
|
||||
import org.apache.maven.profiles.activation.ProfileActivationException;
|
||||
import org.apache.maven.project.ProjectBuildingException;
|
||||
import org.apache.maven.project.injection.ProfileInjector;
|
||||
import org.codehaus.plexus.PlexusConstants;
|
||||
import org.codehaus.plexus.PlexusContainer;
|
||||
import org.codehaus.plexus.context.Context;
|
||||
import org.codehaus.plexus.context.ContextException;
|
||||
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
|
||||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DefaultProfileAdvisor
|
||||
implements ProfileAdvisor, Contextualizable
|
||||
{
|
||||
|
||||
public static final String ROLE_HINT = "default";
|
||||
|
||||
private MavenTools mavenTools;
|
||||
|
||||
private MavenProfilesBuilder profilesBuilder;
|
||||
|
||||
private ProfileInjector profileInjector;
|
||||
|
||||
private PlexusContainer container;
|
||||
|
||||
public List applyActivatedProfiles( Model model, File projectDir, List explicitlyActiveIds,
|
||||
List explicitlyInactiveIds )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
ProfileManager profileManager = buildProfileManager( model, projectDir, explicitlyActiveIds,
|
||||
explicitlyInactiveIds );
|
||||
|
||||
return applyActivatedProfiles( model, projectDir, profileManager );
|
||||
}
|
||||
|
||||
public List applyActivatedExternalProfiles( Model model, File projectDir, ProfileManager externalProfileManager )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
return applyActivatedProfiles( model, projectDir, externalProfileManager );
|
||||
}
|
||||
|
||||
private List applyActivatedProfiles( Model model, File projectDir, ProfileManager profileManager )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
List activeProfiles;
|
||||
|
||||
if ( profileManager != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
activeProfiles = profileManager.getActiveProfiles();
|
||||
}
|
||||
catch ( ProfileActivationException e )
|
||||
{
|
||||
String groupId = model.getGroupId();
|
||||
if ( groupId == null )
|
||||
{
|
||||
groupId = "unknown";
|
||||
}
|
||||
|
||||
String artifactId = model.getArtifactId();
|
||||
if ( artifactId == null )
|
||||
{
|
||||
artifactId = "unknown";
|
||||
}
|
||||
|
||||
String projectId = ArtifactUtils.versionlessKey( groupId, artifactId );
|
||||
|
||||
throw new ProjectBuildingException( projectId, e.getMessage(), e );
|
||||
}
|
||||
|
||||
for ( Iterator it = activeProfiles.iterator(); it.hasNext(); )
|
||||
{
|
||||
Profile profile = (Profile) it.next();
|
||||
|
||||
profileInjector.inject( profile, model );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
activeProfiles = Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
return activeProfiles;
|
||||
}
|
||||
|
||||
private ProfileManager buildProfileManager( Model model, File projectDir, List explicitlyActiveIds,
|
||||
List explicitlyInactiveIds )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
ProfileManager profileManager = new DefaultProfileManager( container, new Properties() );
|
||||
|
||||
profileManager.explicitlyActivate( explicitlyActiveIds );
|
||||
profileManager.explicitlyDeactivate( explicitlyInactiveIds );
|
||||
|
||||
profileManager.addProfiles( model.getProfiles() );
|
||||
|
||||
if ( projectDir != null )
|
||||
{
|
||||
loadExternalProjectProfiles( profileManager, model, projectDir );
|
||||
}
|
||||
|
||||
return profileManager;
|
||||
}
|
||||
|
||||
public LinkedHashSet getArtifactRepositoriesFromActiveProfiles( Model model, File projectDir,
|
||||
List explicitlyActiveIds, List explicitlyInactiveIds )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
ProfileManager profileManager = buildProfileManager( model, projectDir, explicitlyActiveIds,
|
||||
explicitlyInactiveIds );
|
||||
|
||||
List activeExternalProfiles;
|
||||
{
|
||||
try
|
||||
{
|
||||
activeExternalProfiles = profileManager.getActiveProfiles();
|
||||
}
|
||||
catch ( ProfileActivationException e )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(),
|
||||
"Failed to compute active profiles for repository aggregation.", e );
|
||||
}
|
||||
|
||||
LinkedHashSet remoteRepositories = new LinkedHashSet();
|
||||
|
||||
for ( Iterator i = activeExternalProfiles.iterator(); i.hasNext(); )
|
||||
{
|
||||
Profile externalProfile = (Profile) i.next();
|
||||
|
||||
for ( Iterator repoIterator = externalProfile.getRepositories().iterator(); repoIterator.hasNext(); )
|
||||
{
|
||||
Repository mavenRepo = (Repository) repoIterator.next();
|
||||
|
||||
ArtifactRepository artifactRepo = null;
|
||||
try
|
||||
{
|
||||
artifactRepo = mavenTools.buildArtifactRepository( mavenRepo );
|
||||
}
|
||||
catch ( InvalidRepositoryException e )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(), e.getMessage(), e );
|
||||
}
|
||||
|
||||
remoteRepositories.add( artifactRepo );
|
||||
}
|
||||
}
|
||||
|
||||
return remoteRepositories;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadExternalProjectProfiles( ProfileManager profileManager, Model model, File projectDir )
|
||||
throws ProjectBuildingException
|
||||
{
|
||||
if ( projectDir != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
ProfilesRoot root = profilesBuilder.buildProfiles( projectDir );
|
||||
|
||||
if ( root != null )
|
||||
{
|
||||
List active = root.getActiveProfiles();
|
||||
|
||||
if ( active != null && !active.isEmpty() )
|
||||
{
|
||||
profileManager.explicitlyActivate( root.getActiveProfiles() );
|
||||
}
|
||||
|
||||
for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
|
||||
{
|
||||
org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
|
||||
|
||||
Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile );
|
||||
|
||||
profileManager.addProfile( converted );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(), "Cannot read profiles.xml resource from directory: "
|
||||
+ projectDir, e );
|
||||
}
|
||||
catch ( XmlPullParserException e )
|
||||
{
|
||||
throw new ProjectBuildingException( model.getId(),
|
||||
"Cannot parse profiles.xml resource from directory: " + projectDir,
|
||||
e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void contextualize( Context context )
|
||||
throws ContextException
|
||||
{
|
||||
this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package org.apache.maven.project.build.profile;
|
||||
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.profiles.ProfileManager;
|
||||
import org.apache.maven.project.ProjectBuildingException;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jdcasey
|
||||
*
|
||||
*/
|
||||
public interface ProfileAdvisor
|
||||
{
|
||||
|
||||
String ROLE = ProfileAdvisor.class.getName();
|
||||
|
||||
LinkedHashSet getArtifactRepositoriesFromActiveProfiles( Model model, File projectDir, List explicitlyActiveIds, List explicitlyInactiveIds )
|
||||
throws ProjectBuildingException;
|
||||
|
||||
List applyActivatedProfiles( Model model, File projectDir, List explicitlyActiveIds, List explicitlyInactiveIds )
|
||||
throws ProjectBuildingException;
|
||||
|
||||
List applyActivatedExternalProfiles( Model model, File projectDir, ProfileManager externalProfileManager )
|
||||
throws ProjectBuildingException;
|
||||
|
||||
}
|
|
@ -1,5 +1,45 @@
|
|||
<component-set>
|
||||
<components>
|
||||
<component>
|
||||
<role>org.apache.maven.project.build.profile.ProfileAdvisor</role>
|
||||
<role-hint>default</role-hint>
|
||||
<implementation>org.apache.maven.project.build.profile.DefaultProfileAdvisor</implementation>
|
||||
<requirements>
|
||||
<requirement>
|
||||
<role>org.apache.maven.MavenTools</role>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.profiles.MavenProfilesBuilder</role>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.injection.ProfileInjector</role>
|
||||
</requirement>
|
||||
</requirements>
|
||||
</component>
|
||||
|
||||
|
||||
|
||||
|
||||
<component>
|
||||
<role>org.apache.maven.project.build.model.ModelLineageBuilder</role>
|
||||
<role-hint>default</role-hint>
|
||||
<implementation>org.apache.maven.project.build.model.DefaultModelLineageBuilder</implementation>
|
||||
<requirements>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.build.profile.ProfileAdvisor</role>
|
||||
<role-hint>default</role-hint>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.MavenTools</role>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.artifact.resolver.ArtifactResolver</role>
|
||||
</requirement>
|
||||
</requirements>
|
||||
</component>
|
||||
<!--
|
||||
|
|
||||
|
|
||||
|
@ -50,6 +90,14 @@
|
|||
<role>org.apache.maven.project.MavenProjectBuilder</role>
|
||||
<implementation>org.apache.maven.project.DefaultMavenProjectBuilder</implementation>
|
||||
<requirements>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.build.model.ModelLineageBuilder</role>
|
||||
<role-hint>default</role-hint>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.build.profile.ProfileAdvisor</role>
|
||||
<role-hint>default</role-hint>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.profiles.MavenProfilesBuilder</role>
|
||||
</requirement>
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package org.apache.maven.profiles.activation;
|
||||
|
||||
import org.apache.maven.model.Activation;
|
||||
import org.apache.maven.model.ActivationProperty;
|
||||
import org.apache.maven.model.Profile;
|
||||
import org.codehaus.plexus.context.ContextException;
|
||||
import org.codehaus.plexus.context.DefaultContext;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SystemPropertyProfileActivatorTest
|
||||
extends TestCase
|
||||
{
|
||||
|
||||
public void testCanDetect_ShouldReturnTrueWhenActivationPropertyIsPresent()
|
||||
throws ContextException
|
||||
{
|
||||
ActivationProperty prop = new ActivationProperty();
|
||||
prop.setName( "test" );
|
||||
|
||||
Activation activation = new Activation();
|
||||
|
||||
activation.setProperty( prop );
|
||||
|
||||
Profile profile = new Profile();
|
||||
|
||||
profile.setActivation( activation );
|
||||
|
||||
assertTrue( buildProfileActivator().canDetermineActivation( profile ) );
|
||||
}
|
||||
|
||||
public void testCanDetect_ShouldReturnFalseWhenActivationPropertyIsNotPresent()
|
||||
throws ContextException
|
||||
{
|
||||
Activation activation = new Activation();
|
||||
|
||||
Profile profile = new Profile();
|
||||
|
||||
profile.setActivation( activation );
|
||||
|
||||
assertFalse( buildProfileActivator().canDetermineActivation( profile ) );
|
||||
}
|
||||
|
||||
public void testIsActive_ShouldReturnTrueWhenPropertyNameSpecifiedAndPresent()
|
||||
throws ContextException
|
||||
{
|
||||
ActivationProperty prop = new ActivationProperty();
|
||||
prop.setName( "test" );
|
||||
|
||||
Activation activation = new Activation();
|
||||
|
||||
activation.setProperty( prop );
|
||||
|
||||
Profile profile = new Profile();
|
||||
|
||||
profile.setActivation( activation );
|
||||
|
||||
System.setProperty( "test", "true" );
|
||||
|
||||
assertTrue( buildProfileActivator().isActive( profile ) );
|
||||
}
|
||||
|
||||
public void testIsActive_ShouldReturnFalseWhenPropertyNameSpecifiedAndMissing()
|
||||
throws ContextException
|
||||
{
|
||||
ActivationProperty prop = new ActivationProperty();
|
||||
prop.setName( "test" );
|
||||
|
||||
Activation activation = new Activation();
|
||||
|
||||
activation.setProperty( prop );
|
||||
|
||||
Profile profile = new Profile();
|
||||
|
||||
profile.setActivation( activation );
|
||||
|
||||
Properties props = System.getProperties();
|
||||
props.remove( "test" );
|
||||
System.setProperties( props );
|
||||
|
||||
assertFalse( buildProfileActivator().isActive( profile ) );
|
||||
}
|
||||
|
||||
private SystemPropertyProfileActivator buildProfileActivator()
|
||||
throws ContextException
|
||||
{
|
||||
SystemPropertyProfileActivator activator = new SystemPropertyProfileActivator();
|
||||
activator.contextualize( new DefaultContext() );
|
||||
|
||||
return activator;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,360 @@
|
|||
package org.apache.maven.project.build.model;
|
||||
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.project.build.model.ModelLineage;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public abstract class AbstractModelLineageTest
|
||||
extends TestCase
|
||||
{
|
||||
|
||||
protected abstract ModelLineage newModelLineage();
|
||||
|
||||
public void testAddParent_ShouldThrowIllegalStateExceptionWhenSetOriginNotCalled()
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
assertEquals( 0, ml.size() );
|
||||
|
||||
try
|
||||
{
|
||||
ml.addParent( new Model(), null, null );
|
||||
|
||||
fail( "Should throw IllegalStateException when setOrigin(..) is not called first." );
|
||||
}
|
||||
catch ( IllegalStateException e )
|
||||
{
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetOrigin_ShouldThrowIllegalStateExceptionWhenSetOriginCalledRepeatedly()
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
assertEquals( 0, ml.size() );
|
||||
|
||||
ml.setOrigin( new Model(), null, null );
|
||||
|
||||
assertEquals( 1, ml.size() );
|
||||
|
||||
try
|
||||
{
|
||||
ml.setOrigin( new Model(), null, null );
|
||||
|
||||
fail( "Should throw IllegalStateException when setOrigin(..) called multiple times." );
|
||||
}
|
||||
catch ( IllegalStateException e )
|
||||
{
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetOriginAndSize_SizeShouldIncrementByOneWhenOriginSet()
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
assertEquals( 0, ml.size() );
|
||||
|
||||
ml.setOrigin( new Model(), null, null );
|
||||
|
||||
assertEquals( 1, ml.size() );
|
||||
}
|
||||
|
||||
public void testModelIterator_ShouldAddTwoModelsAndIterateInFIFOOrder()
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
String gOne = "group1";
|
||||
String aOne = "artifact1";
|
||||
String vOne = "1";
|
||||
|
||||
Model mOne = new Model();
|
||||
|
||||
mOne.setGroupId( gOne );
|
||||
mOne.setArtifactId( aOne );
|
||||
mOne.setVersion( vOne );
|
||||
|
||||
ml.setOrigin( mOne, null, null );
|
||||
|
||||
String gTwo = "group2";
|
||||
String aTwo = "artifact2";
|
||||
String vTwo = "2";
|
||||
|
||||
Model mTwo = new Model();
|
||||
|
||||
mOne.setGroupId( gTwo );
|
||||
mOne.setArtifactId( aTwo );
|
||||
mOne.setVersion( vTwo );
|
||||
|
||||
ml.addParent( mTwo, null, null );
|
||||
|
||||
Iterator it = ml.modelIterator();
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( mOne.getId(), ( (Model) it.next() ).getId() );
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( mTwo.getId(), ( (Model) it.next() ).getId() );
|
||||
}
|
||||
|
||||
public void testFileIterator_ShouldAddTwoParentsAndIterateInFIFOOrder()
|
||||
throws IOException
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
File fOne = File.createTempFile( "ModelLineageTest.fileIterator-test.", "" );
|
||||
fOne.deleteOnExit();
|
||||
|
||||
ml.setOrigin( new Model(), fOne, null );
|
||||
|
||||
File fTwo = File.createTempFile( "ModelLineageTest.fileIterator-test.", "" );
|
||||
fTwo.deleteOnExit();
|
||||
|
||||
ml.addParent( new Model(), fTwo, null );
|
||||
|
||||
Iterator it = ml.fileIterator();
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( fOne, (File) it.next() );
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( fTwo, (File) it.next() );
|
||||
}
|
||||
|
||||
public void testArtifactRepositoryListIterator_ShouldAddTwoParentsAndIterateInFIFOOrder()
|
||||
{
|
||||
DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
|
||||
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
ArtifactRepository arOne = new DefaultArtifactRepository( "", "", layout );
|
||||
|
||||
ml.setOrigin( new Model(), null, Collections.singletonList( arOne ) );
|
||||
|
||||
ArtifactRepository arTwo = new DefaultArtifactRepository( "", "", layout );
|
||||
|
||||
ml.addParent( new Model(), null, Collections.singletonList( arTwo ) );
|
||||
|
||||
Iterator it = ml.artifactRepositoryListIterator();
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertSame( arOne, ( (List) it.next() ).get( 0 ) );
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertSame( arTwo, ( (List) it.next() ).get( 0 ) );
|
||||
}
|
||||
|
||||
public void testAddParentAndGetFile_ShouldRetrieveCorrectFileForModel()
|
||||
throws IOException
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
File fOne = File.createTempFile( "ModelLineageTest.fileIterator-test.", "" );
|
||||
fOne.deleteOnExit();
|
||||
|
||||
Model mOne = new Model();
|
||||
|
||||
String gOne = "group";
|
||||
String aOne = "artifact";
|
||||
String vOne = "1";
|
||||
|
||||
mOne.setGroupId( gOne );
|
||||
mOne.setArtifactId( aOne );
|
||||
mOne.setVersion( vOne );
|
||||
|
||||
ml.setOrigin( mOne, fOne, null );
|
||||
|
||||
File fTwo = File.createTempFile( "ModelLineageTest.fileIterator-test.", "" );
|
||||
fTwo.deleteOnExit();
|
||||
|
||||
ml.addParent( new Model(), fTwo, null );
|
||||
|
||||
Model retriever = new Model();
|
||||
|
||||
retriever.setGroupId( gOne );
|
||||
retriever.setArtifactId( aOne );
|
||||
retriever.setVersion( vOne );
|
||||
|
||||
assertEquals( fOne, ml.getFile( retriever ) );
|
||||
}
|
||||
|
||||
public void testAddParentAndGetArtifactRepositories_ShouldRetrieveCorrectRepoListForModel()
|
||||
throws IOException
|
||||
{
|
||||
DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
|
||||
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
Model mOne = new Model();
|
||||
|
||||
String gOne = "group";
|
||||
String aOne = "artifact";
|
||||
String vOne = "1";
|
||||
|
||||
mOne.setGroupId( gOne );
|
||||
mOne.setArtifactId( aOne );
|
||||
mOne.setVersion( vOne );
|
||||
|
||||
ArtifactRepository arOne = new DefaultArtifactRepository( "", "", layout );
|
||||
|
||||
ml.setOrigin( mOne, null, Collections.singletonList( arOne ) );
|
||||
|
||||
ArtifactRepository arTwo = new DefaultArtifactRepository( "", "", layout );
|
||||
|
||||
ml.addParent( new Model(), null, Collections.singletonList( arTwo ) );
|
||||
|
||||
Model retriever = new Model();
|
||||
|
||||
retriever.setGroupId( gOne );
|
||||
retriever.setArtifactId( aOne );
|
||||
retriever.setVersion( vOne );
|
||||
|
||||
assertSame( arOne, ( (List) ml.getArtifactRepositories( retriever ) ).get( 0 ) );
|
||||
}
|
||||
|
||||
public void testSetOriginAndGetOriginatingModel()
|
||||
{
|
||||
Model model = new Model();
|
||||
model.setGroupId( "group" );
|
||||
model.setArtifactId( "artifact" );
|
||||
model.setVersion( "1" );
|
||||
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
ml.setOrigin( model, null, null );
|
||||
|
||||
assertEquals( model.getId(), ml.getOriginatingModel().getId() );
|
||||
}
|
||||
|
||||
public void testSetOriginAndGetOriginatingFile()
|
||||
throws IOException
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
File pomFile = File.createTempFile( "ModelLineage.test.", ".pom" );
|
||||
pomFile.deleteOnExit();
|
||||
|
||||
ml.setOrigin( null, pomFile, null );
|
||||
|
||||
assertEquals( pomFile, ml.getOriginatingPOMFile() );
|
||||
}
|
||||
|
||||
public void testSetOriginAndGetOriginatingArtifactRepositoryList()
|
||||
{
|
||||
DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
|
||||
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
Model mOne = new Model();
|
||||
|
||||
String gOne = "group";
|
||||
String aOne = "artifact";
|
||||
String vOne = "1";
|
||||
|
||||
mOne.setGroupId( gOne );
|
||||
mOne.setArtifactId( aOne );
|
||||
mOne.setVersion( vOne );
|
||||
|
||||
ArtifactRepository arOne = new DefaultArtifactRepository( "", "", layout );
|
||||
|
||||
ml.setOrigin( mOne, null, Collections.singletonList( arOne ) );
|
||||
|
||||
assertSame( arOne, ml.getOriginatingArtifactRepositoryList().get( 0 ) );
|
||||
}
|
||||
|
||||
public void testGetModelsInDescendingOrder_ShouldAddTwoAndRetrieveInLIFOOrder()
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
String gOne = "group1";
|
||||
String aOne = "artifact1";
|
||||
String vOne = "1";
|
||||
|
||||
Model mOne = new Model();
|
||||
|
||||
mOne.setGroupId( gOne );
|
||||
mOne.setArtifactId( aOne );
|
||||
mOne.setVersion( vOne );
|
||||
|
||||
ml.setOrigin( mOne, null, null );
|
||||
|
||||
String gTwo = "group2";
|
||||
String aTwo = "artifact2";
|
||||
String vTwo = "2";
|
||||
|
||||
Model mTwo = new Model();
|
||||
|
||||
mOne.setGroupId( gTwo );
|
||||
mOne.setArtifactId( aTwo );
|
||||
mOne.setVersion( vTwo );
|
||||
|
||||
ml.addParent( mTwo, null, null );
|
||||
|
||||
Iterator it = ml.getModelsInDescendingOrder().iterator();
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( mTwo.getId(), ( (Model) it.next() ).getId() );
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( mOne.getId(), ( (Model) it.next() ).getId() );
|
||||
}
|
||||
|
||||
public void testGetFilesInDescendingOrder_ShouldAddTwoAndRetrieveInLIFOOrder()
|
||||
throws IOException
|
||||
{
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
File fOne = File.createTempFile( "ModelLineageTest.fileIterator-test.", "" );
|
||||
fOne.deleteOnExit();
|
||||
|
||||
ml.setOrigin( new Model(), fOne, null );
|
||||
|
||||
File fTwo = File.createTempFile( "ModelLineageTest.fileIterator-test.", "" );
|
||||
fTwo.deleteOnExit();
|
||||
|
||||
ml.addParent( new Model(), fTwo, null );
|
||||
|
||||
Iterator it = ml.getFilesInDescendingOrder().iterator();
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( fTwo, (File) it.next() );
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertEquals( fOne, (File) it.next() );
|
||||
}
|
||||
|
||||
public void testGetArtifactRepositoryListsInDescendingOrder_ShouldAddTwoAndRetrieveInLIFOOrder()
|
||||
{
|
||||
DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
|
||||
|
||||
ModelLineage ml = newModelLineage();
|
||||
|
||||
ArtifactRepository arOne = new DefaultArtifactRepository( "", "", layout );
|
||||
|
||||
ml.setOrigin( new Model(), null, Collections.singletonList( arOne ) );
|
||||
|
||||
ArtifactRepository arTwo = new DefaultArtifactRepository( "", "", layout );
|
||||
|
||||
ml.addParent( new Model(), null, Collections.singletonList( arTwo ) );
|
||||
|
||||
Iterator it = ml.getArtifactRepositoryListsInDescendingOrder().iterator();
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertSame( arTwo, ( (List) it.next() ).get( 0 ) );
|
||||
|
||||
assertTrue( it.hasNext() );
|
||||
assertSame( arOne, ( (List) it.next() ).get( 0 ) );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,313 @@
|
|||
package org.apache.maven.project.build.model;
|
||||
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.Parent;
|
||||
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
|
||||
import org.apache.maven.project.ProjectBuildingException;
|
||||
import org.codehaus.plexus.PlexusTestCase;
|
||||
import org.codehaus.plexus.logging.Logger;
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
import org.codehaus.plexus.util.IOUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class DefaultModelLineageBuilderTest
|
||||
extends PlexusTestCase
|
||||
{
|
||||
|
||||
private DefaultModelLineageBuilder modelLineageBuilder;
|
||||
|
||||
private ArtifactRepositoryLayout defaultLayout;
|
||||
|
||||
public void setUp()
|
||||
throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
getContainer().getLoggerManager().setThresholds( Logger.LEVEL_DEBUG );
|
||||
|
||||
modelLineageBuilder = (DefaultModelLineageBuilder) lookup( ModelLineageBuilder.ROLE,
|
||||
DefaultModelLineageBuilder.ROLE_HINT );
|
||||
|
||||
defaultLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
|
||||
}
|
||||
|
||||
public void testShouldReadSinglePomWithNoParents()
|
||||
throws IOException, ProjectBuildingException
|
||||
{
|
||||
String groupId = "groupId";
|
||||
String artifactId = "artifactId";
|
||||
String version = "1.0";
|
||||
|
||||
Model model = new Model();
|
||||
|
||||
model.setGroupId( groupId );
|
||||
model.setArtifactId( artifactId );
|
||||
model.setVersion( version );
|
||||
|
||||
File pomFile = File.createTempFile( "DefaultModelLineageBuilder.test.", ".pom" );
|
||||
pomFile.deleteOnExit();
|
||||
|
||||
FileWriter writer = null;
|
||||
try
|
||||
{
|
||||
writer = new FileWriter( pomFile );
|
||||
|
||||
new MavenXpp3Writer().write( writer, model );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( writer );
|
||||
}
|
||||
|
||||
ModelLineage lineage = modelLineageBuilder.buildModelLineage( pomFile, null, null, null );
|
||||
|
||||
assertEquals( 1, lineage.size() );
|
||||
|
||||
assertEquals( model.getId(), lineage.getOriginatingModel().getId() );
|
||||
}
|
||||
|
||||
public void testReadPOMWithTwoAncestorsInLocalRepository()
|
||||
throws IOException, ProjectBuildingException
|
||||
{
|
||||
// 1. create local repository directory
|
||||
File localRepoDirectory = File.createTempFile( "DefaultModelLineageBuilder.localRepository.", "" );
|
||||
|
||||
localRepoDirectory.delete();
|
||||
localRepoDirectory.mkdirs();
|
||||
|
||||
deleteDirOnExit( localRepoDirectory );
|
||||
|
||||
// 2. create and write the ancestor model to the local repo directory
|
||||
|
||||
Model ancestor = createModel( "group", "ancestor", "1" );
|
||||
writeModel( ancestor, new File( localRepoDirectory, "group/ancestor/1/ancestor-1.pom" ) );
|
||||
|
||||
// 3. create the parent model with a parent-ref to the ancestor model
|
||||
|
||||
Model parent = createModel( "group", "parent", "1" );
|
||||
|
||||
Parent parentParent = new Parent();
|
||||
parentParent.setGroupId( "group" );
|
||||
parentParent.setArtifactId( "ancestor" );
|
||||
parentParent.setVersion( "1" );
|
||||
|
||||
parent.setParent( parentParent );
|
||||
|
||||
// 4. write the parent model to the local repo directory
|
||||
writeModel( parent, new File( localRepoDirectory, "group/parent/1/parent-1.pom" ) );
|
||||
|
||||
// 5. create the current pom with a parent-ref on the parent model
|
||||
Model current = createModel( "group", "current", "1" );
|
||||
|
||||
Parent currentParent = new Parent();
|
||||
currentParent.setGroupId( "group" );
|
||||
currentParent.setArtifactId( "parent" );
|
||||
currentParent.setVersion( "1" );
|
||||
|
||||
current.setParent( currentParent );
|
||||
|
||||
// 6. write the current pom somewhere
|
||||
File currentPOM = File.createTempFile( "DefaultModelLineageBuilder.test.", ".pom" );
|
||||
currentPOM.deleteOnExit();
|
||||
|
||||
writeModel( current, currentPOM );
|
||||
|
||||
// 7. build the lineage.
|
||||
ArtifactRepository localRepository = new DefaultArtifactRepository( "local", localRepoDirectory.toURL()
|
||||
.toExternalForm(), defaultLayout );
|
||||
|
||||
ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository,
|
||||
Collections.EMPTY_LIST, null );
|
||||
|
||||
assertEquals( 3, lineage.size() );
|
||||
|
||||
Iterator modelIterator = lineage.modelIterator();
|
||||
|
||||
assertEquals( current.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
assertEquals( parent.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
assertEquals( ancestor.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
}
|
||||
|
||||
public void testReadPOMWithParentInLocalRepositoryAndAncestorInRemoteRepository()
|
||||
throws IOException, ProjectBuildingException
|
||||
{
|
||||
// 1. create local and remote repository directories
|
||||
File localRepoDirectory = File.createTempFile( "DefaultModelLineageBuilder.localRepository.", "" );
|
||||
|
||||
localRepoDirectory.delete();
|
||||
localRepoDirectory.mkdirs();
|
||||
|
||||
deleteDirOnExit( localRepoDirectory );
|
||||
|
||||
File remoteRepoDirectory = File.createTempFile( "DefaultModelLineageBuilder.remoteRepository.", "" );
|
||||
|
||||
remoteRepoDirectory.delete();
|
||||
remoteRepoDirectory.mkdirs();
|
||||
|
||||
deleteDirOnExit( remoteRepoDirectory );
|
||||
|
||||
// 2. create and write the ancestor model to the local repo directory
|
||||
|
||||
Model ancestor = createModel( "group", "ancestor", "1" );
|
||||
writeModel( ancestor, new File( remoteRepoDirectory, "group/ancestor/1/ancestor-1.pom" ) );
|
||||
|
||||
// 3. create the parent model with a parent-ref to the ancestor model
|
||||
|
||||
Model parent = createModel( "group", "parent", "1" );
|
||||
|
||||
Parent parentParent = new Parent();
|
||||
parentParent.setGroupId( "group" );
|
||||
parentParent.setArtifactId( "ancestor" );
|
||||
parentParent.setVersion( "1" );
|
||||
|
||||
parent.setParent( parentParent );
|
||||
|
||||
// 4. write the parent model to the local repo directory
|
||||
writeModel( parent, new File( localRepoDirectory, "group/parent/1/parent-1.pom" ) );
|
||||
|
||||
// 5. create the current pom with a parent-ref on the parent model
|
||||
Model current = createModel( "group", "current", "1" );
|
||||
|
||||
Parent currentParent = new Parent();
|
||||
currentParent.setGroupId( "group" );
|
||||
currentParent.setArtifactId( "parent" );
|
||||
currentParent.setVersion( "1" );
|
||||
|
||||
current.setParent( currentParent );
|
||||
|
||||
// 6. write the current pom somewhere
|
||||
File currentPOM = File.createTempFile( "DefaultModelLineageBuilder.test.", ".pom" );
|
||||
currentPOM.deleteOnExit();
|
||||
|
||||
writeModel( current, currentPOM );
|
||||
|
||||
// 7. build the lineage.
|
||||
ArtifactRepository localRepository = new DefaultArtifactRepository( "local", localRepoDirectory.toURL()
|
||||
.toExternalForm(), defaultLayout );
|
||||
|
||||
ArtifactRepository remoteRepository = new DefaultArtifactRepository( "test", remoteRepoDirectory.toURL()
|
||||
.toExternalForm(), defaultLayout );
|
||||
|
||||
ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository, Collections
|
||||
.singletonList( remoteRepository ), null );
|
||||
|
||||
assertEquals( 3, lineage.size() );
|
||||
|
||||
Iterator modelIterator = lineage.modelIterator();
|
||||
|
||||
assertEquals( current.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
assertEquals( parent.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
assertEquals( ancestor.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
}
|
||||
|
||||
public void testReadPOMWithParentInSiblingDirectoryUsingSpecifiedRelativePathThatIsADirectory()
|
||||
throws IOException, ProjectBuildingException
|
||||
{
|
||||
// 1. create project-root directory.
|
||||
File projectRootDirectory = File.createTempFile( "DefaultModelLineageBuilder.projectRootDir.", "" );
|
||||
|
||||
projectRootDirectory.delete();
|
||||
projectRootDirectory.mkdirs();
|
||||
|
||||
deleteDirOnExit( projectRootDirectory );
|
||||
|
||||
// 2. create dir for parent POM within project root directory.
|
||||
File parentDir = new File( projectRootDirectory, "parent" );
|
||||
parentDir.mkdirs();
|
||||
|
||||
// 2. create dir for child project within project root directory.
|
||||
File childDir = new File( projectRootDirectory, "child" );
|
||||
childDir.mkdirs();
|
||||
|
||||
// 3. create the parent model in the parent-POM directory
|
||||
Model parent = createModel( "group", "parent", "1" );
|
||||
|
||||
writeModel( parent, new File( parentDir, "pom.xml" ) );
|
||||
|
||||
// 5. create the current pom with a parent-ref on the parent model
|
||||
Model current = createModel( "group", "child", "1" );
|
||||
|
||||
Parent currentParent = new Parent();
|
||||
currentParent.setGroupId( "group" );
|
||||
currentParent.setArtifactId( "parent" );
|
||||
currentParent.setVersion( "1" );
|
||||
currentParent.setRelativePath( "../parent" );
|
||||
|
||||
current.setParent( currentParent );
|
||||
|
||||
// 6. write the current POM to the child directory
|
||||
File currentPOM = new File( childDir, "pom.xml" );
|
||||
writeModel( current, currentPOM );
|
||||
|
||||
// 7. build the lineage.
|
||||
ArtifactRepository localRepository = new DefaultArtifactRepository( "local", projectRootDirectory.toURL()
|
||||
.toExternalForm(), defaultLayout );
|
||||
|
||||
ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository, Collections
|
||||
.EMPTY_LIST, null );
|
||||
|
||||
assertEquals( 2, lineage.size() );
|
||||
|
||||
Iterator modelIterator = lineage.modelIterator();
|
||||
|
||||
assertEquals( current.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
assertEquals( parent.getId(), ( (Model) modelIterator.next() ).getId() );
|
||||
}
|
||||
|
||||
private void deleteDirOnExit( final File localRepoDirectory )
|
||||
{
|
||||
Runtime.getRuntime().addShutdownHook( new Thread( new Runnable()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileUtils.deleteDirectory( localRepoDirectory );
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
// ignore this.
|
||||
}
|
||||
}
|
||||
} ) );
|
||||
}
|
||||
|
||||
private void writeModel( Model model, File file )
|
||||
throws IOException
|
||||
{
|
||||
FileWriter writer = null;
|
||||
try
|
||||
{
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
writer = new FileWriter( file );
|
||||
new MavenXpp3Writer().write( writer, model );
|
||||
|
||||
writer.flush();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( writer );
|
||||
}
|
||||
|
||||
System.out.println( "Verifying that: " + file.getAbsolutePath() + " exists: " + file.exists() );
|
||||
}
|
||||
|
||||
private Model createModel( String groupId, String artifactId, String version )
|
||||
{
|
||||
Model model = new Model();
|
||||
model.setGroupId( groupId );
|
||||
model.setArtifactId( artifactId );
|
||||
model.setVersion( version );
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.apache.maven.project.build.model;
|
||||
|
||||
import org.apache.maven.project.build.model.DefaultModelLineage;
|
||||
import org.apache.maven.project.build.model.ModelLineage;
|
||||
|
||||
|
||||
public class DefaultModelLineageTest
|
||||
extends AbstractModelLineageTest
|
||||
{
|
||||
|
||||
protected ModelLineage newModelLineage()
|
||||
{
|
||||
return new DefaultModelLineage();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<project>
|
||||
<parent>
|
||||
<artifactId>p3</artifactId>
|
||||
<artifactId>p4</artifactId>
|
||||
<groupId>maven</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
|
|
@ -42,6 +42,14 @@
|
|||
<role-hint>test</role-hint>
|
||||
<implementation>org.apache.maven.project.TestProjectBuilder</implementation>
|
||||
<requirements>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.build.model.ModelLineageBuilder</role>
|
||||
<role-hint>default</role-hint>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.build.profile.ProfileAdvisor</role>
|
||||
<role-hint>default</role-hint>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.profiles.MavenProfilesBuilder</role>
|
||||
</requirement>
|
||||
|
|
|
@ -42,6 +42,14 @@
|
|||
<role-hint>test</role-hint>
|
||||
<implementation>org.apache.maven.project.TestProjectBuilder</implementation>
|
||||
<requirements>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.build.model.ModelLineageBuilder</role>
|
||||
<role-hint>default</role-hint>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.project.build.profile.ProfileAdvisor</role>
|
||||
<role-hint>default</role-hint>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.profiles.MavenProfilesBuilder</role>
|
||||
</requirement>
|
||||
|
|
Loading…
Reference in New Issue