1 package org.apache.maven.project.injection; 2 3 18 19 import org.apache.maven.model.Build; 20 import org.apache.maven.model.BuildBase; 21 import org.apache.maven.model.ConfigurationContainer; 22 import org.apache.maven.model.Dependency; 23 import org.apache.maven.model.DependencyManagement; 24 import org.apache.maven.model.DistributionManagement; 25 import org.apache.maven.model.Model; 26 import org.apache.maven.model.Plugin; 27 import org.apache.maven.model.PluginContainer; 28 import org.apache.maven.model.PluginExecution; 29 import org.apache.maven.model.PluginManagement; 30 import org.apache.maven.model.Profile; 31 import org.apache.maven.model.ReportPlugin; 32 import org.apache.maven.model.ReportSet; 33 import org.apache.maven.model.Reporting; 34 import org.apache.maven.project.ModelUtils; 35 import org.codehaus.plexus.util.StringUtils; 36 import org.codehaus.plexus.util.xml.Xpp3Dom; 37 38 import java.util.ArrayList ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Map ; 43 import java.util.Properties ; 44 import java.util.TreeMap ; 45 46 60 public class DefaultProfileInjector 61 implements ProfileInjector 62 { 63 64 public void inject( Profile profile, Model model ) 65 { 66 injectDependencies( profile, model ); 67 68 injectModules( profile, model ); 69 70 model.setRepositories( ModelUtils.mergeRepositoryLists( profile.getRepositories(), model.getRepositories() ) ); 71 model.setPluginRepositories( ModelUtils.mergeRepositoryLists( profile.getPluginRepositories(), model 72 .getPluginRepositories() ) ); 73 74 injectReporting( profile, model ); 75 76 injectDependencyManagement( profile, model ); 77 78 injectDistributionManagement( profile, model ); 79 80 injectBuild( profile, model ); 81 82 Properties props = new Properties (); 83 props.putAll( model.getProperties() ); 84 props.putAll( profile.getProperties() ); 85 86 model.setProperties( props ); 87 } 88 89 private void injectBuild( Profile profile, Model model ) 90 { 91 BuildBase profileBuild = profile.getBuild(); 92 Build modelBuild = model.getBuild(); 93 94 if ( profileBuild != null ) 96 { 97 if ( modelBuild == null ) 98 { 99 modelBuild = new Build(); 100 model.setBuild( modelBuild ); 101 } 102 103 if ( profileBuild.getDirectory() != null ) 104 { 105 modelBuild.setDirectory( profileBuild.getDirectory() ); 106 } 107 108 if ( profileBuild.getDefaultGoal() != null ) 109 { 110 modelBuild.setDefaultGoal( profileBuild.getDefaultGoal() ); 111 } 112 113 if ( profileBuild.getFinalName() != null ) 114 { 115 modelBuild.setFinalName( profileBuild.getFinalName() ); 116 } 117 118 ModelUtils.mergeFilterLists( modelBuild.getFilters(), profileBuild.getFilters() ); 119 ModelUtils.mergeResourceLists( modelBuild.getResources(), profileBuild.getResources() ); 120 ModelUtils.mergeResourceLists( modelBuild.getTestResources(), profileBuild.getTestResources() ); 121 122 injectPlugins( profileBuild, modelBuild ); 123 124 PluginManagement profilePM = profileBuild.getPluginManagement(); 126 PluginManagement modelPM = modelBuild.getPluginManagement(); 127 128 if ( modelPM == null ) 129 { 130 modelBuild.setPluginManagement( profilePM ); 131 } 132 else 133 { 134 injectPlugins( profilePM, modelPM ); 135 } 136 } 137 } 138 139 private void injectPlugins( PluginContainer profileContainer, PluginContainer modelContainer ) 140 { 141 if ( profileContainer == null || modelContainer == null ) 142 { 143 return; 145 } 146 147 List modelPlugins = modelContainer.getPlugins(); 148 149 if ( modelPlugins == null ) 150 { 151 modelContainer.setPlugins( profileContainer.getPlugins() ); 152 } 153 else if ( profileContainer.getPlugins() != null ) 154 { 155 Map mergedPlugins = new TreeMap (); 156 157 Map profilePlugins = profileContainer.getPluginsAsMap(); 158 159 for ( Iterator it = modelPlugins.iterator(); it.hasNext(); ) 160 { 161 Plugin modelPlugin = (Plugin) it.next(); 162 163 Plugin mergedPlugin = modelPlugin; 164 165 Plugin profilePlugin = (Plugin) profilePlugins.get( modelPlugin.getKey() ); 166 167 if ( profilePlugin != null ) 168 { 169 mergedPlugin = modelPlugin; 170 171 injectPluginDefinition( profilePlugin, modelPlugin ); 172 } 173 174 mergedPlugins.put( mergedPlugin.getKey(), mergedPlugin ); 175 } 176 177 for ( Iterator it = profilePlugins.values().iterator(); it.hasNext(); ) 178 { 179 Plugin profilePlugin = (Plugin) it.next(); 180 181 if ( !mergedPlugins.containsKey( profilePlugin.getKey() ) ) 182 { 183 mergedPlugins.put( profilePlugin.getKey(), profilePlugin ); 184 } 185 } 186 187 modelContainer.setPlugins( new ArrayList ( mergedPlugins.values() ) ); 188 189 modelContainer.flushPluginMap(); 190 } 191 } 192 193 private void injectPluginDefinition( Plugin profilePlugin, Plugin modelPlugin ) 194 { 195 if ( profilePlugin == null || modelPlugin == null ) 196 { 197 return; 199 } 200 201 if ( profilePlugin.isExtensions() ) 202 { 203 modelPlugin.setExtensions( true ); 204 } 205 206 if ( profilePlugin.getVersion() != null ) 207 { 208 modelPlugin.setVersion( profilePlugin.getVersion() ); 209 } 210 211 injectConfigurationContainer( profilePlugin, modelPlugin ); 213 214 List modelExecutions = modelPlugin.getExecutions(); 216 217 if ( modelExecutions == null || modelExecutions.isEmpty() ) 218 { 219 modelPlugin.setExecutions( profilePlugin.getExecutions() ); 220 } 221 else 222 { 223 Map executions = new TreeMap (); 224 225 Map profileExecutions = profilePlugin.getExecutionsAsMap(); 226 227 for ( Iterator it = modelExecutions.iterator(); it.hasNext(); ) 228 { 229 PluginExecution modelExecution = (PluginExecution) it.next(); 230 231 PluginExecution profileExecution = (PluginExecution) profileExecutions.get( modelExecution.getId() ); 232 233 if ( profileExecution != null ) 234 { 235 injectConfigurationContainer( profileExecution, modelExecution ); 236 237 if ( profileExecution.getPhase() != null ) 238 { 239 modelExecution.setPhase( profileExecution.getPhase() ); 240 } 241 242 List profileGoals = profileExecution.getGoals(); 243 List modelGoals = modelExecution.getGoals(); 244 245 List goals = new ArrayList (); 246 247 if ( modelGoals != null && !modelGoals.isEmpty() ) 248 { 249 goals.addAll( modelGoals ); 250 } 251 252 if ( profileGoals != null ) 253 { 254 for ( Iterator goalIterator = profileGoals.iterator(); goalIterator.hasNext(); ) 255 { 256 String goal = (String ) goalIterator.next(); 257 258 if ( !goals.contains( goal ) ) 259 { 260 goals.add( goal ); 261 } 262 } 263 } 264 265 modelExecution.setGoals( goals ); 266 } 267 268 executions.put( modelExecution.getId(), modelExecution ); 269 } 270 271 for ( Iterator it = profileExecutions.entrySet().iterator(); it.hasNext(); ) 272 { 273 Map.Entry entry = (Map.Entry ) it.next(); 274 275 String id = (String ) entry.getKey(); 276 277 if ( !executions.containsKey( id ) ) 278 { 279 executions.put( id, entry.getValue() ); 280 } 281 } 282 283 modelPlugin.setExecutions( new ArrayList ( executions.values() ) ); 284 285 modelPlugin.flushExecutionMap(); 286 } 287 288 } 289 290 private void injectConfigurationContainer( ConfigurationContainer profileContainer, 291 ConfigurationContainer modelContainer ) 292 { 293 Xpp3Dom configuration = (Xpp3Dom) profileContainer.getConfiguration(); 294 Xpp3Dom parentConfiguration = (Xpp3Dom) modelContainer.getConfiguration(); 295 296 configuration = Xpp3Dom.mergeXpp3Dom( configuration, parentConfiguration ); 297 298 modelContainer.setConfiguration( configuration ); 299 } 300 301 305 private void injectModules( Profile profile, Model model ) 306 { 307 List modules = new ArrayList (); 308 309 List modelModules = model.getModules(); 310 311 if ( modelModules != null && !modelModules.isEmpty() ) 312 { 313 modules.addAll( modelModules ); 314 } 315 316 List profileModules = profile.getModules(); 317 318 if ( profileModules != null ) 319 { 320 for ( Iterator it = profileModules.iterator(); it.hasNext(); ) 321 { 322 String module = (String ) it.next(); 323 324 if ( !modules.contains( module ) ) 325 { 326 modules.add( module ); 327 } 328 } 329 } 330 331 model.setModules( modules ); 332 } 333 334 private void injectDistributionManagement( Profile profile, Model model ) 335 { 336 DistributionManagement pDistMgmt = profile.getDistributionManagement(); 337 DistributionManagement mDistMgmt = model.getDistributionManagement(); 338 339 if ( mDistMgmt == null ) 340 { 341 model.setDistributionManagement( pDistMgmt ); 342 } 343 else if ( pDistMgmt != null ) 344 { 345 if ( pDistMgmt.getRepository() != null ) 346 { 347 mDistMgmt.setRepository( pDistMgmt.getRepository() ); 348 } 349 350 if ( pDistMgmt.getSnapshotRepository() != null ) 351 { 352 mDistMgmt.setSnapshotRepository( pDistMgmt.getSnapshotRepository() ); 353 } 354 355 if ( StringUtils.isNotEmpty( pDistMgmt.getDownloadUrl() ) ) 356 { 357 mDistMgmt.setDownloadUrl( pDistMgmt.getDownloadUrl() ); 358 } 359 360 if ( pDistMgmt.getRelocation() != null ) 361 { 362 mDistMgmt.setRelocation( pDistMgmt.getRelocation() ); 363 } 364 365 if ( pDistMgmt.getSite() != null ) 366 { 367 mDistMgmt.setSite( pDistMgmt.getSite() ); 368 } 369 370 } 372 } 373 374 private void injectDependencyManagement( Profile profile, Model model ) 375 { 376 DependencyManagement modelDepMgmt = model.getDependencyManagement(); 377 378 DependencyManagement profileDepMgmt = profile.getDependencyManagement(); 379 380 if ( profileDepMgmt != null ) 381 { 382 if ( modelDepMgmt == null ) 383 { 384 model.setDependencyManagement( profileDepMgmt ); 385 } 386 else 387 { 388 Map depsMap = new HashMap (); 389 390 List deps = modelDepMgmt.getDependencies(); 391 392 if ( deps != null ) 393 { 394 for ( Iterator it = deps.iterator(); it.hasNext(); ) 395 { 396 Dependency dependency = (Dependency) it.next(); 397 depsMap.put( dependency.getManagementKey(), dependency ); 398 } 399 } 400 401 deps = profileDepMgmt.getDependencies(); 402 403 if ( deps != null ) 404 { 405 for ( Iterator it = deps.iterator(); it.hasNext(); ) 406 { 407 Dependency dependency = (Dependency) it.next(); 408 depsMap.put( dependency.getManagementKey(), dependency ); 409 } 410 } 411 412 modelDepMgmt.setDependencies( new ArrayList ( depsMap.values() ) ); 413 } 414 } 415 } 416 417 private void injectReporting( Profile profile, Model model ) 418 { 419 Reporting profileReporting = profile.getReporting(); 421 Reporting modelReporting = model.getReporting(); 422 423 if ( profileReporting != null ) 424 { 425 if ( modelReporting == null ) 426 { 427 model.setReporting( profileReporting ); 428 } 429 else 430 { 431 if ( StringUtils.isEmpty( modelReporting.getOutputDirectory() ) ) 432 { 433 modelReporting.setOutputDirectory( profileReporting.getOutputDirectory() ); 434 } 435 436 Map mergedReportPlugins = new HashMap (); 437 438 Map profileReportersByKey = profileReporting.getReportPluginsAsMap(); 439 440 List modelReportPlugins = modelReporting.getPlugins(); 441 442 if ( modelReportPlugins != null ) 443 { 444 for ( Iterator it = modelReportPlugins.iterator(); it.hasNext(); ) 445 { 446 ReportPlugin modelReportPlugin = (ReportPlugin) it.next(); 447 448 String inherited = modelReportPlugin.getInherited(); 449 450 if ( StringUtils.isEmpty( inherited ) || Boolean.valueOf( inherited ).booleanValue() ) 451 { 452 ReportPlugin profileReportPlugin = (ReportPlugin) profileReportersByKey 453 .get( modelReportPlugin.getKey() ); 454 455 ReportPlugin mergedReportPlugin = modelReportPlugin; 456 457 if ( profileReportPlugin != null ) 458 { 459 mergedReportPlugin = profileReportPlugin; 460 461 mergeReportPlugins( profileReportPlugin, modelReportPlugin ); 462 } 463 else if ( StringUtils.isEmpty( inherited ) ) 464 { 465 mergedReportPlugin.unsetInheritanceApplied(); 466 } 467 468 mergedReportPlugins.put( mergedReportPlugin.getKey(), mergedReportPlugin ); 469 } 470 } 471 } 472 473 for ( Iterator it = profileReportersByKey.entrySet().iterator(); it.hasNext(); ) 474 { 475 Map.Entry entry = (Map.Entry ) it.next(); 476 477 String key = (String ) entry.getKey(); 478 479 if ( !mergedReportPlugins.containsKey( key ) ) 480 { 481 mergedReportPlugins.put( key, entry.getValue() ); 482 } 483 } 484 485 modelReporting.setPlugins( new ArrayList ( mergedReportPlugins.values() ) ); 486 487 modelReporting.flushReportPluginMap(); 488 } 489 } 490 } 491 492 private void mergeReportPlugins( ReportPlugin dominant, ReportPlugin recessive ) 493 { 494 if ( StringUtils.isEmpty( recessive.getVersion() ) ) 495 { 496 recessive.setVersion( dominant.getVersion() ); 497 } 498 499 Xpp3Dom dominantConfig = (Xpp3Dom) dominant.getConfiguration(); 500 Xpp3Dom recessiveConfig = (Xpp3Dom) recessive.getConfiguration(); 501 502 recessive.setConfiguration( Xpp3Dom.mergeXpp3Dom( dominantConfig, recessiveConfig ) ); 503 504 Map mergedReportSets = new HashMap (); 505 506 Map dominantReportSetsById = dominant.getReportSetsAsMap(); 507 508 for ( Iterator it = recessive.getReportSets().iterator(); it.hasNext(); ) 509 { 510 ReportSet recessiveReportSet = (ReportSet) it.next(); 511 512 ReportSet dominantReportSet = (ReportSet) dominantReportSetsById.get( recessiveReportSet.getId() ); 513 514 ReportSet merged = recessiveReportSet; 515 516 if ( dominantReportSet != null ) 517 { 518 merged = recessiveReportSet; 519 520 Xpp3Dom dominantRSConfig = (Xpp3Dom) dominantReportSet.getConfiguration(); 521 Xpp3Dom mergedRSConfig = (Xpp3Dom) merged.getConfiguration(); 522 523 merged.setConfiguration( Xpp3Dom.mergeXpp3Dom( dominantRSConfig, mergedRSConfig ) ); 524 525 List mergedReports = merged.getReports(); 526 527 if ( mergedReports == null ) 528 { 529 mergedReports = new ArrayList (); 530 531 merged.setReports( mergedReports ); 532 } 533 534 List dominantRSReports = dominantReportSet.getReports(); 535 536 if ( dominantRSReports != null ) 537 { 538 for ( Iterator reportIterator = dominantRSReports.iterator(); reportIterator.hasNext(); ) 539 { 540 String report = (String ) reportIterator.next(); 541 542 if ( !mergedReports.contains( report ) ) 543 { 544 mergedReports.add( report ); 545 } 546 } 547 } 548 549 mergedReportSets.put( merged.getId(), merged ); 550 } 551 } 552 553 for ( Iterator rsIterator = dominantReportSetsById.entrySet().iterator(); rsIterator.hasNext(); ) 554 { 555 Map.Entry entry = (Map.Entry ) rsIterator.next(); 556 557 String key = (String ) entry.getKey(); 558 559 if ( !mergedReportSets.containsKey( key ) ) 560 { 561 mergedReportSets.put( key, entry.getValue() ); 562 } 563 } 564 565 recessive.setReportSets( new ArrayList ( mergedReportSets.values() ) ); 566 567 recessive.flushReportSetMap(); 568 } 569 570 private void injectDependencies( Profile profile, Model model ) 571 { 572 Map depsMap = new HashMap (); 573 574 List deps = model.getDependencies(); 575 576 if ( deps != null ) 577 { 578 for ( Iterator it = deps.iterator(); it.hasNext(); ) 579 { 580 Dependency dependency = (Dependency) it.next(); 581 depsMap.put( dependency.getManagementKey(), dependency ); 582 } 583 } 584 585 deps = profile.getDependencies(); 586 587 if ( deps != null ) 588 { 589 for ( Iterator it = deps.iterator(); it.hasNext(); ) 590 { 591 Dependency dependency = (Dependency) it.next(); 592 depsMap.put( dependency.getManagementKey(), dependency ); 593 } 594 } 595 596 model.setDependencies( new ArrayList ( depsMap.values() ) ); 597 } 598 599 } 600 | Popular Tags |