diff --git a/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java b/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java index d6c64a9d91..9ba23c213d 100644 --- a/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/build/model/DefaultModelLineageBuilder.java @@ -277,14 +277,10 @@ public class DefaultModelLineageBuilder LinkedHashSet profileRepos = profileAdvisor.getArtifactRepositoriesFromActiveProfiles( profileManager, model.getId() ); - getLogger().debug( "Got the following repos from global profile manager:\n\n" + profileRepos ); - profileRepos.addAll( profileAdvisor.getArtifactRepositoriesFromActiveProfiles( model, projectDir, explicitlyActive, explicitlyInactive ) ); - getLogger().debug( "Got the following repos from all profile managers:\n\n" + profileRepos ); - if ( !profileRepos.isEmpty() ) { repositories.addAll( profileRepos ); diff --git a/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java b/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java index b4d5efa7ac..ebda3f09e8 100644 --- a/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java @@ -25,7 +25,11 @@ import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.context.BuildContextManager; import org.apache.maven.model.Model; import org.apache.maven.model.Parent; +import org.apache.maven.model.Profile; +import org.apache.maven.model.Repository; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.apache.maven.profiles.DefaultProfileManager; +import org.apache.maven.profiles.ProfileManager; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.build.ProjectBuildCache; import org.codehaus.plexus.PlexusTestCase; @@ -37,7 +41,9 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; +import java.util.Set; public class DefaultModelLineageBuilderTest extends PlexusTestCase @@ -47,13 +53,16 @@ public class DefaultModelLineageBuilderTest private ArtifactRepositoryLayout defaultLayout; + private Set toDelete = new HashSet(); + public void setUp() throws Exception { super.setUp(); getContainer().getLoggerManager().setThresholds( Logger.LEVEL_DEBUG ); - modelLineageBuilder = (DefaultModelLineageBuilder) lookup( ModelLineageBuilder.ROLE, + modelLineageBuilder = (DefaultModelLineageBuilder) lookup( + ModelLineageBuilder.ROLE, DefaultModelLineageBuilder.ROLE_HINT ); defaultLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); @@ -66,6 +75,16 @@ public class DefaultModelLineageBuilderTest ctxMgr.clearBuildContext(); super.tearDown(); + + for ( Iterator it = toDelete.iterator(); it.hasNext(); ) + { + File f = (File) it.next(); + + if ( f.exists() ) + { + FileUtils.forceDelete( f ); + } + } } public void testShouldReadSinglePomWithNoParents() @@ -96,7 +115,8 @@ public class DefaultModelLineageBuilderTest IOUtil.close( writer ); } - ModelLineage lineage = modelLineageBuilder.buildModelLineage( pomFile, null, null, null, false ); + ModelLineage lineage = modelLineageBuilder.buildModelLineage( pomFile, null, null, null, + false ); assertEquals( 1, lineage.size() ); @@ -107,7 +127,9 @@ public class DefaultModelLineageBuilderTest throws IOException, ProjectBuildingException { // 1. create local repository directory - File localRepoDirectory = File.createTempFile( "DefaultModelLineageBuilder.localRepository.", "" ); + File localRepoDirectory = File.createTempFile( + "DefaultModelLineageBuilder.localRepository.", + "" ); localRepoDirectory.delete(); localRepoDirectory.mkdirs(); @@ -152,11 +174,13 @@ public class DefaultModelLineageBuilderTest // 7. build the lineage. ArtifactRepository localRepository = new DefaultArtifactRepository( "local", - localRepoDirectory.toURL().toExternalForm(), + localRepoDirectory.toURL() + .toExternalForm(), defaultLayout ); ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository, - Collections.EMPTY_LIST, null, false ); + Collections.EMPTY_LIST, null, + false ); assertEquals( 3, lineage.size() ); @@ -171,7 +195,9 @@ public class DefaultModelLineageBuilderTest throws IOException, ProjectBuildingException { // 1. create local repository directory - File localRepoDirectory = File.createTempFile( "DefaultModelLineageBuilder.localRepository.", "" ); + File localRepoDirectory = File.createTempFile( + "DefaultModelLineageBuilder.localRepository.", + "" ); localRepoDirectory.delete(); localRepoDirectory.mkdirs(); @@ -197,11 +223,13 @@ public class DefaultModelLineageBuilderTest // 7. build the lineage. ArtifactRepository localRepository = new DefaultArtifactRepository( "local", - localRepoDirectory.toURL().toExternalForm(), + localRepoDirectory.toURL() + .toExternalForm(), defaultLayout ); ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository, - Collections.EMPTY_LIST, null, true ); + Collections.EMPTY_LIST, null, + true ); assertEquals( 2, lineage.size() ); @@ -219,14 +247,18 @@ public class DefaultModelLineageBuilderTest throws IOException, ProjectBuildingException { // 1. create local and remote repository directories - File localRepoDirectory = File.createTempFile( "DefaultModelLineageBuilder.localRepository.", "" ); + File localRepoDirectory = File.createTempFile( + "DefaultModelLineageBuilder.localRepository.", + "" ); localRepoDirectory.delete(); localRepoDirectory.mkdirs(); deleteDirOnExit( localRepoDirectory ); - File remoteRepoDirectory = File.createTempFile( "DefaultModelLineageBuilder.remoteRepository.", "" ); + File remoteRepoDirectory = File.createTempFile( + "DefaultModelLineageBuilder.remoteRepository.", + "" ); remoteRepoDirectory.delete(); remoteRepoDirectory.mkdirs(); @@ -271,15 +303,19 @@ public class DefaultModelLineageBuilderTest // 7. build the lineage. ArtifactRepository localRepository = new DefaultArtifactRepository( "local", - localRepoDirectory.toURL().toExternalForm(), + localRepoDirectory.toURL() + .toExternalForm(), defaultLayout ); - ArtifactRepository remoteRepository = new DefaultArtifactRepository( "test", + ArtifactRepository remoteRepository = new DefaultArtifactRepository( + "test", remoteRepoDirectory.toURL() .toExternalForm(), defaultLayout ); - ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository, + ModelLineage lineage = modelLineageBuilder.buildModelLineage( + currentPOM, + localRepository, Collections.singletonList( remoteRepository ), null, false ); @@ -296,7 +332,9 @@ public class DefaultModelLineageBuilderTest throws IOException, ProjectBuildingException { // 1. create project-root directory. - File projectRootDirectory = File.createTempFile( "DefaultModelLineageBuilder.projectRootDir.", "" ); + File projectRootDirectory = File.createTempFile( + "DefaultModelLineageBuilder.projectRootDir.", + "" ); projectRootDirectory.delete(); projectRootDirectory.mkdirs(); @@ -332,13 +370,15 @@ public class DefaultModelLineageBuilderTest writeModel( current, currentPOM ); // 7. build the lineage. - ArtifactRepository localRepository = new DefaultArtifactRepository( "local", + ArtifactRepository localRepository = new DefaultArtifactRepository( + "local", projectRootDirectory.toURL() .toExternalForm(), defaultLayout ); ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository, - Collections.EMPTY_LIST, null, false ); + Collections.EMPTY_LIST, null, + false ); assertEquals( 2, lineage.size() ); @@ -348,25 +388,91 @@ public class DefaultModelLineageBuilderTest assertEquals( parent.getId(), ( (Model) modelIterator.next() ).getId() ); } - private void deleteDirOnExit( final File localRepoDirectory ) + public void testReadPOMWithParentInRepoBroughtInViaSettingsProfile() + throws IOException, ProjectBuildingException { - Runtime.getRuntime().addShutdownHook( new Thread( new Runnable() - { - public void run() - { - try - { - FileUtils.deleteDirectory( localRepoDirectory ); - } - catch ( IOException e ) - { - // ignore this. - } - } - } ) ); + // 1. create project-root directory. + File projectRootDirectory = createTempDir( "projectRootDir" ); + + // 1. create project-root directory. + File repoRootDirectory = createTempDir( "repoRootDir" ); + + File localRepoRootDirectory = createTempDir( "localRepoRootDir" ); + + // 2. create dir for parent POM within project root directory. + File parentDir = new File( repoRootDirectory, "group/parent/1" ); + parentDir.mkdirs(); + + // 3. create the parent model in the parent-POM directory + Model parent = createModel( "group", "parent", "1" ); + + writeModel( parent, new File( parentDir, "parent-1.pom" ) ); + + // 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" ); + + current.setParent( currentParent ); + + // 6. write the current POM to the child directory + File currentPOM = new File( projectRootDirectory, "pom.xml" ); + writeModel( current, currentPOM ); + + // 7. build the lineage. + ArtifactRepository localRepository = new DefaultArtifactRepository( + "local", + localRepoRootDirectory.toURL() + .toExternalForm(), + defaultLayout ); + + Profile profile = new Profile(); + profile.setId( "external" ); + + Repository repository = new Repository(); + repository.setId( "temp" ); + repository.setUrl( repoRootDirectory.toURL().toExternalForm() ); + + profile.addRepository( repository ); + + ProfileManager profileManager = new DefaultProfileManager( getContainer() ); + profileManager.addProfile( profile ); + profileManager.explicitlyActivate( profile.getId() ); + + ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, localRepository, + Collections.EMPTY_LIST, profileManager, + false ); + + assertEquals( 2, lineage.size() ); + + Iterator modelIterator = lineage.modelIterator(); + + assertEquals( current.getId(), ( (Model) modelIterator.next() ).getId() ); + assertEquals( parent.getId(), ( (Model) modelIterator.next() ).getId() ); } - private void writeModel( Model model, File file ) + private File createTempDir( String basename ) + throws IOException + { + File dir = File.createTempFile( "DefaultModelLineageBuilder." + basename + ".", "" ); + + dir.delete(); + dir.mkdirs(); + + deleteDirOnExit( dir ); + return dir; + } + + private void deleteDirOnExit( File f ) + { + toDelete.add( f ); + } + + private void writeModel( Model model, + File file ) throws IOException { FileWriter writer = null; @@ -384,7 +490,8 @@ public class DefaultModelLineageBuilderTest IOUtil.close( writer ); } - System.out.println( "Verifying that: " + file.getAbsolutePath() + " exists: " + file.exists() ); + System.out.println( "Verifying that: " + file.getAbsolutePath() + " exists: " + + file.exists() ); } public void testReadPOMWithParentInOtherLocalFileWithBadRelativePath() @@ -399,7 +506,9 @@ public class DefaultModelLineageBuilderTest // 4. write the parent model to the local repo directory writeModel( parent, parentPOM ); - BuildContextManager buildContextManager = (BuildContextManager) lookup( BuildContextManager.ROLE, "default" ); + BuildContextManager buildContextManager = (BuildContextManager) lookup( + BuildContextManager.ROLE, + "default" ); ProjectBuildCache cache = ProjectBuildCache.read( buildContextManager ); cache.cacheModelFileForModel( parentPOM, parent ); @@ -423,7 +532,8 @@ public class DefaultModelLineageBuilderTest writeModel( current, currentPOM ); // 7. build the lineage. - ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, null, Collections.EMPTY_LIST, null, + ModelLineage lineage = modelLineageBuilder.buildModelLineage( currentPOM, null, + Collections.EMPTY_LIST, null, false ); assertEquals( 2, lineage.size() ); @@ -434,7 +544,9 @@ public class DefaultModelLineageBuilderTest assertEquals( parent.getId(), ( (Model) modelIterator.next() ).getId() ); } - private Model createModel( String groupId, String artifactId, String version ) + private Model createModel( String groupId, + String artifactId, + String version ) { Model model = new Model(); model.setGroupId( groupId );