1 package org.apache.maven.project.inheritance; 2 3 18 19 import org.apache.maven.model.Build; 20 import org.apache.maven.model.Dependency; 21 import org.apache.maven.model.DependencyManagement; 22 import org.apache.maven.model.DeploymentRepository; 23 import org.apache.maven.model.DistributionManagement; 24 import org.apache.maven.model.Model; 25 import org.apache.maven.model.PluginManagement; 26 import org.apache.maven.model.Reporting; 27 import org.apache.maven.model.Scm; 28 import org.apache.maven.model.Site; 29 import org.apache.maven.project.ModelUtils; 30 import org.codehaus.plexus.util.StringUtils; 31 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import java.util.StringTokenizer ; 40 import java.util.TreeMap ; 41 42 48 public class DefaultModelInheritanceAssembler 49 implements ModelInheritanceAssembler 50 { 51 public void copyModel( Model dest, Model source ) 52 { 53 assembleModelInheritance( dest, source, null, false ); 54 } 55 56 public void assembleModelInheritance( Model child, Model parent, String childPathAdjustment ) 57 { 58 assembleModelInheritance( child, parent, childPathAdjustment, true ); 59 } 60 61 public void assembleModelInheritance( Model child, Model parent ) 62 { 63 assembleModelInheritance( child, parent, null, true ); 64 } 65 66 private void assembleModelInheritance( Model child, Model parent, String childPathAdjustment, boolean appendPaths ) 67 { 68 if ( parent == null ) 70 { 71 return; 72 } 73 74 if ( child.getGroupId() == null ) 76 { 77 child.setGroupId( parent.getGroupId() ); 78 } 79 80 if ( child.getVersion() == null ) 82 { 83 86 if ( child.getParent() != null ) 87 { 88 child.setVersion( child.getParent().getVersion() ); 89 } 90 } 91 92 if ( child.getInceptionYear() == null ) 94 { 95 child.setInceptionYear( parent.getInceptionYear() ); 96 } 97 98 if ( child.getUrl() == null ) 100 { 101 if ( parent.getUrl() != null ) 102 { 103 child.setUrl( appendPath( parent.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) ); 104 } 105 else 106 { 107 child.setUrl( parent.getUrl() ); 108 } 109 } 110 111 assembleDistributionInheritence( child, parent, appendPaths ); 112 113 if ( child.getIssueManagement() == null ) 115 { 116 child.setIssueManagement( parent.getIssueManagement() ); 117 } 118 119 if ( child.getDescription() == null ) 121 { 122 child.setDescription( parent.getDescription() ); 123 } 124 125 if ( child.getOrganization() == null ) 127 { 128 child.setOrganization( parent.getOrganization() ); 129 } 130 131 assembleScmInheritance( child, parent, childPathAdjustment, appendPaths ); 133 134 if ( child.getCiManagement() == null ) 136 { 137 child.setCiManagement( parent.getCiManagement() ); 138 } 139 140 if ( child.getDevelopers().size() == 0 ) 142 { 143 child.setDevelopers( parent.getDevelopers() ); 144 } 145 146 if ( child.getLicenses().size() == 0 ) 148 { 149 child.setLicenses( parent.getLicenses() ); 150 } 151 152 if ( child.getContributors().size() == 0 ) 154 { 155 child.setContributors( parent.getContributors() ); 156 } 157 158 if ( child.getMailingLists().size() == 0 ) 160 { 161 child.setMailingLists( parent.getMailingLists() ); 162 } 163 164 assembleBuildInheritance( child, parent ); 166 167 assembleDependencyInheritance( child, parent ); 168 169 child.setRepositories( ModelUtils.mergeRepositoryLists( child.getRepositories(), parent.getRepositories() ) ); 170 child.setPluginRepositories( 171 ModelUtils.mergeRepositoryLists( child.getPluginRepositories(), parent.getPluginRepositories() ) ); 172 173 assembleReportingInheritance( child, parent ); 174 175 assembleDependencyManagementInheritance( child, parent ); 176 177 Properties props = new Properties (); 178 props.putAll( parent.getProperties() ); 179 props.putAll( child.getProperties() ); 180 181 child.setProperties( props ); 182 } 183 184 private void assembleDependencyManagementInheritance( Model child, Model parent ) 185 { 186 DependencyManagement parentDepMgmt = parent.getDependencyManagement(); 187 188 DependencyManagement childDepMgmt = child.getDependencyManagement(); 189 190 if ( parentDepMgmt != null ) 191 { 192 if ( childDepMgmt == null ) 193 { 194 child.setDependencyManagement( parentDepMgmt ); 195 } 196 else 197 { 198 List childDeps = childDepMgmt.getDependencies(); 199 200 Map mappedChildDeps = new TreeMap (); 201 for ( Iterator it = childDeps.iterator(); it.hasNext(); ) 202 { 203 Dependency dep = (Dependency) it.next(); 204 mappedChildDeps.put( dep.getManagementKey(), dep ); 205 } 206 207 for ( Iterator it = parentDepMgmt.getDependencies().iterator(); it.hasNext(); ) 208 { 209 Dependency dep = (Dependency) it.next(); 210 if ( !mappedChildDeps.containsKey( dep.getManagementKey() ) ) 211 { 212 childDepMgmt.addDependency( dep ); 213 } 214 } 215 } 216 } 217 } 218 219 private void assembleReportingInheritance( Model child, Model parent ) 220 { 221 Reporting childReporting = child.getReporting(); 223 Reporting parentReporting = parent.getReporting(); 224 225 if ( parentReporting != null ) 226 { 227 if ( childReporting == null ) 228 { 229 childReporting = new Reporting(); 230 child.setReporting( childReporting ); 231 } 232 233 childReporting.setExcludeDefaults( parentReporting.isExcludeDefaults() ); 234 235 if ( StringUtils.isEmpty( childReporting.getOutputDirectory() ) ) 236 { 237 childReporting.setOutputDirectory( parentReporting.getOutputDirectory() ); 238 } 239 240 ModelUtils.mergeReportPluginLists( childReporting, parentReporting, true ); 241 } 242 } 243 244 private void assembleDependencyInheritance( Model child, Model parent ) 245 { 246 Map depsMap = new HashMap (); 247 248 List deps = parent.getDependencies(); 249 250 if ( deps != null ) 251 { 252 for ( Iterator it = deps.iterator(); it.hasNext(); ) 253 { 254 Dependency dependency = (Dependency) it.next(); 255 depsMap.put( dependency.getManagementKey(), dependency ); 256 } 257 } 258 259 deps = child.getDependencies(); 260 261 if ( deps != null ) 262 { 263 for ( Iterator it = deps.iterator(); it.hasNext(); ) 264 { 265 Dependency dependency = (Dependency) it.next(); 266 depsMap.put( dependency.getManagementKey(), dependency ); 267 } 268 } 269 270 child.setDependencies( new ArrayList ( depsMap.values() ) ); 271 } 272 273 private void assembleBuildInheritance( Model child, Model parent ) 274 { 275 Build childBuild = child.getBuild(); 276 Build parentBuild = parent.getBuild(); 277 278 if ( parentBuild != null ) 279 { 280 if ( childBuild == null ) 281 { 282 childBuild = new Build(); 283 child.setBuild( childBuild ); 284 } 285 286 289 if ( childBuild.getSourceDirectory() == null ) 290 { 291 childBuild.setSourceDirectory( parentBuild.getSourceDirectory() ); 292 } 293 294 if ( childBuild.getScriptSourceDirectory() == null ) 295 { 296 childBuild.setScriptSourceDirectory( parentBuild.getScriptSourceDirectory() ); 297 } 298 299 if ( childBuild.getTestSourceDirectory() == null ) 300 { 301 childBuild.setTestSourceDirectory( parentBuild.getTestSourceDirectory() ); 302 } 303 304 if ( childBuild.getOutputDirectory() == null ) 305 { 306 childBuild.setOutputDirectory( parentBuild.getOutputDirectory() ); 307 } 308 309 if ( childBuild.getTestOutputDirectory() == null ) 310 { 311 childBuild.setTestOutputDirectory( parentBuild.getTestOutputDirectory() ); 312 } 313 314 ModelUtils.mergeExtensionLists( childBuild, parentBuild ); 316 317 if ( childBuild.getDirectory() == null ) 318 { 319 childBuild.setDirectory( parentBuild.getDirectory() ); 320 } 321 322 if ( childBuild.getDefaultGoal() == null ) 323 { 324 childBuild.setDefaultGoal( parentBuild.getDefaultGoal() ); 325 } 326 327 if ( childBuild.getFinalName() == null ) 328 { 329 childBuild.setFinalName( parentBuild.getFinalName() ); 330 } 331 332 ModelUtils.mergeFilterLists( childBuild.getFilters(), parentBuild.getFilters() ); 333 334 List resources = childBuild.getResources(); 335 if ( resources == null || resources.isEmpty() ) 336 { 337 childBuild.setResources( parentBuild.getResources() ); 338 } 339 340 resources = childBuild.getTestResources(); 341 if ( resources == null || resources.isEmpty() ) 342 { 343 childBuild.setTestResources( parentBuild.getTestResources() ); 344 } 345 346 ModelUtils.mergePluginLists( childBuild, parentBuild, true ); 348 349 PluginManagement dominantPM = childBuild.getPluginManagement(); 351 PluginManagement recessivePM = parentBuild.getPluginManagement(); 352 353 if ( dominantPM == null && recessivePM != null ) 354 { 355 childBuild.setPluginManagement( recessivePM ); 356 } 357 else 358 { 359 ModelUtils.mergePluginLists( childBuild.getPluginManagement(), parentBuild.getPluginManagement(), 360 false ); 361 } 362 } 363 } 364 365 private void assembleScmInheritance( Model child, Model parent, String childPathAdjustment, boolean appendPaths ) 366 { 367 if ( parent.getScm() != null ) 368 { 369 Scm parentScm = parent.getScm(); 370 371 Scm childScm = child.getScm(); 372 373 if ( childScm == null ) 374 { 375 childScm = new Scm(); 376 377 child.setScm( childScm ); 378 } 379 380 if ( StringUtils.isEmpty( childScm.getConnection() ) && !StringUtils.isEmpty( parentScm.getConnection() ) ) 381 { 382 childScm.setConnection( 383 appendPath( parentScm.getConnection(), child.getArtifactId(), childPathAdjustment, appendPaths ) ); 384 } 385 386 if ( StringUtils.isEmpty( childScm.getDeveloperConnection() ) && 387 !StringUtils.isEmpty( parentScm.getDeveloperConnection() ) ) 388 { 389 childScm 390 .setDeveloperConnection( appendPath( parentScm.getDeveloperConnection(), child.getArtifactId(), 391 childPathAdjustment, appendPaths ) ); 392 } 393 394 if ( StringUtils.isEmpty( childScm.getUrl() ) && !StringUtils.isEmpty( parentScm.getUrl() ) ) 395 { 396 childScm.setUrl( 397 appendPath( parentScm.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) ); 398 } 399 } 400 } 401 402 private void assembleDistributionInheritence( Model child, Model parent, boolean appendPaths ) 403 { 404 if ( parent.getDistributionManagement() != null ) 405 { 406 DistributionManagement parentDistMgmt = parent.getDistributionManagement(); 407 408 DistributionManagement childDistMgmt = child.getDistributionManagement(); 409 410 if ( childDistMgmt == null ) 411 { 412 childDistMgmt = new DistributionManagement(); 413 414 child.setDistributionManagement( childDistMgmt ); 415 } 416 417 if ( childDistMgmt.getSite() == null ) 418 { 419 if ( parentDistMgmt.getSite() != null ) 420 { 421 Site site = new Site(); 422 423 childDistMgmt.setSite( site ); 424 425 site.setId( parentDistMgmt.getSite().getId() ); 426 427 site.setName( parentDistMgmt.getSite().getName() ); 428 429 site.setUrl( parentDistMgmt.getSite().getUrl() ); 430 431 if ( site.getUrl() != null ) 432 { 433 site.setUrl( 434 appendPath( site.getUrl(), child.getArtifactId(), null, appendPaths ) ); 435 } 436 } 437 } 438 439 if ( childDistMgmt.getRepository() == null ) 440 { 441 if ( parentDistMgmt.getRepository() != null ) 442 { 443 DeploymentRepository repository = copyDistributionRepository( parentDistMgmt.getRepository() ); 444 childDistMgmt.setRepository( repository ); 445 } 446 } 447 448 if ( childDistMgmt.getSnapshotRepository() == null ) 449 { 450 if ( parentDistMgmt.getSnapshotRepository() != null ) 451 { 452 DeploymentRepository repository = 453 copyDistributionRepository( parentDistMgmt.getSnapshotRepository() ); 454 childDistMgmt.setSnapshotRepository( repository ); 455 } 456 } 457 458 if ( StringUtils.isEmpty( childDistMgmt.getDownloadUrl() ) ) 459 { 460 childDistMgmt.setDownloadUrl( parentDistMgmt.getDownloadUrl() ); 461 } 462 463 } 466 } 467 468 private static DeploymentRepository copyDistributionRepository( DeploymentRepository parentRepository ) 469 { 470 DeploymentRepository repository = new DeploymentRepository(); 471 472 repository.setId( parentRepository.getId() ); 473 474 repository.setName( parentRepository.getName() ); 475 476 repository.setUrl( parentRepository.getUrl() ); 477 478 repository.setLayout( parentRepository.getLayout() ); 479 480 repository.setUniqueVersion( parentRepository.isUniqueVersion() ); 481 482 return repository; 483 } 484 485 protected String appendPath( String parentPath, String childPath, String pathAdjustment, boolean appendPaths ) 487 { 488 String uncleanPath = parentPath; 489 490 if ( appendPaths ) 491 { 492 if ( pathAdjustment != null ) 493 uncleanPath += "/" + pathAdjustment; 494 495 if ( childPath != null ) 496 uncleanPath += "/" + childPath; 497 } 498 499 String cleanedPath = ""; 500 501 int protocolIdx = uncleanPath.indexOf( "://" ); 502 503 if ( protocolIdx > -1 ) 504 { 505 cleanedPath = uncleanPath.substring( 0, protocolIdx + 3 ); 506 uncleanPath = uncleanPath.substring( protocolIdx + 3 ); 507 } 508 509 if ( uncleanPath.startsWith( "/" ) ) 510 cleanedPath += "/"; 511 512 return cleanedPath + resolvePath( uncleanPath ); 513 } 514 515 private static String resolvePath( String uncleanPath ) 517 { 518 LinkedList pathElements = new LinkedList (); 519 520 StringTokenizer tokenizer = new StringTokenizer ( uncleanPath, "/" ); 521 522 while ( tokenizer.hasMoreTokens() ) 523 { 524 String token = (String ) tokenizer.nextToken(); 525 526 if ( token.equals( "" ) ) 527 { 528 } 530 else if ( token.equals( ".." ) ) 531 { 532 if ( pathElements.isEmpty() ) 533 { 534 } 538 else 539 { 540 pathElements.removeLast(); 541 } 542 } 543 else 544 { 545 pathElements.addLast( token ); 546 } 547 } 548 549 550 StringBuffer cleanedPath = new StringBuffer (); 551 552 while ( !pathElements.isEmpty() ) 553 { 554 cleanedPath.append( pathElements.removeFirst() ); 555 if ( !pathElements.isEmpty() ) 556 cleanedPath.append( '/' ); 557 } 558 559 return cleanedPath.toString(); 560 } 561 562 } 563 | Popular Tags |