001 package org.apache.maven.project; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license 005 * agreements. See the NOTICE file distributed with this work for additional information regarding 006 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance with the License. You may obtain a 008 * copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed under the License 013 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 014 * or implied. See the License for the specific language governing permissions and limitations under 015 * the License. 016 */ 017 018 import java.io.File; 019 import java.io.FileNotFoundException; 020 import java.net.URI; 021 import java.net.URISyntaxException; 022 import java.net.URL; 023 import java.util.Arrays; 024 025 import org.apache.maven.artifact.repository.ArtifactRepository; 026 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 027 import org.apache.maven.model.building.ModelBuildingException; 028 import org.apache.maven.model.building.ModelProblem; 029 import org.apache.maven.repository.RepositorySystem; 030 import org.apache.maven.repository.internal.MavenRepositorySystemSession; 031 import org.codehaus.plexus.PlexusTestCase; 032 033 /** 034 * @author Jason van Zyl 035 */ 036 public abstract class AbstractMavenProjectTestCase 037 extends PlexusTestCase 038 { 039 protected ProjectBuilder projectBuilder; 040 041 protected RepositorySystem repositorySystem; 042 043 protected void setUp() 044 throws Exception 045 { 046 super.setUp(); 047 048 if ( getContainer().hasComponent( ProjectBuilder.class, "test" ) ) 049 { 050 projectBuilder = lookup( ProjectBuilder.class, "test" ); 051 } 052 else 053 { 054 // default over to the main project builder... 055 projectBuilder = lookup( ProjectBuilder.class ); 056 } 057 058 repositorySystem = lookup( RepositorySystem.class ); 059 } 060 061 @Override 062 protected void tearDown() 063 throws Exception 064 { 065 projectBuilder = null; 066 067 super.tearDown(); 068 } 069 070 protected ProjectBuilder getProjectBuilder() 071 { 072 return projectBuilder; 073 } 074 075 @Override 076 protected String getCustomConfigurationName() 077 { 078 String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml"; 079 System.out.println( name ); 080 return name; 081 } 082 083 // ---------------------------------------------------------------------- 084 // Local repository 085 // ---------------------------------------------------------------------- 086 087 protected File getLocalRepositoryPath() 088 throws FileNotFoundException, URISyntaxException 089 { 090 File markerFile = getFileForClasspathResource( "local-repo/marker.txt" ); 091 092 return markerFile.getAbsoluteFile().getParentFile(); 093 } 094 095 protected static File getFileForClasspathResource( String resource ) 096 throws FileNotFoundException 097 { 098 ClassLoader cloader = Thread.currentThread().getContextClassLoader(); 099 100 URL resourceUrl = cloader.getResource( resource ); 101 102 if ( resourceUrl == null ) 103 { 104 throw new FileNotFoundException( "Unable to find: " + resource ); 105 } 106 107 return new File( URI.create( resourceUrl.toString().replaceAll( " ", "%20" ) ) ); 108 } 109 110 protected ArtifactRepository getLocalRepository() 111 throws Exception 112 { 113 ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "legacy" ); 114 115 ArtifactRepository r = repositorySystem.createArtifactRepository( "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null ); 116 117 return r; 118 } 119 120 // ---------------------------------------------------------------------- 121 // Project building 122 // ---------------------------------------------------------------------- 123 124 protected MavenProject getProjectWithDependencies( File pom ) 125 throws Exception 126 { 127 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest(); 128 configuration.setLocalRepository( getLocalRepository() ); 129 configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) ); 130 configuration.setProcessPlugins( false ); 131 configuration.setResolveDependencies( true ); 132 initRepoSession( configuration ); 133 134 try 135 { 136 return projectBuilder.build( pom, configuration ).getProject(); 137 } 138 catch ( Exception e ) 139 { 140 Throwable cause = e.getCause(); 141 if ( cause instanceof ModelBuildingException ) 142 { 143 String message = "In: " + pom + "\n\n"; 144 for ( ModelProblem problem : ( (ModelBuildingException) cause ).getProblems() ) 145 { 146 message += problem + "\n"; 147 } 148 System.out.println( message ); 149 fail( message ); 150 } 151 152 throw e; 153 } 154 } 155 156 protected MavenProject getProject( File pom ) 157 throws Exception 158 { 159 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest(); 160 configuration.setLocalRepository( getLocalRepository() ); 161 initRepoSession( configuration ); 162 163 return projectBuilder.build( pom, configuration ).getProject(); 164 } 165 166 protected void initRepoSession( ProjectBuildingRequest request ) 167 { 168 File localRepo = new File( request.getLocalRepository().getBasedir() ); 169 MavenRepositorySystemSession session = new MavenRepositorySystemSession(); 170 session.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) ); 171 request.setRepositorySession( session ); 172 } 173 174 }