001package org.apache.maven.plugin; 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.util.List; 023 024import org.apache.maven.AbstractCoreMavenComponentTestCase; 025import org.apache.maven.artifact.Artifact; 026import org.apache.maven.artifact.repository.DefaultRepositoryRequest; 027import org.apache.maven.artifact.repository.RepositoryRequest; 028import org.apache.maven.execution.MavenSession; 029import org.apache.maven.model.Plugin; 030import org.apache.maven.plugin.descriptor.MojoDescriptor; 031import org.apache.maven.plugin.descriptor.PluginDescriptor; 032import org.apache.maven.project.MavenProject; 033import org.codehaus.plexus.classworlds.realm.ClassRealm; 034import org.codehaus.plexus.component.annotations.Requirement; 035import org.codehaus.plexus.component.repository.ComponentDescriptor; 036 037public class PluginManagerTest 038 extends AbstractCoreMavenComponentTestCase 039{ 040 @Requirement 041 private DefaultBuildPluginManager pluginManager; 042 043 protected void setUp() 044 throws Exception 045 { 046 super.setUp(); 047 pluginManager = (DefaultBuildPluginManager) lookup( BuildPluginManager.class ); 048 } 049 050 @Override 051 protected void tearDown() 052 throws Exception 053 { 054 pluginManager = null; 055 super.tearDown(); 056 } 057 058 protected String getProjectsDirectory() 059 { 060 return "src/test/projects/plugin-manager"; 061 } 062 063 public void testPluginLoading() 064 throws Exception 065 { 066 MavenSession session = createMavenSession( null ); 067 Plugin plugin = new Plugin(); 068 plugin.setGroupId( "org.apache.maven.its.plugins" ); 069 plugin.setArtifactId( "maven-it-plugin" ); 070 plugin.setVersion( "0.1" ); 071 PluginDescriptor pluginDescriptor = 072 pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(), 073 session.getRepositorySession() ); 074 assertNotNull( pluginDescriptor ); 075 } 076 077 public void testMojoDescriptorRetrieval() 078 throws Exception 079 { 080 MavenSession session = createMavenSession( null ); 081 String goal = "it"; 082 Plugin plugin = new Plugin(); 083 plugin.setGroupId( "org.apache.maven.its.plugins" ); 084 plugin.setArtifactId( "maven-it-plugin" ); 085 plugin.setVersion( "0.1" ); 086 087 MojoDescriptor mojoDescriptor = 088 pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject().getRemotePluginRepositories(), 089 session.getRepositorySession() ); 090 assertNotNull( mojoDescriptor ); 091 assertEquals( goal, mojoDescriptor.getGoal() ); 092 // igorf: plugin realm comes later 093 // assertNotNull( mojoDescriptor.getRealm() ); 094 095 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); 096 assertNotNull( pluginDescriptor ); 097 assertEquals( "org.apache.maven.its.plugins", pluginDescriptor.getGroupId() ); 098 assertEquals( "maven-it-plugin", pluginDescriptor.getArtifactId() ); 099 assertEquals( "0.1", pluginDescriptor.getVersion() ); 100 } 101 102 // ----------------------------------------------------------------------------------------------- 103 // Tests which exercise the lifecycle executor when it is dealing with individual goals. 104 // ----------------------------------------------------------------------------------------------- 105 106 //TODO: These two tests display a lack of symmetry with respect to the input which is a free form string and the 107 // mojo descriptor which comes back. All the free form parsing needs to be done somewhere else, this is 108 // really the function of the CLI, and then the pre-processing of that output still needs to be fed into 109 // a hinting process which helps flesh out the full specification of the plugin. The plugin manager should 110 // only deal in concrete terms -- all version finding mumbo jumbo is a customization to base functionality 111 // the plugin manager provides. 112 113 public void testRemoteResourcesPlugin() 114 throws Exception 115 { 116 //TODO: turn an equivalent back on when the RR plugin is released. 117 118 /* 119 120 This will not work until the RR plugin is released to get rid of the binding to the reporting exception which is a mistake. 121 122 This happpens after removing the reporting API from the core: 123 124 java.lang.NoClassDefFoundError: org/apache/maven/reporting/MavenReportException 125 126 MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) ); 127 String goal = "process"; 128 129 Plugin plugin = new Plugin(); 130 plugin.setGroupId( "org.apache.maven.plugins" ); 131 plugin.setArtifactId( "maven-remote-resources-plugin" ); 132 plugin.setVersion( "1.0-beta-2" ); 133 134 MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject(), session.getLocalRepository() ); 135 assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-remote-resources-plugin", "1.0-beta-2" ); 136 MojoExecution mojoExecution = new MojoExecution( mojoDescriptor ); 137 pluginManager.executeMojo( session, mojoExecution ); 138 */ 139 } 140 141 //TODO: this will be the basis of the customizable lifecycle execution so need to figure this out quickly. 142 public void testSurefirePlugin() 143 throws Exception 144 { 145 /* 146 MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) ); 147 String goal = "test"; 148 149 Plugin plugin = new Plugin(); 150 plugin.setGroupId( "org.apache.maven.plugins" ); 151 plugin.setArtifactId( "maven-surefire-plugin" ); 152 plugin.setVersion( "2.4.2" ); 153 154 // The project has already been fully interpolated so getting the raw mojoDescriptor is not going to have the processes configuration. 155 MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), session.getCurrentProject().getPluginArtifactRepositories() ); 156 assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-surefire-plugin", "2.4.2" ); 157 158 System.out.println( session.getCurrentProject().getBuild().getPluginsAsMap() ); 159 160 Xpp3Dom configuration = (Xpp3Dom) session.getCurrentProject().getBuild().getPluginsAsMap().get( plugin.getKey() ).getExecutions().get( 0 ).getConfiguration(); 161 MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, configuration ); 162 pluginManager.executeMojo( session, mojoExecution ); 163 */ 164 } 165 166 public void testMojoConfigurationIsMergedCorrectly() 167 throws Exception 168 { 169 } 170 171 /** 172 * The case where the user wants to specify an alternate version of the underlying tool. Common case 173 * is in the Antlr plugin which comes bundled with a version of Antlr but the user often times needs 174 * to use a specific version. We need to make sure the version that they specify takes precedence. 175 */ 176 public void testMojoWhereInternallyStatedDependencyIsOverriddenByProject() 177 throws Exception 178 { 179 } 180 181 /** 182 * The case where you have a plugin in the current build that you want to be used on projects in 183 * the current build. 184 */ 185 public void testMojoThatIsPresentInTheCurrentBuild() 186 throws Exception 187 { 188 } 189 190 /** 191 * This is the case where the Mojo wants to execute on every project and then do something at the end 192 * with the results of each project. 193 */ 194 public void testAggregatorMojo() 195 throws Exception 196 { 197 } 198 199 /** 200 * This is the case where a Mojo needs the lifecycle run to a certain phase before it can do 201 * anything useful. 202 */ 203 public void testMojoThatRequiresExecutionToAGivenPhaseBeforeExecutingItself() 204 throws Exception 205 { 206 } 207 208 // test that mojo which does not require dependency resolution trigger no downloading of dependencies 209 210 // test interpolation of basedir values in mojo configuration 211 212 // test a build where projects use different versions of the same plugin 213 214 public void testThatPluginDependencyThatHasSystemScopeIsResolved() 215 throws Exception 216 { 217 /* 218 File systemPath = new File( getBasedir(), "pom.xml" ); 219 220 Plugin plugin = new PluginBuilder( "org.apache.maven", "project-test", "1.0" ) 221 .addDependency( "org.apache.maven", "system-dependency", "1.0", Artifact.SCOPE_SYSTEM, systemPath.getAbsolutePath() ) 222 .get(); 223 224 MavenProject pluginProject = new ProjectBuilder( "org.apache.maven", "project-test", "1.0" ) 225 .addPlugin( plugin ) 226 .addDependency( "junit", "junit", "3.8.1", Artifact.SCOPE_COMPILE ) 227 .get(); 228 229 // i'm making this artifact which is assumed to come from a pom in the metadata processor, then it tries to create a POM artifact 230 // and parse it for the dependencies and it blows up. 231 // 232 // we need to pass this through as is so it doesn't get parsed again. 233 Artifact pluginArtifact = new ProjectArtifact( pluginProject ); 234 235 Set<Artifact> artifacts = pluginManager.getPluginArtifacts( pluginArtifact, plugin, getLocalRepository(), getPluginArtifactRepositories() ); 236 System.out.println( artifacts ); 237 */ 238 239 MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) ); 240 MavenProject project = session.getCurrentProject(); 241 Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" ); 242 243 RepositoryRequest repositoryRequest = new DefaultRepositoryRequest(); 244 repositoryRequest.setLocalRepository( getLocalRepository() ); 245 repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() ); 246 247 PluginDescriptor pluginDescriptor = 248 pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(), 249 session.getRepositorySession() ); 250 pluginManager.getPluginRealm( session, pluginDescriptor ); 251 List<Artifact> artifacts = pluginDescriptor.getArtifacts(); 252 253 for ( Artifact a : artifacts ) 254 { 255 if ( a.getGroupId().equals( "org.apache.maven.its.mng3586" ) && a.getArtifactId().equals( "tools" ) ) 256 { 257 // The system scoped dependencies will be present in the classloader for the plugin 258 return; 259 } 260 } 261 262 fail( "Can't find the system scoped dependency in the plugin artifacts." ); 263 } 264 265 // ----------------------------------------------------------------------------------------------- 266 // Testing help 267 // ----------------------------------------------------------------------------------------------- 268 269 protected void assertPluginDescriptor( MojoDescriptor mojoDescriptor, String groupId, String artifactId, String version ) 270 { 271 assertNotNull( mojoDescriptor ); 272 PluginDescriptor pd = mojoDescriptor.getPluginDescriptor(); 273 assertNotNull( pd ); 274 assertEquals( groupId, pd.getGroupId() ); 275 assertEquals( artifactId, pd.getArtifactId() ); 276 assertEquals( version, pd.getVersion() ); 277 } 278 279 public void testPluginRealmCache() 280 throws Exception 281 { 282 RepositoryRequest repositoryRequest = new DefaultRepositoryRequest(); 283 repositoryRequest.setLocalRepository( getLocalRepository() ); 284 repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() ); 285 286 // prime realm cache 287 MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) ); 288 MavenProject project = session.getCurrentProject(); 289 Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" ); 290 291 PluginDescriptor pluginDescriptor = 292 pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(), 293 session.getRepositorySession() ); 294 pluginManager.getPluginRealm( session, pluginDescriptor ); 295 296 assertEquals( 1, pluginDescriptor.getDependencies().size() ); 297 298 for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() ) 299 { 300 assertNotNull( descriptor.getRealm() ); 301 assertNotNull( descriptor.getImplementationClass() ); 302 } 303 304 // reload plugin realm from cache 305 session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) ); 306 project = session.getCurrentProject(); 307 plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" ); 308 309 pluginDescriptor = 310 pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(), 311 session.getRepositorySession() ); 312 pluginManager.getPluginRealm( session, pluginDescriptor ); 313 314 assertEquals( 1, pluginDescriptor.getDependencies().size() ); 315 316 for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() ) 317 { 318 assertNotNull( descriptor.getRealm() ); 319 assertNotNull( descriptor.getImplementationClass() ); 320 } 321 } 322 323 public void testBuildExtensionsPluginLoading() 324 throws Exception 325 { 326 RepositoryRequest repositoryRequest = new DefaultRepositoryRequest(); 327 repositoryRequest.setLocalRepository( getLocalRepository() ); 328 repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() ); 329 330 // prime realm cache 331 MavenSession session = createMavenSession( getProject( "project-with-build-extensions-plugin" ) ); 332 MavenProject project = session.getCurrentProject(); 333 Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" ); 334 335 PluginDescriptor pluginDescriptor = 336 pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(), 337 session.getRepositorySession() ); 338 ClassRealm pluginRealm = pluginManager.getPluginRealm( session, pluginDescriptor ); 339 340 assertEquals(pluginRealm, pluginDescriptor.getComponents().get(0).getRealm()); 341 } 342}