001package org.apache.maven.project; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.util.ArrayList; 024import java.util.List; 025 026import org.apache.maven.artifact.Artifact; 027import org.apache.maven.artifact.repository.ArtifactRepository; 028import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 029import org.codehaus.plexus.util.FileUtils; 030 031public class DefaultMavenProjectBuilderTest 032 extends AbstractMavenProjectTestCase 033{ 034 035 private List<File> filesToDelete = new ArrayList<>(); 036 037 private File localRepoDir; 038 039 @Override 040 public void setUp() 041 throws Exception 042 { 043 super.setUp(); 044 045 projectBuilder = lookup( ProjectBuilder.class ); 046 047 localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() ); 048 localRepoDir.mkdirs(); 049 050 filesToDelete.add( localRepoDir ); 051 } 052 053 @Override 054 public void tearDown() 055 throws Exception 056 { 057 super.tearDown(); 058 059 if ( !filesToDelete.isEmpty() ) 060 { 061 for ( File file : filesToDelete ) 062 { 063 if ( file.exists() ) 064 { 065 if ( file.isDirectory() ) 066 { 067 FileUtils.deleteDirectory( file ); 068 } 069 else 070 { 071 file.delete(); 072 } 073 } 074 } 075 } 076 } 077 078 protected MavenProject getProject( Artifact pom, boolean allowStub ) 079 throws Exception 080 { 081 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest(); 082 configuration.setLocalRepository( getLocalRepository() ); 083 initRepoSession( configuration ); 084 085 return projectBuilder.build( pom, allowStub, configuration ).getProject(); 086 } 087 088 /** 089 * Check that we can build ok from the middle pom of a (parent,child,grandchild) heirarchy 090 * @throws Exception 091 */ 092 public void testBuildFromMiddlePom() throws Exception 093 { 094 File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml"); 095 File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml"); 096 097 getProject( f1 ); 098 099 // it's the building of the grandchild project, having already cached the child project 100 // (but not the parent project), which causes the problem. 101 getProject( f2 ); 102 } 103 104 public void testDuplicatePluginDefinitionsMerged() 105 throws Exception 106 { 107 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" ); 108 109 MavenProject project = getProject( f1 ); 110 assertEquals( 2, project.getBuildPlugins().get( 0 ).getDependencies().size() ); 111 assertEquals( 2, project.getBuildPlugins().get( 0 ).getExecutions().size() ); 112 assertEquals( "first", project.getBuildPlugins().get( 0 ).getExecutions().get( 0 ).getId() ); 113 } 114 115 public void testBuildStubModelForMissingRemotePom() 116 throws Exception 117 { 118 Artifact pom = repositorySystem.createProjectArtifact( "org.apache.maven.its", "missing", "0.1" ); 119 MavenProject project = getProject( pom, true ); 120 121 assertNotNull( project.getArtifactId() ); 122 123 assertNotNull( project.getRemoteArtifactRepositories() ); 124 assertFalse( project.getRemoteArtifactRepositories().isEmpty() ); 125 126 assertNotNull( project.getPluginArtifactRepositories() ); 127 assertFalse( project.getPluginArtifactRepositories().isEmpty() ); 128 129 assertNull( project.getParent() ); 130 assertNull( project.getParentArtifact() ); 131 132 assertFalse( project.isExecutionRoot() ); 133 } 134 135 @Override 136 protected ArtifactRepository getLocalRepository() 137 throws Exception 138 { 139 ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "default" ); 140 ArtifactRepository r = 141 repositorySystem.createArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(), repoLayout, 142 null, null ); 143 return r; 144 } 145 146 public void xtestLoop() 147 throws Exception 148 { 149 while ( true ) 150 { 151 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" ); 152 getProject( f1 ); 153 } 154 } 155 156 public void testPartialResultUponBadDependencyDeclaration() 157 throws Exception 158 { 159 File pomFile = getTestFile( "src/test/resources/projects/bad-dependency.xml" ); 160 161 try 162 { 163 ProjectBuildingRequest request = newBuildingRequest(); 164 request.setProcessPlugins( false ); 165 request.setResolveDependencies( true ); 166 projectBuilder.build( pomFile, request ); 167 fail( "Project building did not fail despite invalid POM" ); 168 } 169 catch ( ProjectBuildingException e ) 170 { 171 List<ProjectBuildingResult> results = e.getResults(); 172 assertNotNull( results ); 173 assertEquals( 1, results.size() ); 174 ProjectBuildingResult result = results.get( 0 ); 175 assertNotNull( result ); 176 assertNotNull( result.getProject() ); 177 assertEquals( 1, result.getProblems().size() ); 178 assertEquals( 1, result.getProject().getArtifacts().size() ); 179 assertNotNull( result.getDependencyResolutionResult() ); 180 } 181 } 182 183 public void testImportScopePomResolvesFromPropertyBasedRepository() 184 throws Exception 185 { 186 File pomFile = getTestFile( "src/test/resources/projects/import-scope-pom-resolves-from-property-based-repository.xml" ); 187 ProjectBuildingRequest request = newBuildingRequest(); 188 request.setProcessPlugins( false ); 189 request.setResolveDependencies( true ); 190 projectBuilder.build( pomFile, request ); 191 } 192}