1 package org.apache.maven.model.converter; 2 3 18 19 import org.apache.maven.model.Build; 20 import org.apache.maven.model.CiManagement; 21 import org.apache.maven.model.Contributor; 22 import org.apache.maven.model.Dependency; 23 import org.apache.maven.model.DeploymentRepository; 24 import org.apache.maven.model.Developer; 25 import org.apache.maven.model.DistributionManagement; 26 import org.apache.maven.model.IssueManagement; 27 import org.apache.maven.model.License; 28 import org.apache.maven.model.MailingList; 29 import org.apache.maven.model.Model; 30 import org.apache.maven.model.Notifier; 31 import org.apache.maven.model.Organization; 32 import org.apache.maven.model.Plugin; 33 import org.apache.maven.model.Resource; 34 import org.apache.maven.model.Scm; 35 import org.apache.maven.model.Site; 36 import org.apache.maven.model.v3_0_0.UnitTest; 37 import org.codehaus.plexus.util.StringUtils; 38 import org.codehaus.plexus.util.xml.Xpp3Dom; 39 40 import java.util.ArrayList ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Properties ; 44 45 48 public class PomV3ToV4Translator 49 implements ModelConverter 50 { 51 private transient List discoveredPlugins = new ArrayList (); 52 53 private List warnings; 54 55 public Model translate( org.apache.maven.model.v3_0_0.Model v3Model ) 56 throws PomTranslationException 57 { 58 warnings = new ArrayList (); 59 60 try 61 { 62 String groupId = format( v3Model.getGroupId() ); 63 String artifactId = format( v3Model.getArtifactId() ); 64 65 String id = v3Model.getId(); 66 67 if ( StringUtils.isNotEmpty( id ) ) 68 { 69 if ( StringUtils.isEmpty( groupId ) ) 70 { 71 int plusIdx = id.indexOf( "+" ); 72 if ( plusIdx > -1 ) 73 { 74 groupId = id.substring( 0, plusIdx ); 75 } 76 else 77 { 78 groupId = id; 79 } 80 } 81 82 if ( StringUtils.isEmpty( artifactId ) ) 83 { 84 artifactId = format( id ); 85 } 86 } 87 88 String version = format( v3Model.getCurrentVersion() ); 89 90 if ( version == null ) 91 { 92 version = format( v3Model.getVersion() ); 93 } 94 95 PomKey pomKey = new PomKey( groupId, artifactId, version ); 96 97 warnOfUnsupportedMainModelElements( v3Model ); 98 99 Model model = new Model(); 100 model.setArtifactId( artifactId ); 101 102 model.setDependencies( translateDependencies( v3Model.getDependencies() ) ); 106 107 model.setBuild( translateBuild( v3Model.getBuild() ) ); 108 model.setCiManagement( translateCiManagementInfo( v3Model.getBuild() ) ); 109 model.setContributors( translateContributors( v3Model.getContributors() ) ); 110 111 model.setDescription( v3Model.getDescription() ); 112 model.setDevelopers( translateDevelopers( v3Model.getDevelopers() ) ); 113 114 model.setDistributionManagement( translateDistributionManagement( pomKey, v3Model ) ); 115 116 model.setGroupId( groupId ); 117 model.setInceptionYear( v3Model.getInceptionYear() ); 118 model.setIssueManagement( translateIssueManagement( v3Model ) ); 119 120 model.setLicenses( translateLicenses( v3Model.getLicenses() ) ); 121 model.setMailingLists( translateMailingLists( v3Model.getMailingLists() ) ); 122 model.setModelVersion( "4.0.0" ); 123 model.setName( v3Model.getName() ); 124 model.setOrganization( translateOrganization( v3Model.getOrganization() ) ); 125 model.setPackaging( "jar" ); 126 model.setScm( translateScm( v3Model ) ); 129 model.setUrl( v3Model.getUrl() ); 130 131 model.setVersion( version ); 132 133 return model; 134 } 135 finally 136 { 137 this.discoveredPlugins.clear(); 138 } 139 } 140 141 private String format( String source ) 142 { 143 return source == null ? null : source.replace( '+', '-' ); 144 } 145 146 private CiManagement translateCiManagementInfo( org.apache.maven.model.v3_0_0.Build v3Build ) 147 { 148 CiManagement ciMgmt = null; 149 150 if ( v3Build != null ) 151 { 152 String nagEmailAddress = v3Build.getNagEmailAddress(); 153 154 if ( StringUtils.isNotEmpty( nagEmailAddress ) ) 155 { 156 Notifier notifier = new Notifier(); 157 158 notifier.setType( "mail" ); 159 notifier.addConfiguration( "address", nagEmailAddress ); 160 161 ciMgmt = new CiManagement(); 162 ciMgmt.addNotifier( notifier ); 163 } 164 } 165 166 return ciMgmt; 167 } 168 169 private void warnOfUnsupportedMainModelElements( org.apache.maven.model.v3_0_0.Model v3Model ) 170 { 171 if ( StringUtils.isNotEmpty( v3Model.getExtend() ) ) 172 { 173 warnings.add( "Ignoring non-portable parent declaration: " + v3Model.getExtend() ); 174 } 175 176 if ( StringUtils.isNotEmpty( v3Model.getGumpRepositoryId() ) ) 177 { 178 warnings.add( "Ignoring gump repository id: \'" + v3Model.getGumpRepositoryId() + 179 "\'. This is not supported in v4 POMs." ); 180 } 181 182 if ( notEmpty( v3Model.getVersions() ) ) 183 { 184 warnings.add( "Ignoring <versions/> section. This is not supported in v4 POMs." ); 185 } 186 187 if ( notEmpty( v3Model.getBranches() ) ) 188 { 189 warnings.add( "Ignoring <branches/> section. This is not supported in v4 POMs." ); 190 } 191 192 Properties v3ModelProperties = v3Model.getProperties(); 193 194 if ( v3ModelProperties != null && !v3ModelProperties.isEmpty() ) 195 { 196 warnings.add( "Ignoring <properties/> section. It is not supported in v4 POMs." ); 197 } 198 199 if ( StringUtils.isNotEmpty( v3Model.getPackage() ) ) 200 { 201 warnings.add( "Ignoring <package/>. It is not supported in v4 POMs." ); 202 } 203 204 if ( notEmpty( v3Model.getPackageGroups() ) ) 205 { 206 warnings.add( "Ignoring <packageGroups/> section. It is not supported in v4 POMs." ); 207 } 208 209 if ( StringUtils.isNotEmpty( v3Model.getLogo() ) ) 210 { 211 warnings.add( "Ignoring <logo/> for project. It is not supported in v4 POMs." ); 212 } 213 214 if ( StringUtils.isNotEmpty( v3Model.getShortDescription() ) ) 215 { 216 warnings.add( "Ignoring <shortDescription/>. It is not supported in v4 POMs." ); 217 } 218 } 219 220 private Scm translateScm( org.apache.maven.model.v3_0_0.Model v3Model ) 221 { 222 Scm scm = null; 223 224 org.apache.maven.model.v3_0_0.Repository repo = v3Model.getRepository(); 225 if ( repo != null ) 226 { 227 scm = new Scm(); 228 scm.setConnection( repo.getConnection() ); 229 scm.setDeveloperConnection( repo.getDeveloperConnection() ); 230 scm.setUrl( repo.getUrl() ); 231 } 232 233 return scm; 234 } 235 236 287 288 private Organization translateOrganization( org.apache.maven.model.v3_0_0.Organization v3Organization ) 289 { 290 Organization organization = null; 291 292 if ( v3Organization != null ) 293 { 294 organization = new Organization(); 295 296 organization.setName( v3Organization.getName() ); 297 organization.setUrl( v3Organization.getUrl() ); 298 299 if ( StringUtils.isNotEmpty( v3Organization.getLogo() ) ) 300 { 301 warnings.add( "Ignoring <organization><logo/></organization>. It is not supported in v4 POMs." ); 302 } 303 } 304 305 return organization; 306 } 307 308 private List translateMailingLists( List v3MailingLists ) 309 { 310 List mailingLists = new ArrayList (); 311 312 if ( notEmpty( v3MailingLists ) ) 313 { 314 for ( Iterator it = v3MailingLists.iterator(); it.hasNext(); ) 315 { 316 org.apache.maven.model.v3_0_0.MailingList v3List = (org.apache.maven.model.v3_0_0.MailingList) it 317 .next(); 318 MailingList list = new MailingList(); 319 list.setArchive( v3List.getArchive() ); 320 list.setName( v3List.getName() ); 321 list.setSubscribe( v3List.getSubscribe() ); 322 list.setUnsubscribe( v3List.getUnsubscribe() ); 323 324 mailingLists.add( list ); 325 } 326 } 327 328 return mailingLists; 329 } 330 331 private List translateLicenses( List v3Licenses ) 332 { 333 List licenses = new ArrayList (); 334 335 if ( notEmpty( v3Licenses ) ) 336 { 337 for ( Iterator it = v3Licenses.iterator(); it.hasNext(); ) 338 { 339 org.apache.maven.model.v3_0_0.License v3License = (org.apache.maven.model.v3_0_0.License) it.next(); 340 License license = new License(); 341 license.setComments( v3License.getComments() ); 342 license.setName( v3License.getName() ); 343 license.setUrl( v3License.getUrl() ); 344 345 licenses.add( license ); 346 } 347 } 348 349 return licenses; 350 } 351 352 private IssueManagement translateIssueManagement( org.apache.maven.model.v3_0_0.Model v3Model ) 353 { 354 IssueManagement issueMgmt = null; 355 356 String issueTrackingUrl = v3Model.getIssueTrackingUrl(); 357 if ( StringUtils.isNotEmpty( issueTrackingUrl ) ) 358 { 359 issueMgmt = new IssueManagement(); 360 issueMgmt.setUrl( issueTrackingUrl ); 361 } 362 363 return issueMgmt; 364 } 365 366 private DistributionManagement translateDistributionManagement( PomKey pomKey, 367 org.apache.maven.model.v3_0_0.Model v3Model ) 368 throws PomTranslationException 369 { 370 DistributionManagement distributionManagement = new DistributionManagement(); 371 372 Site site = null; 373 374 String siteAddress = v3Model.getSiteAddress(); 375 376 String siteDirectory = v3Model.getSiteDirectory(); 377 378 if ( StringUtils.isEmpty( siteAddress ) ) 379 { 380 if ( !StringUtils.isEmpty( siteDirectory ) ) 381 { 382 site = new Site(); 383 384 site.setId( "default" ); 385 386 site.setName( "Default Site" ); 387 388 site.setUrl( "file://" + siteDirectory ); 389 } 390 } 391 else 392 { 393 if ( StringUtils.isEmpty( siteDirectory ) ) 394 { 395 throw new PomTranslationException( pomKey.groupId(), pomKey.artifactId(), pomKey.version(), 396 "Missing 'siteDirectory': Both siteAddress and siteDirectory must be set at the same time." ); 397 } 398 399 site = new Site(); 400 401 site.setId( "default" ); 402 403 site.setName( "Default Site" ); 404 405 site.setUrl( "scp://" + siteAddress + "/" + siteDirectory ); 406 } 407 408 distributionManagement.setSite( site ); 409 410 String distributionSite = v3Model.getDistributionSite(); 411 412 String distributionDirectory = v3Model.getDistributionDirectory(); 413 414 DeploymentRepository repository = null; 415 416 if ( StringUtils.isEmpty( distributionSite ) ) 417 { 418 if ( !StringUtils.isEmpty( distributionDirectory ) ) 419 { 420 repository = new DeploymentRepository(); 421 422 repository.setId( "default" ); 423 424 repository.setName( "Default Repository" ); 425 426 repository.setUrl( "file://" + distributionDirectory ); 427 } 430 } 431 else 432 { 433 if ( StringUtils.isEmpty( distributionDirectory ) ) 434 { 435 throw new PomTranslationException( pomKey.groupId(), pomKey.artifactId(), pomKey.version(), 436 "Missing 'distributionDirectory': must be set is 'distributionSite' is set." ); 437 } 438 439 repository = new DeploymentRepository(); 440 441 repository.setId( "default" ); 442 443 repository.setName( "Default Repository" ); 444 445 repository.setUrl( distributionSite + "/" + distributionDirectory ); 446 } 447 448 distributionManagement.setRepository( repository ); 449 450 distributionManagement.setStatus( "converted" ); 451 452 if ( site == null && repository == null ) 453 { 454 return null; 455 } 456 457 return distributionManagement; 458 } 459 460 private List translateDevelopers( List v3Developers ) 461 { 462 List developers = new ArrayList (); 463 464 if ( notEmpty( v3Developers ) ) 465 { 466 for ( Iterator it = v3Developers.iterator(); it.hasNext(); ) 467 { 468 org.apache.maven.model.v3_0_0.Developer v3Developer = (org.apache.maven.model.v3_0_0.Developer) it 469 .next(); 470 471 Developer developer = new Developer(); 472 473 developer.setEmail( v3Developer.getEmail() ); 474 developer.setId( v3Developer.getId() ); 475 developer.setName( v3Developer.getName() ); 476 developer.setOrganization( v3Developer.getOrganization() ); 477 developer.setRoles( v3Developer.getRoles() ); 478 developer.setTimezone( v3Developer.getTimezone() ); 479 developer.setUrl( v3Developer.getUrl() ); 480 481 developers.add( developer ); 482 } 483 } 484 485 return developers; 486 } 487 488 private List translateDependencies( List v3Deps ) 489 { 490 List deps = new ArrayList (); 491 492 if ( notEmpty( v3Deps ) ) 493 { 494 for ( Iterator it = v3Deps.iterator(); it.hasNext(); ) 495 { 496 org.apache.maven.model.v3_0_0.Dependency v3Dep = (org.apache.maven.model.v3_0_0.Dependency) it.next(); 497 498 String groupId = format( v3Dep.getGroupId() ); 499 String artifactId = format( v3Dep.getArtifactId() ); 500 501 String id = v3Dep.getId(); 502 503 if ( StringUtils.isNotEmpty( id ) ) 504 { 505 if ( StringUtils.isEmpty( groupId ) ) 506 { 507 int plusIdx = id.indexOf( "+" ); 508 509 if ( plusIdx > -1 ) 510 { 511 groupId = id.substring( 0, plusIdx ); 512 } 513 else 514 { 515 groupId = id; 516 } 517 } 518 519 if ( StringUtils.isEmpty( artifactId ) ) 520 { 521 artifactId = format( id ); 522 } 523 } 524 525 String type = v3Dep.getType(); 526 if ( "plugin".equals( type ) ) 527 { 528 if ( "maven".equals( groupId ) ) 529 { 530 groupId = "org.apache.maven.plugins"; 531 } 532 533 Plugin plugin = new Plugin(); 534 plugin.setGroupId( groupId ); 535 plugin.setArtifactId( artifactId ); 536 plugin.setVersion( format( v3Dep.getVersion() ) ); 537 538 Xpp3Dom config = new Xpp3Dom( "configuration" ); 539 540 Properties props = v3Dep.getProperties(); 541 542 if ( !props.isEmpty() ) 543 { 544 for ( Iterator propertyIterator = props.keySet().iterator(); propertyIterator.hasNext(); ) 545 { 546 String key = (String ) propertyIterator.next(); 547 String value = props.getProperty( key ); 548 549 Xpp3Dom child = new Xpp3Dom( key ); 550 child.setValue( value ); 551 552 config.addChild( child ); 553 } 554 } 555 556 plugin.setConfiguration( config ); 557 558 this.discoveredPlugins.add( plugin ); 559 } 560 else 561 { 562 Dependency dep = new Dependency(); 563 564 dep.setGroupId( groupId ); 565 dep.setArtifactId( artifactId ); 566 dep.setVersion( v3Dep.getVersion() ); 567 dep.setType( v3Dep.getType() ); 568 569 String scope = v3Dep.getProperty( "scope" ); 570 if ( StringUtils.isNotEmpty( scope ) ) 571 { 572 dep.setScope( scope ); 573 } 574 575 String optional = v3Dep.getProperty( "optional" ); 576 if ( StringUtils.isNotEmpty( optional ) ) 577 { 578 dep.setOptional( Boolean.valueOf( optional ).booleanValue() ); 579 } 580 581 deps.add( dep ); 582 } 583 } 584 } 585 586 return deps; 587 } 588 589 private List translateContributors( List v3Contributors ) 590 { 591 List contributors = new ArrayList (); 592 593 if ( notEmpty( v3Contributors ) ) 594 { 595 for ( Iterator it = v3Contributors.iterator(); it.hasNext(); ) 596 { 597 org.apache.maven.model.v3_0_0.Contributor v3Contributor = (org.apache.maven.model.v3_0_0.Contributor) it 598 .next(); 599 600 Contributor contributor = new Contributor(); 601 602 contributor.setEmail( v3Contributor.getEmail() ); 603 contributor.setName( v3Contributor.getName() ); 604 contributor.setOrganization( v3Contributor.getOrganization() ); 605 contributor.setRoles( v3Contributor.getRoles() ); 606 contributor.setTimezone( v3Contributor.getTimezone() ); 607 contributor.setUrl( v3Contributor.getUrl() ); 608 609 contributors.add( contributor ); 610 } 611 } 612 613 return contributors; 614 } 615 616 private Build translateBuild( org.apache.maven.model.v3_0_0.Build v3Build ) 617 { 618 Build build = null; 619 if ( v3Build != null ) 620 { 621 build = new Build(); 622 623 warnOfUnsupportedBuildElements( v3Build ); 624 625 build.setSourceDirectory( v3Build.getSourceDirectory() ); 626 build.setTestSourceDirectory( v3Build.getUnitTestSourceDirectory() ); 627 628 build.setResources( translateResources( v3Build.getResources() ) ); 629 630 UnitTest unitTest = v3Build.getUnitTest(); 631 if ( unitTest != null ) 632 { 633 build.setTestResources( translateResources( unitTest.getResources() ) ); 634 635 List testIncludes = unitTest.getIncludes(); 636 637 List testExcludes = new ArrayList ( unitTest.getExcludes() ); 638 639 if ( notEmpty( testIncludes ) || notEmpty( testExcludes ) ) 640 { 641 Plugin plugin = new Plugin(); 642 plugin.setGroupId( "org.apache.maven.plugins" ); 643 plugin.setArtifactId( "maven-surefire-plugin" ); 644 645 Xpp3Dom config = new Xpp3Dom( "configuration" ); 646 647 if ( notEmpty( testIncludes ) ) 648 { 649 Xpp3Dom includes = new Xpp3Dom( "includes" ); 650 for ( Iterator it = testIncludes.iterator(); it.hasNext(); ) 651 { 652 String includePattern = (String ) it.next(); 653 Xpp3Dom include = new Xpp3Dom( "include" ); 654 include.setValue( includePattern ); 655 656 includes.addChild( include ); 657 } 658 659 config.addChild( includes ); 660 } 661 662 if ( notEmpty( testExcludes ) ) 663 { 664 Xpp3Dom excludes = new Xpp3Dom( "excludes" ); 665 for ( Iterator it = testExcludes.iterator(); it.hasNext(); ) 666 { 667 String excludePattern = (String ) it.next(); 668 Xpp3Dom exclude = new Xpp3Dom( "exclude" ); 669 exclude.setValue( excludePattern ); 670 671 excludes.addChild( exclude ); 672 } 673 674 config.addChild( excludes ); 675 } 676 677 if ( config.getChildCount() > 0 ) 678 { 679 plugin.setConfiguration( config ); 680 } 681 682 build.addPlugin( plugin ); 683 } 684 } 685 } 686 687 if ( !this.discoveredPlugins.isEmpty() ) 688 { 689 if ( build == null ) 690 { 691 build = new Build(); 692 } 693 694 for ( Iterator it = this.discoveredPlugins.iterator(); it.hasNext(); ) 695 { 696 Plugin plugin = (Plugin) it.next(); 697 698 build.addPlugin( plugin ); 699 } 700 } 701 702 return build; 703 } 704 705 private void warnOfUnsupportedBuildElements( org.apache.maven.model.v3_0_0.Build v3Build ) 706 { 707 if ( notEmpty( v3Build.getSourceModifications() ) ) 708 { 709 warnings.add( "Ignoring <sourceModifications/> section. It is not supported in v4 POMs." ); 710 } 711 712 if ( StringUtils.isNotEmpty( v3Build.getAspectSourceDirectory() ) ) 713 { 714 warnings.add( "Ignoring <aspectSourceDirectory/>. It is not supported in v4 POMs." ); 715 } 716 717 if ( StringUtils.isNotEmpty( v3Build.getIntegrationUnitTestSourceDirectory() ) ) 718 { 719 warnings.add( "Ignoring <integrationUnitTestSourceDirectory/>. It is not supported in v4 POMs." ); 720 } 721 } 722 723 private List translateResources( List v3Resources ) 724 { 725 List resources = new ArrayList (); 726 727 if ( notEmpty( v3Resources ) ) 728 { 729 for ( Iterator it = v3Resources.iterator(); it.hasNext(); ) 730 { 731 org.apache.maven.model.v3_0_0.Resource v3Resource = (org.apache.maven.model.v3_0_0.Resource) it.next(); 732 Resource resource = new Resource(); 733 734 if ( v3Resource.getDirectory() == null ) 735 { 736 resource.setDirectory( "." ); 737 } 738 else 739 { 740 resource.setDirectory( v3Resource.getDirectory() ); 741 } 742 743 List excludes = new ArrayList ( v3Resource.getExcludes() ); 744 745 resource.setExcludes( excludes ); 746 747 resource.setIncludes( v3Resource.getIncludes() ); 748 resource.setTargetPath( v3Resource.getTargetPath() ); 749 750 resources.add( resource ); 751 } 752 } 753 754 return resources; 755 } 756 757 776 private boolean notEmpty( List test ) 777 { 778 return test != null && !test.isEmpty(); 779 } 780 781 public void validateV4Basics( Model model, String groupId, String artifactId, String version, String packaging ) 782 { 783 if ( StringUtils.isEmpty( model.getModelVersion() ) ) 784 { 785 model.setModelVersion( "4.0.0" ); 786 } 787 788 if ( StringUtils.isEmpty( model.getGroupId() ) ) 789 { 790 warnings.add( "Setting groupId on model using artifact information." ); 791 model.setGroupId( groupId ); 792 } 793 794 if ( StringUtils.isEmpty( model.getArtifactId() ) ) 795 { 796 warnings.add( "Setting artifactId on model using artifact information." ); 797 model.setArtifactId( artifactId ); 798 } 799 800 if ( StringUtils.isEmpty( model.getVersion() ) ) 801 { 802 warnings.add( "Setting version on model using artifact information." ); 803 model.setVersion( version ); 804 } 805 806 if ( StringUtils.isEmpty( model.getPackaging() ) ) 807 { 808 warnings.add( "Setting packaging on model using artifact type information." ); 809 model.setPackaging( packaging ); 810 } 811 } 812 813 public List getWarnings() 814 { 815 return warnings; 816 } 817 818 private static class PomKey 819 { 820 private final String groupId; 821 822 private final String artifactId; 823 824 private final String version; 825 826 PomKey( String groupId, String artifactId, String version ) 827 { 828 this.groupId = groupId; 829 this.artifactId = artifactId; 830 this.version = version; 831 } 832 833 public String groupId() 834 { 835 return groupId; 836 } 837 838 public String artifactId() 839 { 840 return artifactId; 841 } 842 843 public String version() 844 { 845 return version; 846 } 847 } 848 849 } | Popular Tags |