001 package 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 022 import java.io.File; 023 import java.util.ArrayList; 024 import java.util.Collections; 025 import java.util.List; 026 import java.util.Map; 027 import java.util.Properties; 028 029 import org.apache.maven.AbstractCoreMavenComponentTestCase; 030 import org.apache.maven.artifact.Artifact; 031 import org.apache.maven.artifact.ArtifactUtils; 032 import org.apache.maven.artifact.factory.ArtifactFactory; 033 import org.apache.maven.artifact.repository.ArtifactRepository; 034 import org.apache.maven.execution.DefaultMavenExecutionRequest; 035 import org.apache.maven.execution.DefaultMavenExecutionResult; 036 import org.apache.maven.execution.MavenExecutionRequest; 037 import org.apache.maven.execution.MavenSession; 038 import org.apache.maven.model.Build; 039 import org.apache.maven.model.Dependency; 040 import org.apache.maven.model.Model; 041 import org.apache.maven.plugin.descriptor.MojoDescriptor; 042 import org.apache.maven.plugin.descriptor.PluginDescriptor; 043 import org.apache.maven.project.DuplicateProjectException; 044 import org.apache.maven.project.MavenProject; 045 import org.apache.maven.repository.RepositorySystem; 046 import org.codehaus.plexus.MutablePlexusContainer; 047 import org.codehaus.plexus.PlexusContainer; 048 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; 049 import org.codehaus.plexus.util.dag.CycleDetectedException; 050 051 052 /** 053 * @author Jason van Zyl 054 */ 055 public class PluginParameterExpressionEvaluatorTest 056 extends AbstractCoreMavenComponentTestCase 057 { 058 private static final String FS = System.getProperty( "file.separator" ); 059 060 private RepositorySystem factory; 061 062 public void setUp() 063 throws Exception 064 { 065 super.setUp(); 066 factory = lookup( RepositorySystem.class ); 067 } 068 069 @Override 070 protected void tearDown() 071 throws Exception 072 { 073 factory = null; 074 super.tearDown(); 075 } 076 077 public void testPluginDescriptorExpressionReference() 078 throws Exception 079 { 080 MojoExecution exec = newMojoExecution(); 081 082 MavenSession session = newMavenSession(); 083 084 Object result = new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin}" ); 085 086 System.out.println( "Result: " + result ); 087 088 assertSame( "${plugin} expression does not return plugin descriptor.", 089 exec.getMojoDescriptor().getPluginDescriptor(), 090 result ); 091 } 092 093 public void testPluginArtifactsExpressionReference() 094 throws Exception 095 { 096 MojoExecution exec = newMojoExecution(); 097 098 Artifact depArtifact = createArtifact( "group", "artifact", "1" ); 099 100 List<Artifact> deps = new ArrayList<Artifact>(); 101 deps.add( depArtifact ); 102 103 exec.getMojoDescriptor().getPluginDescriptor().setArtifacts( deps ); 104 105 MavenSession session = newMavenSession(); 106 107 List depResults = (List) new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin.artifacts}" ); 108 109 System.out.println( "Result: " + depResults ); 110 111 assertNotNull( depResults ); 112 assertEquals( 1, depResults.size() ); 113 assertSame( "dependency artifact is wrong.", depArtifact, depResults.get( 0 ) ); 114 } 115 116 public void testPluginArtifactMapExpressionReference() 117 throws Exception 118 { 119 MojoExecution exec = newMojoExecution(); 120 121 Artifact depArtifact = createArtifact( "group", "artifact", "1" ); 122 123 List<Artifact> deps = new ArrayList<Artifact>(); 124 deps.add( depArtifact ); 125 126 exec.getMojoDescriptor().getPluginDescriptor().setArtifacts( deps ); 127 128 MavenSession session = newMavenSession(); 129 130 Map depResults = (Map) new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin.artifactMap}" ); 131 132 System.out.println( "Result: " + depResults ); 133 134 assertNotNull( depResults ); 135 assertEquals( 1, depResults.size() ); 136 assertSame( "dependency artifact is wrong.", 137 depArtifact, 138 depResults.get( ArtifactUtils.versionlessKey( depArtifact ) ) ); 139 } 140 141 public void testPluginArtifactIdExpressionReference() 142 throws Exception 143 { 144 MojoExecution exec = newMojoExecution(); 145 146 MavenSession session = newMavenSession(); 147 148 Object result = new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin.artifactId}" ); 149 150 System.out.println( "Result: " + result ); 151 152 assertSame( "${plugin.artifactId} expression does not return plugin descriptor's artifactId.", 153 exec.getMojoDescriptor().getPluginDescriptor().getArtifactId(), 154 result ); 155 } 156 157 public void testValueExtractionWithAPomValueContainingAPath() 158 throws Exception 159 { 160 String expected = getTestFile( "target/test-classes/target/classes" ).getCanonicalPath(); 161 162 Build build = new Build(); 163 build.setDirectory( expected.substring( 0, expected.length() - "/classes".length() ) ); 164 165 Model model = new Model(); 166 model.setBuild( build ); 167 168 MavenProject project = new MavenProject( model ); 169 project.setFile( new File( "pom.xml" ).getCanonicalFile() ); 170 171 ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project, null, new Properties() ); 172 173 Object value = expressionEvaluator.evaluate( "${project.build.directory}/classes" ); 174 String actual = new File( value.toString() ).getCanonicalPath(); 175 176 assertEquals( expected, actual ); 177 } 178 179 public void testEscapedVariablePassthrough() 180 throws Exception 181 { 182 String var = "${var}"; 183 184 Model model = new Model(); 185 model.setVersion( "1" ); 186 187 MavenProject project = new MavenProject( model ); 188 189 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() ); 190 191 Object value = ee.evaluate( "$" + var ); 192 193 assertEquals( var, value ); 194 } 195 196 public void testEscapedVariablePassthroughInLargerExpression() 197 throws Exception 198 { 199 String var = "${var}"; 200 String key = var + " with version: ${project.version}"; 201 202 Model model = new Model(); 203 model.setVersion( "1" ); 204 205 MavenProject project = new MavenProject( model ); 206 207 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() ); 208 209 Object value = ee.evaluate( "$" + key ); 210 211 assertEquals( "${var} with version: 1", value ); 212 } 213 214 public void testMultipleSubExpressionsInLargerExpression() 215 throws Exception 216 { 217 String key = "${project.artifactId} with version: ${project.version}"; 218 219 Model model = new Model(); 220 model.setArtifactId( "test" ); 221 model.setVersion( "1" ); 222 223 MavenProject project = new MavenProject( model ); 224 225 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() ); 226 227 Object value = ee.evaluate( key ); 228 229 assertEquals( "test with version: 1", value ); 230 } 231 232 public void testMissingPOMPropertyRefInLargerExpression() 233 throws Exception 234 { 235 String expr = "/path/to/someproject-${baseVersion}"; 236 237 MavenProject project = new MavenProject( new Model() ); 238 239 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() ); 240 241 Object value = ee.evaluate( expr ); 242 243 assertEquals( expr, value ); 244 } 245 246 public void testPOMPropertyExtractionWithMissingProject_WithDotNotation() 247 throws Exception 248 { 249 String key = "m2.name"; 250 String checkValue = "value"; 251 252 Properties properties = new Properties(); 253 properties.setProperty( key, checkValue ); 254 255 Model model = new Model(); 256 model.setProperties( properties ); 257 258 MavenProject project = new MavenProject( model ); 259 260 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() ); 261 262 Object value = ee.evaluate( "${" + key + "}" ); 263 264 assertEquals( checkValue, value ); 265 } 266 267 public void testBasedirExtractionWithMissingProject() 268 throws Exception 269 { 270 ExpressionEvaluator ee = createExpressionEvaluator( null, null, new Properties() ); 271 272 Object value = ee.evaluate( "${basedir}" ); 273 274 assertEquals( System.getProperty( "user.dir" ), value ); 275 } 276 277 public void testValueExtractionFromSystemPropertiesWithMissingProject() 278 throws Exception 279 { 280 String sysprop = "PPEET_sysprop1"; 281 282 Properties executionProperties = new Properties(); 283 284 if ( executionProperties.getProperty( sysprop ) == null ) 285 { 286 executionProperties.setProperty( sysprop, "value" ); 287 } 288 289 ExpressionEvaluator ee = createExpressionEvaluator( null, null, executionProperties ); 290 291 Object value = ee.evaluate( "${" + sysprop + "}" ); 292 293 assertEquals( "value", value ); 294 } 295 296 public void testValueExtractionFromSystemPropertiesWithMissingProject_WithDotNotation() 297 throws Exception 298 { 299 String sysprop = "PPEET.sysprop2"; 300 301 Properties executionProperties = new Properties(); 302 303 if ( executionProperties.getProperty( sysprop ) == null ) 304 { 305 executionProperties.setProperty( sysprop, "value" ); 306 } 307 308 ExpressionEvaluator ee = createExpressionEvaluator( null, null, executionProperties ); 309 310 Object value = ee.evaluate( "${" + sysprop + "}" ); 311 312 assertEquals( "value", value ); 313 } 314 315 private static MavenSession createSession( PlexusContainer container, ArtifactRepository repo, Properties properties ) 316 throws CycleDetectedException, DuplicateProjectException 317 { 318 MavenExecutionRequest request = new DefaultMavenExecutionRequest() 319 .setSystemProperties( properties ) 320 .setGoals( Collections.EMPTY_LIST ) 321 .setBaseDirectory( new File( "" ) ) 322 .setLocalRepository( repo ); 323 324 return new MavenSession( container, request, new DefaultMavenExecutionResult(), Collections.EMPTY_LIST ); 325 } 326 327 public void testLocalRepositoryExtraction() 328 throws Exception 329 { 330 ExpressionEvaluator expressionEvaluator = 331 createExpressionEvaluator( createDefaultProject(), null, new Properties() ); 332 Object value = expressionEvaluator.evaluate( "${localRepository}" ); 333 334 assertEquals( "local", ( (ArtifactRepository) value ).getId() ); 335 } 336 337 public void testTwoExpressions() 338 throws Exception 339 { 340 Build build = new Build(); 341 build.setDirectory( "expected-directory" ); 342 build.setFinalName( "expected-finalName" ); 343 344 Model model = new Model(); 345 model.setBuild( build ); 346 347 ExpressionEvaluator expressionEvaluator = 348 createExpressionEvaluator( new MavenProject( model ), null, new Properties() ); 349 350 Object value = expressionEvaluator.evaluate( "${project.build.directory}" + FS + "${project.build.finalName}" ); 351 352 assertEquals( "expected-directory" + File.separatorChar + "expected-finalName", value ); 353 } 354 355 public void testShouldExtractPluginArtifacts() 356 throws Exception 357 { 358 PluginDescriptor pd = new PluginDescriptor(); 359 360 Artifact artifact = createArtifact( "testGroup", "testArtifact", "1.0" ); 361 362 pd.setArtifacts( Collections.singletonList( artifact ) ); 363 364 ExpressionEvaluator ee = createExpressionEvaluator( createDefaultProject(), pd, new Properties() ); 365 366 Object value = ee.evaluate( "${plugin.artifacts}" ); 367 368 assertTrue( value instanceof List ); 369 370 List artifacts = (List) value; 371 372 assertEquals( 1, artifacts.size() ); 373 374 Artifact result = (Artifact) artifacts.get( 0 ); 375 376 assertEquals( "testGroup", result.getGroupId() ); 377 } 378 379 private MavenProject createDefaultProject() 380 { 381 return new MavenProject( new Model() ); 382 } 383 384 private ExpressionEvaluator createExpressionEvaluator( MavenProject project, PluginDescriptor pluginDescriptor, Properties executionProperties ) 385 throws Exception 386 { 387 ArtifactRepository repo = factory.createDefaultLocalRepository(); 388 389 MutablePlexusContainer container = (MutablePlexusContainer) getContainer(); 390 MavenSession session = createSession( container, repo, executionProperties ); 391 session.setCurrentProject( project ); 392 393 MojoDescriptor mojo = new MojoDescriptor(); 394 mojo.setPluginDescriptor( pluginDescriptor ); 395 mojo.setGoal( "goal" ); 396 397 MojoExecution mojoExecution = new MojoExecution( mojo ); 398 399 return new PluginParameterExpressionEvaluator( session, mojoExecution ); 400 } 401 402 protected Artifact createArtifact( String groupId, String artifactId, String version ) 403 throws Exception 404 { 405 Dependency dependency = new Dependency(); 406 dependency.setGroupId( groupId ); 407 dependency.setArtifactId( artifactId ); 408 dependency.setVersion( version ); 409 dependency.setType( "jar" ); 410 dependency.setScope( "compile" ); 411 412 return factory.createDependencyArtifact( dependency ); 413 } 414 415 private MojoExecution newMojoExecution() 416 { 417 PluginDescriptor pd = new PluginDescriptor(); 418 pd.setArtifactId( "my-plugin" ); 419 pd.setGroupId( "org.myco.plugins" ); 420 pd.setVersion( "1" ); 421 422 MojoDescriptor md = new MojoDescriptor(); 423 md.setPluginDescriptor( pd ); 424 425 pd.addComponentDescriptor( md ); 426 427 return new MojoExecution( md ); 428 } 429 430 private MavenSession newMavenSession() 431 throws Exception 432 { 433 return createMavenSession( null ); 434 } 435 436 @Override 437 protected String getProjectsDirectory() 438 { 439 // TODO Auto-generated method stub 440 return null; 441 } 442 443 }