From c327c9444383214a04a8d863fde89b1d494bd813 Mon Sep 17 00:00:00 2001 From: Britton Isbell Date: Thu, 19 Feb 2009 21:22:46 +0000 Subject: [PATCH] Refactored out more uses of modello and moved classes from maven-project to maven-project-builder. Doing this so that maven-mercury will not have direct dependency on modello or maven model. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@746002 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/plugin/DefaultPluginManager.java | 40 ++- .../apache/maven/embedder/MavenEmbedder.java | 1 - maven-project-builder/pom.xml | 4 + .../builder/IPomClassicDomainModel.java | 12 - .../project/builder/IProjectBuilder.java | 6 - .../builder/PomClassicDomainModel.java | 233 ++++++++---------- .../builder/PomClassicDomainModelFactory.java | 4 - .../maven/project/builder/PomTransformer.java | 2 +- .../project/builder/DefaultDomainModel.java | 4 +- .../project/DefaultMavenProjectBuilder.java | 10 +- .../DefaultPomArtifactResolver.java | 3 +- .../impl => }/DefaultProjectBuilder.java | 97 +++++--- .../{builder => }/PomArtifactResolver.java | 2 +- .../project/{builder => }/ProjectBuilder.java | 8 +- .../maven/project/builder/Interpolator.java | 46 ---- .../builder/impl/DefaultImportModel.java | 86 ------- .../project/builder/PomConstructionTest.java | 18 +- .../maven/project/harness/PomTestWrapper.java | 56 +++-- .../parent-inheritance/child3.xml | 6 + .../parent-inheritance/pom.xml | 10 + .../parent-inheritance/sub/pom.xml | 9 + .../maven/project/ProjectClasspathTest.xml | 6 +- .../canonical/CanonicalProjectBuilderTest.xml | 6 +- 23 files changed, 307 insertions(+), 362 deletions(-) delete mode 100644 maven-project-builder/src/main/java/org/apache/maven/project/builder/IPomClassicDomainModel.java delete mode 100644 maven-project-builder/src/main/java/org/apache/maven/project/builder/IProjectBuilder.java rename {maven-project => maven-project-builder}/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java (67%) rename {maven-project => maven-project-builder}/src/main/java/org/apache/maven/project/builder/PomClassicDomainModelFactory.java (71%) rename maven-project/src/main/java/org/apache/maven/project/{builder => }/DefaultPomArtifactResolver.java (97%) rename maven-project/src/main/java/org/apache/maven/project/{builder/impl => }/DefaultProjectBuilder.java (89%) rename maven-project/src/main/java/org/apache/maven/project/{builder => }/PomArtifactResolver.java (81%) rename maven-project/src/main/java/org/apache/maven/project/{builder => }/ProjectBuilder.java (90%) delete mode 100644 maven-project/src/main/java/org/apache/maven/project/builder/Interpolator.java delete mode 100644 maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultImportModel.java create mode 100644 maven-project/src/test/resources-project-builder/parent-inheritance/child3.xml create mode 100644 maven-project/src/test/resources-project-builder/parent-inheritance/pom.xml create mode 100644 maven-project/src/test/resources-project-builder/parent-inheritance/sub/pom.xml diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java index 25e2ddd42f..0cb3608a56 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java @@ -21,6 +21,7 @@ package org.apache.maven.plugin; import java.io.IOException; import java.io.StringReader; +import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -65,20 +66,19 @@ import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder; import org.apache.maven.plugin.version.PluginVersionManager; import org.apache.maven.plugin.version.PluginVersionNotFoundException; import org.apache.maven.plugin.version.PluginVersionResolutionException; -import org.apache.maven.project.DuplicateArtifactAttachmentException; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.project.*; +import org.apache.maven.project.builder.*; +import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.artifact.InvalidDependencyVersionException; import org.apache.maven.project.artifact.MavenMetadataSource; -import org.apache.maven.project.builder.Interpolator; -import org.apache.maven.project.builder.PomInterpolatorTag; -import org.apache.maven.project.builder.ProjectBuilder; import org.apache.maven.project.path.PathTranslator; import org.apache.maven.realm.MavenRealmManager; import org.apache.maven.realm.RealmManagementException; import org.apache.maven.reporting.MavenReport; import org.apache.maven.shared.model.InterpolatorProperty; +import org.apache.maven.shared.model.ModelProperty; +import org.apache.maven.shared.model.ModelMarshaller; +import org.apache.maven.shared.model.ModelTransformerContext; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.annotations.Component; @@ -575,7 +575,7 @@ public class DefaultPluginManager interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( session.getProjectBuilderConfiguration().getUserProperties(), PomInterpolatorTag.USER_PROPERTIES.name())); String interpolatedDom = - Interpolator.interpolateXmlString( String.valueOf( dom ), interpolatorProperties ); + interpolateXmlString( String.valueOf( dom ), interpolatorProperties ); dom = Xpp3DomBuilder.build( new StringReader( interpolatedDom ) ); } catch ( XmlPullParserException e ) @@ -1685,4 +1685,28 @@ public class DefaultPluginManager } } } + + private static String interpolateXmlString( String xml, List interpolatorProperties ) + throws IOException + { + List modelProperties = ModelMarshaller.marshallXmlToModelProperties( new ByteArrayInputStream( xml.getBytes() ), ProjectUri.baseUri, PomTransformer.URIS ); + + Map aliases = new HashMap(); + aliases.put( "project.", "pom." ); + + List ips = new ArrayList( interpolatorProperties ); + ips.addAll( ModelTransformerContext.createInterpolatorProperties( modelProperties, ProjectUri.baseUri, aliases, PomInterpolatorTag.PROJECT_PROPERTIES.name(), false, false ) ); + + for ( ModelProperty mp : modelProperties ) + { + if ( mp.getUri().startsWith( ProjectUri.properties ) && mp.getValue() != null ) + { + String uri = mp.getUri(); + ips.add( new InterpolatorProperty( "${" + uri.substring( uri.lastIndexOf( "/" ) + 1, uri.length() ) + "}", mp.getValue() ) ); + } + } + + ModelTransformerContext.interpolateModelProperties( modelProperties, ips ); + return ModelMarshaller.unmarshalModelPropertiesToXml( modelProperties, ProjectUri.baseUri ); + } } diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java index 9fc4b87ec6..3afb17fe56 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java @@ -62,7 +62,6 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.MavenProjectBuildingResult; import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.project.builder.ProjectBuilder; import org.apache.maven.plugin.PluginRepository; import org.apache.maven.reactor.MavenExecutionException; import org.apache.maven.reactor.MissingModuleException; diff --git a/maven-project-builder/pom.xml b/maven-project-builder/pom.xml index 7973a659e0..79b86f920b 100644 --- a/maven-project-builder/pom.xml +++ b/maven-project-builder/pom.xml @@ -26,6 +26,10 @@ org.sonatype.spice model-builder + + org.codehaus.plexus + plexus-utils + junit junit diff --git a/maven-project-builder/src/main/java/org/apache/maven/project/builder/IPomClassicDomainModel.java b/maven-project-builder/src/main/java/org/apache/maven/project/builder/IPomClassicDomainModel.java deleted file mode 100644 index dcbdf8a0c4..0000000000 --- a/maven-project-builder/src/main/java/org/apache/maven/project/builder/IPomClassicDomainModel.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.apache.maven.project.builder; - -import org.apache.maven.shared.model.InputStreamDomainModel; - -import java.io.File; - -public interface IPomClassicDomainModel extends InputStreamDomainModel -{ - boolean isPomInBuild(); - - File getProjectDirectory(); -} diff --git a/maven-project-builder/src/main/java/org/apache/maven/project/builder/IProjectBuilder.java b/maven-project-builder/src/main/java/org/apache/maven/project/builder/IProjectBuilder.java deleted file mode 100644 index 65ae30439c..0000000000 --- a/maven-project-builder/src/main/java/org/apache/maven/project/builder/IProjectBuilder.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.apache.maven.project.builder; - -public interface IProjectBuilder -{ - -} diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java b/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java similarity index 67% rename from maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java rename to maven-project-builder/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java index 5d4d356476..d5b299d14f 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java +++ b/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomClassicDomainModel.java @@ -19,25 +19,17 @@ package org.apache.maven.project.builder; * under the License. */ -import org.apache.maven.model.Model; -import org.apache.maven.model.Parent; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.model.io.xpp3.MavenXpp3Writer; -import org.apache.maven.shared.model.InputStreamDomainModel; import org.apache.maven.shared.model.ModelProperty; import org.apache.maven.shared.model.ModelMarshaller; +import org.apache.maven.shared.model.InputStreamDomainModel; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.ReaderFactory; -import org.codehaus.plexus.util.WriterFactory; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.Writer; import java.util.List; import java.util.ArrayList; import java.util.Set; @@ -46,8 +38,7 @@ import java.util.HashSet; /** * Provides a wrapper for the maven model. */ -public final class PomClassicDomainModel - implements IPomClassicDomainModel +public class PomClassicDomainModel implements InputStreamDomainModel { /** @@ -60,11 +51,6 @@ public final class PomClassicDomainModel */ private String eventHistory; - /** - * Maven model - */ - private Model model; - private String id; private File file; @@ -77,44 +63,18 @@ public final class PomClassicDomainModel private int lineageCount; - public PomClassicDomainModel( List modelProperties) + private String parentGroupId = null, parentArtifactId = null, parentVersion = null, parentId = null, parentRelativePath; + + public PomClassicDomainModel( List modelProperties ) { this.modelProperties = modelProperties; + try { inputBytes = IOUtil.toByteArray( ModelMarshaller.unmarshalModelPropertiesToXml(modelProperties, ProjectUri.baseUri)); } catch (IOException e) { } - } - /** - * Constructor - * - * @param model maven model - * @throws IOException if there is a problem constructing the model - */ - public PomClassicDomainModel( Model model ) - throws IOException - { - if ( model == null ) - { - throw new IllegalArgumentException( "model: null" ); - } - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Writer out = null; - MavenXpp3Writer writer = new MavenXpp3Writer(); - try - { - out = WriterFactory.newXmlWriter( baos ); - writer.write( out, model ); - } - finally - { - if ( out != null ) - { - out.close(); - } - } - inputBytes = baos.toByteArray(); + initializeProperties( modelProperties ); } /** @@ -131,6 +91,76 @@ public final class PomClassicDomainModel throw new IllegalArgumentException( "inputStream: null" ); } this.inputBytes = IOUtil.toByteArray( inputStream ); + modelProperties = getModelProperties(); + initializeProperties( modelProperties ); + } + + private void initializeProperties(List modelProperties) + { + String groupId = null, artifactId = null, version = null; + for(ModelProperty mp : modelProperties) + { + if(mp.getUri().equals(ProjectUri.groupId)) + { + groupId = mp.getResolvedValue(); + } + else if(mp.getUri().equals(ProjectUri.artifactId)) + { + artifactId = mp.getResolvedValue(); + } + else if(mp.getUri().equals(ProjectUri.version)) + { + version = mp.getResolvedValue(); + } + else if(mp.getUri().equals(ProjectUri.Parent.artifactId)) + { + parentArtifactId = mp.getResolvedValue(); + } + else if(mp.getUri().equals(ProjectUri.Parent.groupId)) + { + parentGroupId = mp.getResolvedValue(); + } + else if(mp.getUri().equals(ProjectUri.Parent.version)) + { + parentVersion = mp.getResolvedValue(); + } + else if(mp.getUri().equals(ProjectUri.Parent.relativePath)) + { + parentRelativePath = mp.getResolvedValue(); + } + + + + if(groupId != null && artifactId != null && version != null && parentGroupId != null && + parentArtifactId != null && parentVersion != null & parentRelativePath != null) + { + break; + } + } + if( groupId == null && parentGroupId != null) + { + groupId = parentGroupId; + } + if( artifactId == null && parentArtifactId != null) + { + artifactId = parentArtifactId; + } + if( version == null && parentVersion != null ) + { + version = parentVersion; + } + + if(parentGroupId != null && parentArtifactId != null && parentVersion != null) + { + parentId = parentGroupId + ":" + parentArtifactId + ":" + parentVersion; + } + + if(parentRelativePath == null) + { + parentRelativePath = ".." + File.separator + "pom.xml"; + } + + id = groupId + ":" + artifactId + ":" + version; } public PomClassicDomainModel( File file ) @@ -149,7 +179,19 @@ public final class PomClassicDomainModel { this.parentFile = parentFile; } - + + public String getParentGroupId() { + return parentGroupId; + } + + public String getParentArtifactId() { + return parentArtifactId; + } + + public String getParentVersion() { + return parentVersion; + } + /** * This should only be set for projects that are in the build. Setting for poms in the repo may cause unstable behavior. * @@ -170,72 +212,30 @@ public final class PomClassicDomainModel return projectDirectory != null; } - /** - * Returns true if groupId.equals(a.groupId) && artifactId.equals(a.artifactId) && version.equals(a.version), - * otherwise returns false. - * - * @param a model to compare - * @return true if groupId.equals(a.groupId) && artifactId.equals(a.artifactId) && version.equals(a.version), - * otherwise returns false. - */ - public boolean matchesModel( Model a ) + public String getParentId() throws IOException { - if ( a == null ) - { - throw new IllegalArgumentException( "a: null" ); - } - if ( model == null ) - { - try - { - model = getModel(); - } - catch ( IOException e ) - { - return false; - } - } - return a.getId().equals( this.getId() ); + return parentId; } - public String getId() + public String getRelativePathOfParent() { - if ( id == null ) - { - if ( model == null ) - { - try - { - model = getModel(); - } - catch ( IOException e ) - { - return ""; - } - } - String groupId = ( model.getGroupId() == null && model.getParent() != null ) - ? model.getParent().getGroupId() - : model.getGroupId(); - String artifactId = ( model.getArtifactId() == null && model.getParent() != null ) - ? model.getParent().getArtifactId() - : model.getArtifactId(); - String version = ( model.getVersion() == null && model.getParent() != null ) - ? model.getParent().getVersion() - : model.getVersion(); + return parentRelativePath; + } - id = groupId + ":" + artifactId + ":" + version; - } + public String getId() throws IOException + { return id; } - public boolean matchesParent( Parent parent ) + public boolean matchesParentOf( PomClassicDomainModel domainModel ) throws IOException { - if ( parent == null ) + if ( domainModel == null ) { - throw new IllegalArgumentException( "parent: null" ); + throw new IllegalArgumentException( "domainModel: null" ); } - return getId().equals( parent.getGroupId() + ":" + parent.getArtifactId() + ":" + parent.getVersion() ); + + return getId().equals(domainModel.getParentId()); } /** @@ -256,29 +256,6 @@ public final class PomClassicDomainModel } } - /** - * Returns maven model - * - * @return maven model - */ - public Model getModel() - throws IOException - { - if ( model != null ) - { - return model; - } - try - { - model = new MavenXpp3Reader().read( ReaderFactory.newXmlReader( new ByteArrayInputStream( inputBytes ) ) ); - return model; - } - catch ( XmlPullParserException e ) - { - throw new IOException( e.getMessage() ); - } - } - /** * @see org.apache.maven.shared.model.InputStreamDomainModel#getInputStream() */ @@ -370,7 +347,11 @@ public final class PomClassicDomainModel */ public boolean equals( Object o ) { - return o instanceof PomClassicDomainModel && getId().equals( ( (PomClassicDomainModel) o ).getId() ); + try { + return o instanceof PomClassicDomainModel && getId().equals( ( (PomClassicDomainModel) o ).getId() ); + } catch (IOException e) { + return false; + } } } diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModelFactory.java b/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomClassicDomainModelFactory.java similarity index 71% rename from maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModelFactory.java rename to maven-project-builder/src/main/java/org/apache/maven/project/builder/PomClassicDomainModelFactory.java index 49e0caf37a..e293584423 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicDomainModelFactory.java +++ b/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomClassicDomainModelFactory.java @@ -3,13 +3,9 @@ package org.apache.maven.project.builder; import org.apache.maven.shared.model.DomainModelFactory; import org.apache.maven.shared.model.DomainModel; import org.apache.maven.shared.model.ModelProperty; -import org.apache.maven.shared.model.ModelMarshaller; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.util.List; import java.io.IOException; -import java.io.StringReader; public class PomClassicDomainModelFactory implements DomainModelFactory { diff --git a/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomTransformer.java b/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomTransformer.java index 29720201ef..1a07384936 100644 --- a/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomTransformer.java +++ b/maven-project-builder/src/main/java/org/apache/maven/project/builder/PomTransformer.java @@ -799,7 +799,7 @@ public class PomTransformer DomainModel domainModel ) throws IOException { - IPomClassicDomainModel dm = (IPomClassicDomainModel) domainModel; + PomClassicDomainModel dm = (PomClassicDomainModel) domainModel; if ( !containsProjectVersion( interpolatorProperties ) ) { diff --git a/maven-project-builder/src/test/java/org/apache/maven/project/builder/DefaultDomainModel.java b/maven-project-builder/src/test/java/org/apache/maven/project/builder/DefaultDomainModel.java index 2d27dacb92..2a05115ef8 100644 --- a/maven-project-builder/src/test/java/org/apache/maven/project/builder/DefaultDomainModel.java +++ b/maven-project-builder/src/test/java/org/apache/maven/project/builder/DefaultDomainModel.java @@ -2,17 +2,19 @@ package org.apache.maven.project.builder; import org.apache.maven.shared.model.DomainModel; import org.apache.maven.shared.model.ModelProperty; +import org.apache.maven.shared.model.InputStreamDomainModel; import java.util.List; import java.io.IOException; import java.io.File; import java.io.InputStream; -public class DefaultDomainModel implements IPomClassicDomainModel { +public class DefaultDomainModel extends PomClassicDomainModel { private List modelProperties; public DefaultDomainModel(List modelProperties) { + super( modelProperties); this.modelProperties = modelProperties; } diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 54d5d5e21e..313fc2b50f 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -19,8 +19,7 @@ package org.apache.maven.project; * under the License. */ -import java.io.File; -import java.io.IOException; +import java.io.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; @@ -40,6 +39,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.model.Build; import org.apache.maven.model.Model; import org.apache.maven.model.Profile; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.profiles.MavenProfilesBuilder; import org.apache.maven.profiles.ProfileManager; import org.apache.maven.profiles.activation.DefaultProfileActivationContext; @@ -47,10 +47,7 @@ import org.apache.maven.profiles.activation.ProfileActivationContext; import org.apache.maven.profiles.activation.ProfileActivationException; import org.apache.maven.profiles.build.ProfileAdvisor; import org.apache.maven.project.artifact.InvalidDependencyVersionException; -import org.apache.maven.project.builder.DefaultPomArtifactResolver; -import org.apache.maven.project.builder.PomArtifactResolver; -import org.apache.maven.project.builder.PomInterpolatorTag; -import org.apache.maven.project.builder.ProjectBuilder; +import org.apache.maven.project.builder.*; import org.apache.maven.project.validation.ModelValidationResult; import org.apache.maven.project.validation.ModelValidator; import org.apache.maven.shared.model.InterpolatorProperty; @@ -59,6 +56,7 @@ import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.LogEnabled; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.WriterFactory; /** diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/DefaultPomArtifactResolver.java b/maven-project/src/main/java/org/apache/maven/project/DefaultPomArtifactResolver.java similarity index 97% rename from maven-project/src/main/java/org/apache/maven/project/builder/DefaultPomArtifactResolver.java rename to maven-project/src/main/java/org/apache/maven/project/DefaultPomArtifactResolver.java index 78d13743a1..9fbcb0c409 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/DefaultPomArtifactResolver.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultPomArtifactResolver.java @@ -1,4 +1,4 @@ -package org.apache.maven.project.builder; +package org.apache.maven.project; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -24,6 +24,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.project.PomArtifactResolver; import java.io.File; import java.io.IOException; diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java similarity index 89% rename from maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultProjectBuilder.java rename to maven-project/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 1b2dc766d1..d2ad633975 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -1,4 +1,4 @@ -package org.apache.maven.project.builder.impl; +package org.apache.maven.project; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -19,9 +19,7 @@ package org.apache.maven.project.builder.impl; * under the License. */ -import java.io.File; -import java.io.IOException; -import java.io.Reader; +import java.io.*; import java.util.*; import org.apache.maven.MavenTools; @@ -36,11 +34,9 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.model.Model; -import org.apache.maven.model.Parent; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuilderConfiguration; -import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.apache.maven.project.*; import org.apache.maven.project.builder.*; import org.apache.maven.project.builder.ProjectUri; import org.apache.maven.project.builder.profile.ProfileContext; @@ -52,6 +48,8 @@ import org.codehaus.plexus.logging.LogEnabled; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.WriterFactory; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** * Default implementation of the project builder. @@ -233,10 +231,10 @@ public class DefaultProjectBuilder File parentFile = null; int lineageCount = 0; - if ( domainModel.getModel().getParent() != null ) + if ( domainModel.getParentId() != null ) { List mavenParents; - if ( isParentLocal( domainModel.getModel().getParent(), pom.getParentFile() ) ) + if ( isParentLocal( domainModel.getRelativePathOfParent(), pom.getParentFile() ) ) { mavenParents = getDomainModelParentsFromLocalPath( domainModel, resolver, pom.getParentFile(), properties, @@ -262,7 +260,7 @@ public class DefaultProjectBuilder for ( Model model : mixins ) { - domainModels.add( new PomClassicDomainModel( model ) ); + domainModels.add( convertToDomainModel( model ) ); } PomTransformer transformer = new PomTransformer( new PomClassicDomainModelFactory() ); @@ -281,6 +279,30 @@ public class DefaultProjectBuilder return transformedDomainModel; } + + private PomClassicDomainModel convertToDomainModel(Model model) throws IOException + { + if ( model == null ) + { + throw new IllegalArgumentException( "model: null" ); + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Writer out = null; + MavenXpp3Writer writer = new MavenXpp3Writer(); + try + { + out = WriterFactory.newXmlWriter( baos ); + writer.write( out, model ); + } + finally + { + if ( out != null ) + { + out.close(); + } + } + return new PomClassicDomainModel(new ByteArrayInputStream(baos.toByteArray())); + } public MavenProject buildFromLocalPath( File pom, List mixins, @@ -309,7 +331,7 @@ public class DefaultProjectBuilder try { - MavenProject mavenProject = new MavenProject( domainModel.getModel(), + MavenProject mavenProject = new MavenProject( convertFromInputStreamToModel(domainModel.getInputStream()), artifactFactory, mavenTools, mavenProjectBuilder, @@ -325,18 +347,32 @@ public class DefaultProjectBuilder } } + private static Model convertFromInputStreamToModel(InputStream inputStream) throws IOException + { + + try + { + return new MavenXpp3Reader().read( ReaderFactory.newXmlReader( inputStream ) ); + } + catch ( XmlPullParserException e ) + { + throw new IOException( e.getMessage() ); + } + + } + /** * Returns true if the relative path of the specified parent references a pom, otherwise returns false. * - * @param parent the parent model info + * @param relativePath the parent model info * @param projectDirectory the project directory of the child pom * @return true if the relative path of the specified parent references a pom, otherwise returns fals */ - private boolean isParentLocal( Parent parent, File projectDirectory ) + private boolean isParentLocal( String relativePath, File projectDirectory ) { try { - File f = new File( projectDirectory, parent.getRelativePath() ).getCanonicalFile(); + File f = new File( projectDirectory, relativePath ).getCanonicalFile(); if ( f.isDirectory() ) { @@ -360,23 +396,24 @@ public class DefaultProjectBuilder { List domainModels = new ArrayList(); - Parent parent = domainModel.getModel().getParent(); + String parentId = domainModel.getParentId(); - if ( parent == null ) + if ( parentId == null ) { return domainModels; } - Artifact artifactParent = artifactFactory.createParentArtifact( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); + Artifact artifactParent = artifactFactory.createParentArtifact( domainModel.getParentGroupId(), + domainModel.getParentArtifactId(), domainModel.getParentVersion() ); artifactResolver.resolve( artifactParent ); PomClassicDomainModel parentDomainModel = new PomClassicDomainModel( artifactParent.getFile() ); - if ( !parentDomainModel.matchesParent( domainModel.getModel().getParent() ) ) + if ( !parentDomainModel.matchesParentOf( domainModel ) ) { logger.debug( "Parent pom ids do not match: Parent File = " + artifactParent.getFile().getAbsolutePath() + - ": Child ID = " + domainModel.getModel().getId() ); + ": Child ID = " + domainModel.getId() ); return domainModels; } @@ -428,16 +465,16 @@ public class DefaultProjectBuilder { List domainModels = new ArrayList(); - Parent parent = domainModel.getModel().getParent(); + String parentId = domainModel.getParentId(); - if ( parent == null ) + if ( parentId == null ) { return domainModels; } - Model model = domainModel.getModel(); + // Model model = domainModel.getModel(); - File parentFile = new File( projectDirectory, model.getParent().getRelativePath() ).getCanonicalFile(); + File parentFile = new File( projectDirectory, domainModel.getRelativePathOfParent() ).getCanonicalFile(); if ( parentFile.isDirectory() ) { parentFile = new File( parentFile.getAbsolutePath(), "pom.xml" ); @@ -472,11 +509,11 @@ public class DefaultProjectBuilder domainModels.add(new PomClassicDomainModel(transformed)); } - if ( !parentDomainModel.matchesParent( domainModel.getModel().getParent() ) ) + if ( !parentDomainModel.matchesParentOf( domainModel ) ) { - logger.debug( "Parent pom ids do not match: Parent File = " + parentFile.getAbsolutePath() + ", Parent ID = " + logger.info( "Parent pom ids do not match: Parent File = " + parentFile.getAbsolutePath() + ", Parent ID = " + parentDomainModel.getId() + ", Child ID = " + domainModel.getId() + ", Expected Parent ID = " - + domainModel.getModel().getParent().getId() ); + + domainModel.getParentId() ); List parentDomainModels = getDomainModelParentsFromRepository( domainModel, artifactResolver, properties, activeProfileIds, @@ -485,7 +522,7 @@ public class DefaultProjectBuilder if(parentDomainModels.size() == 0) { throw new IOException("Unable to find parent pom on local path or repo: " - + domainModel.getModel().getParent().getId()); + + domainModel.getParentId()); } domainModels.addAll( parentDomainModels ); @@ -493,9 +530,9 @@ public class DefaultProjectBuilder } domainModels.add( parentDomainModel ); - if ( parentDomainModel.getModel().getParent() != null ) + if ( domainModel.getParentId() != null ) { - if ( isParentLocal( parentDomainModel.getModel().getParent(), parentFile.getParentFile() ) ) + if ( isParentLocal(parentDomainModel.getRelativePathOfParent(), parentFile.getParentFile() ) ) { domainModels.addAll( getDomainModelParentsFromLocalPath( parentDomainModel, artifactResolver, parentFile.getParentFile(), properties, diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/PomArtifactResolver.java b/maven-project/src/main/java/org/apache/maven/project/PomArtifactResolver.java similarity index 81% rename from maven-project/src/main/java/org/apache/maven/project/builder/PomArtifactResolver.java rename to maven-project/src/main/java/org/apache/maven/project/PomArtifactResolver.java index 0a54ebbc6a..39ce51ed21 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/PomArtifactResolver.java +++ b/maven-project/src/main/java/org/apache/maven/project/PomArtifactResolver.java @@ -1,4 +1,4 @@ -package org.apache.maven.project.builder; +package org.apache.maven.project; import java.io.IOException; diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/ProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/ProjectBuilder.java similarity index 90% rename from maven-project/src/main/java/org/apache/maven/project/builder/ProjectBuilder.java rename to maven-project/src/main/java/org/apache/maven/project/ProjectBuilder.java index 3f059a42f3..55b12c123b 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/ProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/ProjectBuilder.java @@ -1,4 +1,4 @@ -package org.apache.maven.project.builder; +package org.apache.maven.project; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -25,17 +25,15 @@ import java.util.Collection; import java.util.List; import org.apache.maven.model.Model; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuilderConfiguration; -import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.shared.model.InterpolatorProperty; +import org.apache.maven.project.builder.PomClassicDomainModel; /** * Provides services for building maven projects from models. */ public interface ProjectBuilder { - public IPomClassicDomainModel buildModel( File pom, + public PomClassicDomainModel buildModel( File pom, Collection interpolatorProperties, PomArtifactResolver resolver ) throws IOException; diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/Interpolator.java b/maven-project/src/main/java/org/apache/maven/project/builder/Interpolator.java deleted file mode 100644 index 35d05cb519..0000000000 --- a/maven-project/src/main/java/org/apache/maven/project/builder/Interpolator.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.apache.maven.project.builder; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.maven.model.Model; -import org.apache.maven.shared.model.InterpolatorProperty; -import org.apache.maven.shared.model.ModelMarshaller; -import org.apache.maven.shared.model.ModelProperty; -import org.apache.maven.shared.model.ModelTransformerContext; - -public class Interpolator -{ - - // Only used by the plugin manager - public static String interpolateXmlString( String xml, List interpolatorProperties ) - throws IOException - { - List modelProperties = ModelMarshaller.marshallXmlToModelProperties( new ByteArrayInputStream( xml.getBytes() ), ProjectUri.baseUri, PomTransformer.URIS ); - - Map aliases = new HashMap(); - aliases.put( "project.", "pom." ); - - List ips = new ArrayList( interpolatorProperties ); - ips.addAll( ModelTransformerContext.createInterpolatorProperties( modelProperties, ProjectUri.baseUri, aliases, PomInterpolatorTag.PROJECT_PROPERTIES.name(), false, false ) ); - - for ( ModelProperty mp : modelProperties ) - { - if ( mp.getUri().startsWith( ProjectUri.properties ) && mp.getValue() != null ) - { - String uri = mp.getUri(); - ips.add( new InterpolatorProperty( "${" + uri.substring( uri.lastIndexOf( "/" ) + 1, uri.length() ) + "}", mp.getValue() ) ); - } - } - - ModelTransformerContext.interpolateModelProperties( modelProperties, ips ); - return ModelMarshaller.unmarshalModelPropertiesToXml( modelProperties, ProjectUri.baseUri ); - } -} \ No newline at end of file diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultImportModel.java b/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultImportModel.java deleted file mode 100644 index 19b572cdc7..0000000000 --- a/maven-project/src/main/java/org/apache/maven/project/builder/impl/DefaultImportModel.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.apache.maven.project.builder.impl; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.shared.model.ImportModel; -import org.apache.maven.shared.model.ModelProperty; - -import java.util.ArrayList; -import java.util.List; - -public final class DefaultImportModel - implements ImportModel -{ - - private final String id; - - private final List modelProperties; - - public DefaultImportModel( String id, List modelProperties ) - { - if ( id == null ) - { - throw new IllegalArgumentException( "id: null" ); - } - - if ( modelProperties == null ) - { - throw new IllegalArgumentException( "modelProperties: null" ); - } - this.id = id; - this.modelProperties = new ArrayList( modelProperties ); - } - - public String getId() - { - return id; - } - - public List getModelProperties() - { - return new ArrayList( modelProperties ); - } - - public boolean equals( Object o ) - { - if ( this == o ) - { - return true; - } - if ( o == null || getClass() != o.getClass() ) - { - return false; - } - - ImportModel that = (ImportModel) o; - - if ( id != null ? !id.equals( that.getId() ) : that.getId() != null ) - { - return false; - } - - return true; - } - - public int hashCode() - { - return ( id != null ? id.hashCode() : 0 ); - } -} diff --git a/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java b/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java index 39d026bfaa..723d3b09f3 100644 --- a/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/builder/PomConstructionTest.java @@ -29,15 +29,12 @@ import org.apache.maven.profiles.DefaultProfileManager; import org.apache.maven.profiles.activation.DefaultProfileActivationContext; import org.apache.maven.profiles.activation.ProfileActivationContext; import org.apache.maven.model.Model; -import org.apache.maven.model.Plugin; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.DefaultArtifactRepository; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; import org.apache.maven.project.harness.PomTestWrapper; -import org.apache.maven.project.MavenProjectBuilder; -import org.apache.maven.project.ProjectBuilderConfiguration; -import org.apache.maven.project.DefaultProjectBuilderConfiguration; +import org.apache.maven.project.*; import org.codehaus.plexus.PlexusTestCase; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -83,6 +80,17 @@ public class PomConstructionTest }; } + /** + * Will throw exception if doesn't find parent(s) in build + * + * @throws Exception + */ + public void testParentInheritance() + throws Exception + { + buildPom( "parent-inheritance/sub" ); + } + /*MNG-3995*/ public void testExecutionConfigurationJoin() throws Exception @@ -112,7 +120,7 @@ public class PomConstructionTest { File pom = new File( testDirectory, "micromailer/micromailer-1.0.3.pom" ); PomArtifactResolver resolver = artifactResolver( "micromailer" ); - IPomClassicDomainModel model = projectBuilder.buildModel( pom, null, resolver ); + PomClassicDomainModel model = projectBuilder.buildModel( pom, null, resolver ); // This should be 2 //assertEquals( 2, model.getLineageCount() ); PomTestWrapper tester = new PomTestWrapper( (PomClassicDomainModel) model ); diff --git a/maven-project/src/test/java/org/apache/maven/project/harness/PomTestWrapper.java b/maven-project/src/test/java/org/apache/maven/project/harness/PomTestWrapper.java index ec39d1731b..d1836cd817 100644 --- a/maven-project/src/test/java/org/apache/maven/project/harness/PomTestWrapper.java +++ b/maven-project/src/test/java/org/apache/maven/project/harness/PomTestWrapper.java @@ -19,8 +19,7 @@ package org.apache.maven.project.harness; * under the License. */ -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -29,10 +28,13 @@ import java.util.Map; import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl; import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.project.builder.PomClassicDomainModel; -import org.apache.maven.project.builder.IPomClassicDomainModel; import org.apache.maven.project.MavenProject; import org.apache.maven.shared.model.ModelProperty; +import org.codehaus.plexus.util.WriterFactory; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; public class PomTestWrapper { @@ -65,7 +67,11 @@ public class PomTestWrapper } this.domainModel = domainModel; this.pomFile = pomFile; - context = JXPathContext.newContext( domainModel.getModel() ); + try { + context = JXPathContext.newContext( new MavenXpp3Reader().read(domainModel.getInputStream())); + } catch (XmlPullParserException e) { + throw new IOException(e.getMessage()); + } } public PomTestWrapper( File pomFile, MavenProject mavenProject ) @@ -100,19 +106,11 @@ public class PomTestWrapper } this.domainModel = new PomClassicDomainModel( file ); - context = JXPathContext.newContext( domainModel.getModel() ); - } - - public PomTestWrapper( Model model ) - throws IOException - { - if ( model == null ) - { - throw new IllegalArgumentException( "model: null" ); + try { + context = JXPathContext.newContext( new MavenXpp3Reader().read(domainModel.getInputStream())); + } catch (XmlPullParserException e) { + throw new IOException(e.getMessage()); } - - this.domainModel = new PomClassicDomainModel( model ); - context = JXPathContext.newContext( domainModel.getModel() ); } public MavenProject getMavenProject() @@ -125,7 +123,7 @@ public class PomTestWrapper if(domainModel == null && mavenProject != null) { try { - return new PomClassicDomainModel(mavenProject.getModel()); + return convertToDomainModel(mavenProject.getModel()); } catch (IOException e) { } @@ -134,6 +132,30 @@ public class PomTestWrapper return this.domainModel; } + private PomClassicDomainModel convertToDomainModel(Model model) throws IOException + { + if ( model == null ) + { + throw new IllegalArgumentException( "model: null" ); + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Writer out = null; + MavenXpp3Writer writer = new MavenXpp3Writer(); + try + { + out = WriterFactory.newXmlWriter( baos ); + writer.write( out, model ); + } + finally + { + if ( out != null ) + { + out.close(); + } + } + return new PomClassicDomainModel(new ByteArrayInputStream(baos.toByteArray())); + } + public File getBasedir() { return ( pomFile != null ) ? pomFile.getParentFile() : null; diff --git a/maven-project/src/test/resources-project-builder/parent-inheritance/child3.xml b/maven-project/src/test/resources-project-builder/parent-inheritance/child3.xml new file mode 100644 index 0000000000..54f9b29daf --- /dev/null +++ b/maven-project/src/test/resources-project-builder/parent-inheritance/child3.xml @@ -0,0 +1,6 @@ + + gid + child-3 + 1.0 + 4.0.0 + \ No newline at end of file diff --git a/maven-project/src/test/resources-project-builder/parent-inheritance/pom.xml b/maven-project/src/test/resources-project-builder/parent-inheritance/pom.xml new file mode 100644 index 0000000000..3dc28738a3 --- /dev/null +++ b/maven-project/src/test/resources-project-builder/parent-inheritance/pom.xml @@ -0,0 +1,10 @@ + + + gid + child-3 + 1.0 + child3.xml + + 4.0.0 + child-2 + \ No newline at end of file diff --git a/maven-project/src/test/resources-project-builder/parent-inheritance/sub/pom.xml b/maven-project/src/test/resources-project-builder/parent-inheritance/sub/pom.xml new file mode 100644 index 0000000000..2d4671ed9f --- /dev/null +++ b/maven-project/src/test/resources-project-builder/parent-inheritance/sub/pom.xml @@ -0,0 +1,9 @@ + + + gid + child-2 + 1.0 + + 4.0.0 + child-1 + \ No newline at end of file diff --git a/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml b/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml index ffbaedb372..809bc9a758 100644 --- a/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml +++ b/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml @@ -57,9 +57,9 @@ under the License. - org.apache.maven.project.builder.ProjectBuilder + org.apache.maven.project.ProjectBuilder default - org.apache.maven.project.builder.impl.DefaultProjectBuilder + org.apache.maven.project.DefaultProjectBuilder org.apache.maven.artifact.factory.ArtifactFactory @@ -75,7 +75,7 @@ under the License. org.apache.maven.project.TestProjectBuilder - org.apache.maven.project.builder.ProjectBuilder + org.apache.maven.project.ProjectBuilder org.apache.maven.profiles.build.ProfileAdvisor diff --git a/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml b/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml index 94e55d84db..fbcf30aeef 100644 --- a/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml +++ b/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml @@ -60,9 +60,9 @@ under the License. - org.apache.maven.project.builder.ProjectBuilder + org.apache.maven.project.ProjectBuilder default - org.apache.maven.project.builder.impl.DefaultProjectBuilder + org.apache.maven.project.DefaultProjectBuilder org.apache.maven.artifact.factory.ArtifactFactory @@ -78,7 +78,7 @@ under the License. org.apache.maven.project.TestProjectBuilder - org.apache.maven.project.builder.ProjectBuilder + org.apache.maven.project.ProjectBuilder org.apache.maven.profiles.build.ProfileAdvisor