diff --git a/maven-model-converter/pom.xml b/maven-model-converter/pom.xml new file mode 100755 index 0000000000..6c5d0c8e3e --- /dev/null +++ b/maven-model-converter/pom.xml @@ -0,0 +1,31 @@ + + + maven + org.apache.maven + 2.0.1-SNAPSHOT + + 4.0.0 + maven-model-converter + Maven Model Converter + Converts between version 3.0.0 and version 4.0.0 models. + http://maven.apache.org + + + org.apache.maven + maven-model + 2.0.1-SNAPSHOT + + + org.apache.maven + maven-model-v3 + 2.0 + + + org.codehaus.plexus + plexus-container-default + 1.0-alpha-8 + test + + + diff --git a/maven-model-converter/src/main/java/org/apache/maven/model/converter/ArtifactPomRewriter.java b/maven-model-converter/src/main/java/org/apache/maven/model/converter/ArtifactPomRewriter.java new file mode 100644 index 0000000000..81440a3422 --- /dev/null +++ b/maven-model-converter/src/main/java/org/apache/maven/model/converter/ArtifactPomRewriter.java @@ -0,0 +1,41 @@ +package org.apache.maven.model.converter; + +/* + * Copyright 2005-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.sun.corba.se.impl.ior.ObjectAdapterIdArray; + +import java.io.Reader; +import java.io.Writer; +import java.util.List; + +/** + * @author jdcasey + */ +public interface ArtifactPomRewriter +{ + public static final String ROLE = ArtifactPomRewriter.class.getName(); + + public static final String V3_POM = "v3"; + + public static final String V4_POM = "v4"; + + public void rewrite( Reader from, Writer to, boolean reportOnly, String groupId, String artifactId, String version, + String packaging ) + throws Exception; + + List getWarnings(); +} diff --git a/maven-model-converter/src/main/java/org/apache/maven/model/converter/ModelConverter.java b/maven-model-converter/src/main/java/org/apache/maven/model/converter/ModelConverter.java new file mode 100644 index 0000000000..9ac70c8444 --- /dev/null +++ b/maven-model-converter/src/main/java/org/apache/maven/model/converter/ModelConverter.java @@ -0,0 +1,39 @@ +package org.apache.maven.model.converter; + +/* + * Copyright 2005-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.model.Model; + +import java.util.List; + +/** + * Model conversion interface. + * + * @author Brett Porter + * @version $Id$ + */ +public interface ModelConverter +{ + String ROLE = ModelConverter.class.getName(); + + Model translate( org.apache.maven.model.v3_0_0.Model v3Model ) + throws PomTranslationException; + + void validateV4Basics( Model model, String groupId, String artifactId, String version, String packaging ); + + List getWarnings(); +} diff --git a/maven-model-converter/src/main/java/org/apache/maven/model/converter/PomTranslationException.java b/maven-model-converter/src/main/java/org/apache/maven/model/converter/PomTranslationException.java new file mode 100644 index 0000000000..695d2d3728 --- /dev/null +++ b/maven-model-converter/src/main/java/org/apache/maven/model/converter/PomTranslationException.java @@ -0,0 +1,47 @@ +package org.apache.maven.model.converter;/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @author jdcasey + */ +public class PomTranslationException + extends Exception +{ + + private final String groupId; + + private final String artifactId; + + private final String version; + + public PomTranslationException( String groupId, String artifactId, String version, String message ) + { + this( groupId, artifactId, version, message, null ); + } + + public PomTranslationException( String groupId, String artifactId, String version, Throwable cause ) + { + this( groupId, artifactId, version, "[No message provided.]", cause ); + } + + public PomTranslationException( String groupId, String artifactId, String version, String message, Throwable cause ) + { + super( "In POM{" + groupId + ":" + artifactId + ":" + version + "}: " + message, cause ); + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + } +} \ No newline at end of file diff --git a/maven-model-converter/src/main/java/org/apache/maven/model/converter/PomV3ToV4Translator.java b/maven-model-converter/src/main/java/org/apache/maven/model/converter/PomV3ToV4Translator.java new file mode 100644 index 0000000000..994301239a --- /dev/null +++ b/maven-model-converter/src/main/java/org/apache/maven/model/converter/PomV3ToV4Translator.java @@ -0,0 +1,849 @@ +package org.apache.maven.model.converter; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.model.Build; +import org.apache.maven.model.CiManagement; +import org.apache.maven.model.Contributor; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.DeploymentRepository; +import org.apache.maven.model.Developer; +import org.apache.maven.model.DistributionManagement; +import org.apache.maven.model.IssueManagement; +import org.apache.maven.model.License; +import org.apache.maven.model.MailingList; +import org.apache.maven.model.Model; +import org.apache.maven.model.Notifier; +import org.apache.maven.model.Organization; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.Resource; +import org.apache.maven.model.Scm; +import org.apache.maven.model.Site; +import org.apache.maven.model.v3_0_0.UnitTest; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.xml.Xpp3Dom; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; + +/** + * @author jdcasey + */ +public class PomV3ToV4Translator + implements ModelConverter +{ + private transient List discoveredPlugins = new ArrayList(); + + private List warnings; + + public Model translate( org.apache.maven.model.v3_0_0.Model v3Model ) + throws PomTranslationException + { + warnings = new ArrayList(); + + try + { + String groupId = format( v3Model.getGroupId() ); + String artifactId = format( v3Model.getArtifactId() ); + + String id = v3Model.getId(); + + if ( StringUtils.isNotEmpty( id ) ) + { + if ( StringUtils.isEmpty( groupId ) ) + { + int plusIdx = id.indexOf( "+" ); + if ( plusIdx > -1 ) + { + groupId = id.substring( 0, plusIdx ); + } + else + { + groupId = id; + } + } + + if ( StringUtils.isEmpty( artifactId ) ) + { + artifactId = format( id ); + } + } + + String version = format( v3Model.getCurrentVersion() ); + + if ( version == null ) + { + version = format( v3Model.getVersion() ); + } + + PomKey pomKey = new PomKey( groupId, artifactId, version ); + + warnOfUnsupportedMainModelElements( v3Model ); + + Model model = new Model(); + model.setArtifactId( artifactId ); + + // moved this above the translation of the build, to allow + // additional plugins to be defined in v3 poms via + // plugin + model.setDependencies( translateDependencies( v3Model.getDependencies() ) ); + + model.setBuild( translateBuild( v3Model.getBuild() ) ); + model.setCiManagement( translateCiManagementInfo( v3Model.getBuild() ) ); + model.setContributors( translateContributors( v3Model.getContributors() ) ); + + model.setDescription( v3Model.getDescription() ); + model.setDevelopers( translateDevelopers( v3Model.getDevelopers() ) ); + + model.setDistributionManagement( translateDistributionManagement( pomKey, v3Model ) ); + + model.setGroupId( groupId ); + model.setInceptionYear( v3Model.getInceptionYear() ); + model.setIssueManagement( translateIssueManagement( v3Model ) ); + + model.setLicenses( translateLicenses( v3Model.getLicenses() ) ); + model.setMailingLists( translateMailingLists( v3Model.getMailingLists() ) ); + model.setModelVersion( "4.0.0" ); + model.setName( v3Model.getName() ); + model.setOrganization( translateOrganization( v3Model.getOrganization() ) ); + model.setPackaging( "jar" ); + // TODO: not very good conversion - just omit for now +// model.setReporting( translateReports( v3Model.getReports(), reporter ) ); + model.setScm( translateScm( v3Model ) ); + model.setUrl( v3Model.getUrl() ); + + model.setVersion( version ); + + return model; + } + finally + { + this.discoveredPlugins.clear(); + } + } + + private String format( String source ) + { + return source == null ? null : source.replace( '+', '-' ); + } + + private CiManagement translateCiManagementInfo( org.apache.maven.model.v3_0_0.Build v3Build ) + { + CiManagement ciMgmt = null; + + if ( v3Build != null ) + { + String nagEmailAddress = v3Build.getNagEmailAddress(); + + if ( StringUtils.isNotEmpty( nagEmailAddress ) ) + { + Notifier notifier = new Notifier(); + + notifier.setType( "mail" ); + notifier.addConfiguration( "address", nagEmailAddress ); + + ciMgmt = new CiManagement(); + ciMgmt.addNotifier( notifier ); + } + } + + return ciMgmt; + } + + private void warnOfUnsupportedMainModelElements( org.apache.maven.model.v3_0_0.Model v3Model ) + { + if ( StringUtils.isNotEmpty( v3Model.getExtend() ) ) + { + warnings.add( "Ignoring non-portable parent declaration: " + v3Model.getExtend() ); + } + + if ( StringUtils.isNotEmpty( v3Model.getGumpRepositoryId() ) ) + { + warnings.add( "Ignoring gump repository id: \'" + v3Model.getGumpRepositoryId() + + "\'. This is not supported in v4 POMs." ); + } + + if ( notEmpty( v3Model.getVersions() ) ) + { + warnings.add( "Ignoring section. This is not supported in v4 POMs." ); + } + + if ( notEmpty( v3Model.getBranches() ) ) + { + warnings.add( "Ignoring section. This is not supported in v4 POMs." ); + } + + Properties v3ModelProperties = v3Model.getProperties(); + + if ( v3ModelProperties != null && !v3ModelProperties.isEmpty() ) + { + warnings.add( "Ignoring section. It is not supported in v4 POMs." ); + } + + if ( StringUtils.isNotEmpty( v3Model.getPackage() ) ) + { + warnings.add( "Ignoring . It is not supported in v4 POMs." ); + } + + if ( notEmpty( v3Model.getPackageGroups() ) ) + { + warnings.add( "Ignoring section. It is not supported in v4 POMs." ); + } + + if ( StringUtils.isNotEmpty( v3Model.getLogo() ) ) + { + warnings.add( "Ignoring for project. It is not supported in v4 POMs." ); + } + + if ( StringUtils.isNotEmpty( v3Model.getShortDescription() ) ) + { + warnings.add( "Ignoring . It is not supported in v4 POMs." ); + } + } + + private Scm translateScm( org.apache.maven.model.v3_0_0.Model v3Model ) + { + Scm scm = null; + + org.apache.maven.model.v3_0_0.Repository repo = v3Model.getRepository(); + if ( repo != null ) + { + scm = new Scm(); + scm.setConnection( repo.getConnection() ); + scm.setDeveloperConnection( repo.getDeveloperConnection() ); + scm.setUrl( repo.getUrl() ); + } + + return scm; + } + +/* + private Reporting translateReports( List v3Reports, Reporter reporter ) + throws ReportWriteException + { + Reporting reports = null; + if ( v3Reports != null && !v3Reports.isEmpty() ) + { + reports = new Reporting(); + for ( Iterator it = v3Reports.iterator(); it.hasNext(); ) + { + String reportName = (String) it.next(); + + Pattern pluginNamePattern = Pattern.compile( "maven-(.+)-plugin" ); + Matcher matcher = pluginNamePattern.matcher( reportName ); + + String reportPluginName; + if ( !matcher.matches() ) + { + warnings.add( + "Non-standard report name: \'" + reportName + "\'. Using entire name for plugin artifactId." ); + + reportPluginName = reportName; + } + else + { + reportPluginName = matcher.group( 1 ); + } + + ReportPlugin reportPlugin = new ReportPlugin(); + + reportPlugin.setGroupId( "maven" ); + + reportPlugin.setArtifactId( reportPluginName ); + + StringBuffer info = new StringBuffer(); + + info.append( "Using some derived information for report: \'" ).append( reportName ).append( "\'.\n" ) + .append( "\to groupId: \'maven\'\n" ).append( "\to artifactId: \'" ).append( reportPluginName ) + .append( "\'\n" ).append( "\to goal: \'report\'\n" ) + .append( "\n" ) + .append( "These values were extracted using the v3 report naming convention, but may be wrong." ); + + warnings.add( info.toString() ); + + reports.addPlugin( reportPlugin ); + } + } + + return reports; + } +*/ + + private Organization translateOrganization( org.apache.maven.model.v3_0_0.Organization v3Organization ) + { + Organization organization = null; + + if ( v3Organization != null ) + { + organization = new Organization(); + + organization.setName( v3Organization.getName() ); + organization.setUrl( v3Organization.getUrl() ); + + if ( StringUtils.isNotEmpty( v3Organization.getLogo() ) ) + { + warnings.add( "Ignoring . It is not supported in v4 POMs." ); + } + } + + return organization; + } + + private List translateMailingLists( List v3MailingLists ) + { + List mailingLists = new ArrayList(); + + if ( notEmpty( v3MailingLists ) ) + { + for ( Iterator it = v3MailingLists.iterator(); it.hasNext(); ) + { + org.apache.maven.model.v3_0_0.MailingList v3List = (org.apache.maven.model.v3_0_0.MailingList) it + .next(); + MailingList list = new MailingList(); + list.setArchive( v3List.getArchive() ); + list.setName( v3List.getName() ); + list.setSubscribe( v3List.getSubscribe() ); + list.setUnsubscribe( v3List.getUnsubscribe() ); + + mailingLists.add( list ); + } + } + + return mailingLists; + } + + private List translateLicenses( List v3Licenses ) + { + List licenses = new ArrayList(); + + if ( notEmpty( v3Licenses ) ) + { + for ( Iterator it = v3Licenses.iterator(); it.hasNext(); ) + { + org.apache.maven.model.v3_0_0.License v3License = (org.apache.maven.model.v3_0_0.License) it.next(); + License license = new License(); + license.setComments( v3License.getComments() ); + license.setName( v3License.getName() ); + license.setUrl( v3License.getUrl() ); + + licenses.add( license ); + } + } + + return licenses; + } + + private IssueManagement translateIssueManagement( org.apache.maven.model.v3_0_0.Model v3Model ) + { + IssueManagement issueMgmt = null; + + String issueTrackingUrl = v3Model.getIssueTrackingUrl(); + if ( StringUtils.isNotEmpty( issueTrackingUrl ) ) + { + issueMgmt = new IssueManagement(); + issueMgmt.setUrl( issueTrackingUrl ); + } + + return issueMgmt; + } + + private DistributionManagement translateDistributionManagement( PomKey pomKey, + org.apache.maven.model.v3_0_0.Model v3Model ) + throws PomTranslationException + { + DistributionManagement distributionManagement = new DistributionManagement(); + + Site site = null; + + String siteAddress = v3Model.getSiteAddress(); + + String siteDirectory = v3Model.getSiteDirectory(); + + if ( StringUtils.isEmpty( siteAddress ) ) + { + if ( !StringUtils.isEmpty( siteDirectory ) ) + { + site = new Site(); + + site.setId( "default" ); + + site.setName( "Default Site" ); + + site.setUrl( "file://" + siteDirectory ); + } + } + else + { + if ( StringUtils.isEmpty( siteDirectory ) ) + { + throw new PomTranslationException( pomKey.groupId(), pomKey.artifactId(), pomKey.version(), + "Missing 'siteDirectory': Both siteAddress and siteDirectory must be set at the same time." ); + } + + site = new Site(); + + site.setId( "default" ); + + site.setName( "Default Site" ); + + site.setUrl( "scp://" + siteAddress + "/" + siteDirectory ); + } + + distributionManagement.setSite( site ); + + String distributionSite = v3Model.getDistributionSite(); + + String distributionDirectory = v3Model.getDistributionDirectory(); + + DeploymentRepository repository = null; + + if ( StringUtils.isEmpty( distributionSite ) ) + { + if ( !StringUtils.isEmpty( distributionDirectory ) ) + { + repository = new DeploymentRepository(); + + repository.setId( "default" ); + + repository.setName( "Default Repository" ); + + repository.setUrl( "file://" + distributionDirectory ); + // throw new Exception( "Missing 'distributionSite': Both distributionSite and + // distributionDirectory must be set." ); + } + } + else + { + if ( StringUtils.isEmpty( distributionDirectory ) ) + { + throw new PomTranslationException( pomKey.groupId(), pomKey.artifactId(), pomKey.version(), + "Missing 'distributionDirectory': must be set is 'distributionSite' is set." ); + } + + repository = new DeploymentRepository(); + + repository.setId( "default" ); + + repository.setName( "Default Repository" ); + + repository.setUrl( distributionSite + "/" + distributionDirectory ); + } + + distributionManagement.setRepository( repository ); + + distributionManagement.setStatus( "converted" ); + + if ( site == null && repository == null ) + { + return null; + } + + return distributionManagement; + } + + private List translateDevelopers( List v3Developers ) + { + List developers = new ArrayList(); + + if ( notEmpty( v3Developers ) ) + { + for ( Iterator it = v3Developers.iterator(); it.hasNext(); ) + { + org.apache.maven.model.v3_0_0.Developer v3Developer = (org.apache.maven.model.v3_0_0.Developer) it + .next(); + + Developer developer = new Developer(); + + developer.setEmail( v3Developer.getEmail() ); + developer.setId( v3Developer.getId() ); + developer.setName( v3Developer.getName() ); + developer.setOrganization( v3Developer.getOrganization() ); + developer.setRoles( v3Developer.getRoles() ); + developer.setTimezone( v3Developer.getTimezone() ); + developer.setUrl( v3Developer.getUrl() ); + + developers.add( developer ); + } + } + + return developers; + } + + private List translateDependencies( List v3Deps ) + { + List deps = new ArrayList(); + + if ( notEmpty( v3Deps ) ) + { + for ( Iterator it = v3Deps.iterator(); it.hasNext(); ) + { + org.apache.maven.model.v3_0_0.Dependency v3Dep = (org.apache.maven.model.v3_0_0.Dependency) it.next(); + + String groupId = format( v3Dep.getGroupId() ); + String artifactId = format( v3Dep.getArtifactId() ); + + String id = v3Dep.getId(); + + if ( StringUtils.isNotEmpty( id ) ) + { + if ( StringUtils.isEmpty( groupId ) ) + { + int plusIdx = id.indexOf( "+" ); + + if ( plusIdx > -1 ) + { + groupId = id.substring( 0, plusIdx ); + } + else + { + groupId = id; + } + } + + if ( StringUtils.isEmpty( artifactId ) ) + { + artifactId = format( id ); + } + } + + String type = v3Dep.getType(); + if ( "plugin".equals( type ) ) + { + if ( "maven".equals( groupId ) ) + { + groupId = "org.apache.maven.plugins"; + } + + Plugin plugin = new Plugin(); + plugin.setGroupId( groupId ); + plugin.setArtifactId( artifactId ); + plugin.setVersion( format( v3Dep.getVersion() ) ); + + Xpp3Dom config = new Xpp3Dom( "configuration" ); + + Properties props = v3Dep.getProperties(); + + if ( !props.isEmpty() ) + { + for ( Iterator propertyIterator = props.keySet().iterator(); propertyIterator.hasNext(); ) + { + String key = (String) propertyIterator.next(); + String value = props.getProperty( key ); + + Xpp3Dom child = new Xpp3Dom( key ); + child.setValue( value ); + + config.addChild( child ); + } + } + + plugin.setConfiguration( config ); + + this.discoveredPlugins.add( plugin ); + } + else + { + Dependency dep = new Dependency(); + + dep.setGroupId( groupId ); + dep.setArtifactId( artifactId ); + dep.setVersion( v3Dep.getVersion() ); + dep.setType( v3Dep.getType() ); + + String scope = v3Dep.getProperty( "scope" ); + if ( StringUtils.isNotEmpty( scope ) ) + { + dep.setScope( scope ); + } + + String optional = v3Dep.getProperty( "optional" ); + if ( StringUtils.isNotEmpty( optional ) ) + { + dep.setOptional( Boolean.parseBoolean( optional ) ); + } + + deps.add( dep ); + } + } + } + + return deps; + } + + private List translateContributors( List v3Contributors ) + { + List contributors = new ArrayList(); + + if ( notEmpty( v3Contributors ) ) + { + for ( Iterator it = v3Contributors.iterator(); it.hasNext(); ) + { + org.apache.maven.model.v3_0_0.Contributor v3Contributor = (org.apache.maven.model.v3_0_0.Contributor) it + .next(); + + Contributor contributor = new Contributor(); + + contributor.setEmail( v3Contributor.getEmail() ); + contributor.setName( v3Contributor.getName() ); + contributor.setOrganization( v3Contributor.getOrganization() ); + contributor.setRoles( v3Contributor.getRoles() ); + contributor.setTimezone( v3Contributor.getTimezone() ); + contributor.setUrl( v3Contributor.getUrl() ); + + contributors.add( contributor ); + } + } + + return contributors; + } + + private Build translateBuild( org.apache.maven.model.v3_0_0.Build v3Build ) + { + Build build = null; + if ( v3Build != null ) + { + build = new Build(); + + warnOfUnsupportedBuildElements( v3Build ); + + build.setSourceDirectory( v3Build.getSourceDirectory() ); + build.setTestSourceDirectory( v3Build.getUnitTestSourceDirectory() ); + + build.setResources( translateResources( v3Build.getResources() ) ); + + UnitTest unitTest = v3Build.getUnitTest(); + if ( unitTest != null ) + { + build.setTestResources( translateResources( unitTest.getResources() ) ); + + List testIncludes = unitTest.getIncludes(); + + List testExcludes = new ArrayList( unitTest.getExcludes() ); + + if ( notEmpty( testIncludes ) || notEmpty( testExcludes ) ) + { + Plugin plugin = new Plugin(); + plugin.setGroupId( "org.apache.maven.plugins" ); + plugin.setArtifactId( "maven-surefire-plugin" ); + + Xpp3Dom config = new Xpp3Dom( "configuration" ); + + if ( notEmpty( testIncludes ) ) + { + Xpp3Dom includes = new Xpp3Dom( "includes" ); + for ( Iterator it = testIncludes.iterator(); it.hasNext(); ) + { + String includePattern = (String) it.next(); + Xpp3Dom include = new Xpp3Dom( "include" ); + include.setValue( includePattern ); + + includes.addChild( include ); + } + + config.addChild( includes ); + } + + if ( notEmpty( testExcludes ) ) + { + Xpp3Dom excludes = new Xpp3Dom( "excludes" ); + for ( Iterator it = testExcludes.iterator(); it.hasNext(); ) + { + String excludePattern = (String) it.next(); + Xpp3Dom exclude = new Xpp3Dom( "exclude" ); + exclude.setValue( excludePattern ); + + excludes.addChild( exclude ); + } + + config.addChild( excludes ); + } + + if ( config.getChildCount() > 0 ) + { + plugin.setConfiguration( config ); + } + + build.addPlugin( plugin ); + } + } + } + + if ( !this.discoveredPlugins.isEmpty() ) + { + if ( build == null ) + { + build = new Build(); + } + + for ( Iterator it = this.discoveredPlugins.iterator(); it.hasNext(); ) + { + Plugin plugin = (Plugin) it.next(); + + build.addPlugin( plugin ); + } + } + + return build; + } + + private void warnOfUnsupportedBuildElements( org.apache.maven.model.v3_0_0.Build v3Build ) + { + if ( notEmpty( v3Build.getSourceModifications() ) ) + { + warnings.add( "Ignoring section. It is not supported in v4 POMs." ); + } + + if ( StringUtils.isNotEmpty( v3Build.getAspectSourceDirectory() ) ) + { + warnings.add( "Ignoring . It is not supported in v4 POMs." ); + } + + if ( StringUtils.isNotEmpty( v3Build.getIntegrationUnitTestSourceDirectory() ) ) + { + warnings.add( "Ignoring . It is not supported in v4 POMs." ); + } + } + + private List translateResources( List v3Resources ) + { + List resources = new ArrayList(); + + if ( notEmpty( v3Resources ) ) + { + for ( Iterator it = v3Resources.iterator(); it.hasNext(); ) + { + org.apache.maven.model.v3_0_0.Resource v3Resource = (org.apache.maven.model.v3_0_0.Resource) it.next(); + Resource resource = new Resource(); + + if ( v3Resource.getDirectory() == null ) + { + resource.setDirectory( "." ); + } + else + { + resource.setDirectory( v3Resource.getDirectory() ); + } + + List excludes = new ArrayList( v3Resource.getExcludes() ); + + resource.setExcludes( excludes ); + + resource.setIncludes( v3Resource.getIncludes() ); + resource.setTargetPath( v3Resource.getTargetPath() ); + + resources.add( resource ); + } + } + + return resources; + } + + // private String pathPatternsToString( List patterns ) + // { + // StringBuffer result = new StringBuffer(); + // + // if ( notEmpty( patterns ) ) + // { + // for ( Iterator it = patterns.iterator(); it.hasNext(); ) + // { + // String pattern = (String) it.next(); + // + // result.append( "," ).append( pattern ); + // } + // + // result.setLength( result.length() - 1 ); + // } + // + // return result.toString(); + // } + + private boolean notEmpty( List test ) + { + return test != null && !test.isEmpty(); + } + + public void validateV4Basics( Model model, String groupId, String artifactId, String version, String packaging ) + { + if ( StringUtils.isEmpty( model.getModelVersion() ) ) + { + model.setModelVersion( "4.0.0" ); + } + + if ( StringUtils.isEmpty( model.getGroupId() ) ) + { + warnings.add( "Setting groupId on model using artifact information." ); + model.setGroupId( groupId ); + } + + if ( StringUtils.isEmpty( model.getArtifactId() ) ) + { + warnings.add( "Setting artifactId on model using artifact information." ); + model.setArtifactId( artifactId ); + } + + if ( StringUtils.isEmpty( model.getVersion() ) ) + { + warnings.add( "Setting version on model using artifact information." ); + model.setVersion( version ); + } + + if ( StringUtils.isEmpty( model.getPackaging() ) ) + { + warnings.add( "Setting packaging on model using artifact type information." ); + model.setPackaging( packaging ); + } + } + + public List getWarnings() + { + return warnings; + } + + private static class PomKey + { + private final String groupId; + + private final String artifactId; + + private final String version; + + PomKey( String groupId, String artifactId, String version ) + { + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + } + + public String groupId() + { + return groupId; + } + + public String artifactId() + { + return artifactId; + } + + public String version() + { + return version; + } + } + +} \ No newline at end of file diff --git a/maven-model-converter/src/main/java/org/apache/maven/model/converter/V3PomRewriter.java b/maven-model-converter/src/main/java/org/apache/maven/model/converter/V3PomRewriter.java new file mode 100644 index 0000000000..2d6af2a681 --- /dev/null +++ b/maven-model-converter/src/main/java/org/apache/maven/model/converter/V3PomRewriter.java @@ -0,0 +1,78 @@ +package org.apache.maven.model.converter; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.model.Model; +import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; + +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.List; + +/** + * @author jdcasey + */ +public class V3PomRewriter + implements ArtifactPomRewriter +{ + private ModelConverter translator; + + public void rewrite( Reader from, Writer to, boolean reportOnly, String groupId, String artifactId, String version, + String packaging ) + throws Exception + { + Model v4Model; + + if ( from != null ) + { + MavenXpp3Reader v3Reader = new MavenXpp3Reader(); + + StringWriter w = new StringWriter(); + IOUtil.copy( from, w ); + String content = StringUtils.replace( w.toString(), "${pom.currentVersion}", "${project.version}" ); + + org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( new StringReader( content ) ); + v4Model = translator.translate( v3Model ); + } + else + { + v4Model = new Model(); + } + + if ( v4Model != null ) + { + translator.validateV4Basics( v4Model, groupId, artifactId, version, packaging ); + + if ( !reportOnly ) + { + MavenXpp3Writer v4Writer = new MavenXpp3Writer(); + v4Writer.write( to, v4Model ); + } + } + } + + public List getWarnings() + { + return translator.getWarnings(); + } + +} \ No newline at end of file diff --git a/maven-model-converter/src/main/java/org/apache/maven/model/converter/V4PomRewriter.java b/maven-model-converter/src/main/java/org/apache/maven/model/converter/V4PomRewriter.java new file mode 100644 index 0000000000..6463877031 --- /dev/null +++ b/maven-model-converter/src/main/java/org/apache/maven/model/converter/V4PomRewriter.java @@ -0,0 +1,69 @@ +package org.apache.maven.model.converter; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; + +import java.io.Reader; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +/** + * @author jdcasey + */ +public class V4PomRewriter + implements ArtifactPomRewriter +{ + private ModelConverter translator; + + public void rewrite( Reader from, Writer to, boolean reportOnly, String groupId, String artifactId, String version, + String packaging ) + throws Exception + { + Model model = null; + + if ( from != null ) + { + MavenXpp3Reader reader = new MavenXpp3Reader(); + + model = reader.read( from ); + } + else + { + model = new Model(); + } + + if ( model != null ) + { + translator.validateV4Basics( model, groupId, artifactId, version, packaging ); + + if ( !reportOnly ) + { + MavenXpp3Writer writer = new MavenXpp3Writer(); + writer.write( to, model ); + } + } + } + + public List getWarnings() + { + return translator.getWarnings(); + } +} \ No newline at end of file diff --git a/maven-model-converter/src/main/resources/META-INF/plexus/components.xml b/maven-model-converter/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000000..61b3869378 --- /dev/null +++ b/maven-model-converter/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,30 @@ + + + + org.apache.maven.model.converter.ArtifactPomRewriter + v3 + org.apache.maven.model.converter.V3PomRewriter + + + org.apache.maven.model.converter.ModelConverter + + + + + + org.apache.maven.model.converter.ArtifactPomRewriter + v4 + org.apache.maven.model.converter.V4PomRewriter + + + org.apache.maven.model.converter.ModelConverter + + + + + + org.apache.maven.model.converter.ModelConverter + org.apache.maven.model.converter.PomV3ToV4Translator + + + diff --git a/maven-model-converter/src/test/java/org/apache/maven/model/converter/PomV3ToV4TranslatorTest.java b/maven-model-converter/src/test/java/org/apache/maven/model/converter/PomV3ToV4TranslatorTest.java new file mode 100644 index 0000000000..7d04606ed5 --- /dev/null +++ b/maven-model-converter/src/test/java/org/apache/maven/model/converter/PomV3ToV4TranslatorTest.java @@ -0,0 +1,155 @@ +package org.apache.maven.model.converter; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import junit.framework.Assert; +import org.apache.maven.model.Build; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.Resource; +import org.codehaus.plexus.PlexusTestCase; + +import java.util.Arrays; + +public class PomV3ToV4TranslatorTest + extends PlexusTestCase +{ + + private ModelConverter translator; + + private org.apache.maven.model.v3_0_0.Dependency v3Dep; + + private org.apache.maven.model.v3_0_0.Model v3Model; + + protected void setUp() + throws Exception + { + super.setUp(); + translator = (ModelConverter) lookup( ModelConverter.ROLE ); + + v3Dep = new org.apache.maven.model.v3_0_0.Dependency(); + v3Dep.setGroupId( "testGroup" ); + v3Dep.setArtifactId( "testArtifact" ); + v3Dep.setVersion( "1.0" ); + + v3Model = new org.apache.maven.model.v3_0_0.Model(); + v3Model.setBuild( new org.apache.maven.model.v3_0_0.Build() ); + } + + public void testConvertedEmptyResourceDirectory() + throws PomTranslationException + { + org.apache.maven.model.v3_0_0.Resource v3Resource = new org.apache.maven.model.v3_0_0.Resource(); + v3Resource.setIncludes( Arrays.asList( new String[]{"**/*.properties"} ) ); + v3Model.getBuild().addResource( v3Resource ); + + Model result = translator.translate( v3Model ); + Resource resource = (Resource) result.getBuild().getResources().get( 0 ); + Assert.assertEquals( "check directory of v3Resource", ".", resource.getDirectory() ); + } + + public void testShouldConvertScopePropertyToDependencyScope() + throws Exception + { + v3Dep.addProperty( "scope", "test" ); + + v3Model.addDependency( v3Dep ); + + Model result = translator.translate( v3Model ); + Assert.assertEquals( "test", ( (Dependency) result.getDependencies().get( 0 ) ).getScope() ); + + } + + public void testShouldConvertOptionalPropertyToDependencyOptional() + throws Exception + { + v3Dep.addProperty( "optional", "true" ); + + v3Model.addDependency( v3Dep ); + + Model result = translator.translate( v3Model ); + Assert.assertTrue( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() ); + + v3Dep.addProperty( "optional", "TRUE" ); + + v3Model.addDependency( v3Dep ); + + result = translator.translate( v3Model ); + Assert.assertTrue( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() ); + } + + public void testDontBreakWithMalformedOptionalProperty() + throws Exception + { + v3Dep.addProperty( "optional", "xxx" ); + + v3Model.addDependency( v3Dep ); + + Model result = translator.translate( v3Model ); + Assert.assertFalse( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() ); + + v3Dep.addProperty( "optional", "" ); + + v3Model.addDependency( v3Dep ); + + result = translator.translate( v3Model ); + Assert.assertFalse( ( (Dependency) result.getDependencies().get( 0 ) ).isOptional() ); + } + + public void testShouldConvertDependencyWithTypePluginToBuildPluginEntry() + throws Exception + { + v3Dep.setType( "plugin" ); + + v3Model.addDependency( v3Dep ); + + Model result = translator.translate( v3Model ); + + Build build = result.getBuild(); + + Plugin plugin = (Plugin) build.getPlugins().get( 0 ); + + Assert.assertEquals( "testGroup", plugin.getGroupId() ); + Assert.assertEquals( "testArtifact", plugin.getArtifactId() ); + Assert.assertEquals( "1.0", plugin.getVersion() ); + + Assert.assertEquals( "check no dependencies", 0, result.getDependencies().size() ); + } + + public void testShouldConvertDependencyWithTypePluginAndGroupMavenToBuildPluginEntryWithOAMPluginsGroup() + throws Exception + { + v3Dep.setGroupId( "maven" ); + v3Dep.setType( "plugin" ); + + v3Model.addDependency( v3Dep ); + + Model result = translator.translate( v3Model ); + + Build build = result.getBuild(); + + Plugin plugin = (Plugin) build.getPlugins().get( 0 ); + + Assert.assertEquals( "org.apache.maven.plugins", plugin.getGroupId() ); + Assert.assertEquals( "testArtifact", plugin.getArtifactId() ); + Assert.assertEquals( "1.0", plugin.getVersion() ); + + Assert.assertEquals( "check no dependencies", 0, result.getDependencies().size() ); + } + +} diff --git a/maven-model-converter/src/test/java/org/apache/maven/model/converter/V3PomRewriterTest.java b/maven-model-converter/src/test/java/org/apache/maven/model/converter/V3PomRewriterTest.java new file mode 100644 index 0000000000..e1251d5d19 --- /dev/null +++ b/maven-model-converter/src/test/java/org/apache/maven/model/converter/V3PomRewriterTest.java @@ -0,0 +1,60 @@ +package org.apache.maven.model.converter; + +/* + * Copyright 2005-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; + +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; + +/** + * Test rewriter. + * + * @author Brett Porter + * @version $Id$ + */ +public class V3PomRewriterTest + extends PlexusTestCase +{ + private V3PomRewriter rewriter; + + protected void setUp() + throws Exception + { + super.setUp(); + + rewriter = (V3PomRewriter) lookup( V3PomRewriter.ROLE ); + } + + public void testCurrentVersionExpressionConversion() + throws Exception + { + String pom = + "ga${pom.currentVersion}"; + + Writer to = new StringWriter(); + rewriter.rewrite( new StringReader( pom ), to, false, null, null, null, null ); + + Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( to.toString() ) ); + String version = dom.getChild( "dependencies" ).getChild( "dependency" ).getChild( "version" ).getValue(); + assertEquals( "check new version expression", "${project.version}", version ); + } + +} diff --git a/pom.xml b/pom.xml index fc414569e5..a8d086d734 100644 --- a/pom.xml +++ b/pom.xml @@ -173,6 +173,7 @@ maven-core maven-error-diagnostics maven-model + maven-model-converter maven-monitor maven-plugin-api maven-plugin-descriptor