diff --git a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicTransformer.java b/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicTransformer.java index d266cf402f..007e72d5af 100644 --- a/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicTransformer.java +++ b/maven-project/src/main/java/org/apache/maven/project/builder/PomClassicTransformer.java @@ -243,10 +243,10 @@ public final class PomClassicTransformer } /** - * @see ModelTransformer#transformToModelProperties(java.util.List, java.util.List) + * @see ModelTransformer#transformToModelProperties(java.util.List */ - public List transformToModelProperties( List domainModels, - List interpolatorProperties) + public List transformToModelProperties(List domainModels + ) throws IOException { if ( domainModels == null || domainModels.isEmpty() ) @@ -484,11 +484,17 @@ public final class PomClassicTransformer modelProperties.removeAll( clearedProperties ); } - interpolateModelProperties( modelProperties, interpolatorProperties, ((PomClassicDomainModel) domainModels.get(0)) ); - return modelProperties; } + public void interpolateModelProperties(List modelProperties, + List interpolatorProperties, + DomainModel domainModel) + throws IOException + { + interpolateModelProperties( modelProperties, interpolatorProperties, (PomClassicDomainModel) domainModel); + } + public static String interpolateXmlString( String xml, List interpolatorProperties ) throws IOException { @@ -541,32 +547,36 @@ public final class PomClassicTransformer PomClassicDomainModel domainModel) throws IOException { - //RULE: No Self-Referential Properties - /* - for(ModelProperty mp : modelProperties) - { - if(mp.getUri().startsWith( ProjectUri.properties ) && mp.getValue() != null) - { - String name = mp.getUri().substring( mp.getUri().lastIndexOf( "/" ) + 1, mp.getUri().length() ); - if( mp.getValue().equals( "${" + name + "}") ) - { - throw new IOException( "Self-Referential reference in pom: Name = " + name ); - } - } - } - */ + Map aliases = new HashMap(); aliases.put( "project.", "pom."); - List ips = new ArrayList(interpolatorProperties); - ips.addAll(ModelTransformerContext.createInterpolatorProperties(modelProperties, ProjectUri.baseUri, aliases, - false, false)); + List firstPassModelProperties = new ArrayList(); + List secondPassModelProperties = new ArrayList(); + ModelProperty buildProperty = new ModelProperty(ProjectUri.Build.xUri, null); + for(ModelProperty mp : modelProperties) + { + if( mp.getValue() != null && !mp.getUri().contains( "#property" ) && !mp.getUri().contains( "#collection" )) + { + if( !buildProperty.isParentOf( mp ) || mp.getUri().equals(ProjectUri.Build.finalName ) ) + { + firstPassModelProperties.add(mp); + } + else + { + secondPassModelProperties.add(mp); + } + } + } + + String basedir = domainModel.getProjectDirectory().getAbsolutePath(); + List standardInterpolatorProperties = new ArrayList(); if(domainModel.isPomInBuild()) { - ips.add(new InterpolatorProperty("${project.basedir}", domainModel.getProjectDirectory().getAbsolutePath())); - ips.add(new InterpolatorProperty("${basedir}", domainModel.getProjectDirectory().getAbsolutePath())); - ips.add(new InterpolatorProperty("${pom.basedir}", domainModel.getProjectDirectory().getAbsolutePath())); + standardInterpolatorProperties.add(new InterpolatorProperty("${project.basedir}", basedir )); + standardInterpolatorProperties.add(new InterpolatorProperty("${basedir}", basedir )); + standardInterpolatorProperties.add(new InterpolatorProperty("${pom.basedir}", basedir )); } @@ -575,12 +585,49 @@ public final class PomClassicTransformer if(mp.getUri().startsWith(ProjectUri.properties) && mp.getValue() != null ) { String uri = mp.getUri(); - ips.add( new InterpolatorProperty( "${" + uri.substring( uri.lastIndexOf( "/" ) + 1, + standardInterpolatorProperties.add( new InterpolatorProperty( "${" + uri.substring( uri.lastIndexOf( "/" ) + 1, uri.length() ) + "}", mp.getValue() ) ); } } - ModelTransformerContext.interpolateModelProperties( modelProperties, ips ); + //FIRST PASS - Withhold using build directories as interpolator properties + List ips1 = new ArrayList(interpolatorProperties); + ips1.addAll(ModelTransformerContext.createInterpolatorProperties(firstPassModelProperties, ProjectUri.baseUri, aliases, + false, false)); + ips1.addAll(standardInterpolatorProperties); + + ModelTransformerContext.interpolateModelProperties( modelProperties, ips1 ); + + //SECOND PASS - Set absolute paths on build directories + if( domainModel.isPomInBuild() ) + { + Map buildDirectories = new HashMap(); + for(ModelProperty mp : secondPassModelProperties) + { + if(mp.getUri().equals( ProjectUri.Build.directory )) + { + File file = new File(mp.getResolvedValue()); + if( !file.isAbsolute() ) + { + buildDirectories.put(mp, new ModelProperty(mp.getUri(), new File(basedir, file.getPath()).getAbsolutePath())); + } + } + } + + for ( Map.Entry e : buildDirectories.entrySet() ) + { + secondPassModelProperties.remove( e.getKey() ); + secondPassModelProperties.add(e.getValue() ); + } + } + + //THIRD PASS - Use build directories as interpolator properties + List ips2 = new ArrayList(interpolatorProperties); + ips2.addAll(ModelTransformerContext.createInterpolatorProperties(secondPassModelProperties, ProjectUri.baseUri, aliases, + false, false)); + ips2.addAll(standardInterpolatorProperties); + + ModelTransformerContext.interpolateModelProperties( modelProperties, ips2 ); } private static boolean hasExecutionId( ModelContainer executionContainer ) diff --git a/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformer.java b/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformer.java index a2e4ea8d3f..acd05ca3a9 100644 --- a/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformer.java +++ b/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformer.java @@ -49,8 +49,18 @@ public interface ModelTransformer * @param domainModels list of domain models to transform to a list of model properties. List may not be null. * @return list of model properties */ - List transformToModelProperties( List domainModels, - List interpolatorProperties) + List transformToModelProperties(List domainModels ) throws IOException; + /** + * + * @param modelProperties + * @param interpolatorProperties + * @param domainModel + * @throws IOException + */ + void interpolateModelProperties(List modelProperties, + List interpolatorProperties, + DomainModel domainModel) + throws IOException; } diff --git a/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformerContext.java b/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformerContext.java index 0b49b783d5..6dd38c560d 100644 --- a/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformerContext.java +++ b/maven-shared-model/src/main/java/org/apache/maven/shared/model/ModelTransformerContext.java @@ -201,7 +201,7 @@ public final class ModelTransformerContext { List transformedProperties = - importModelProperties(importModels, fromModelTransformer.transformToModelProperties( domainModels, interpolatorProperties )); + importModelProperties(importModels, fromModelTransformer.transformToModelProperties( domainModels)); String baseUriForModel = fromModelTransformer.getBaseUri(); List modelProperties = @@ -265,9 +265,10 @@ public final class ModelTransformerContext List mps = modelDataSource.getModelProperties(); - //interpolateModelProperties( mps, baseUriForModel ); mps = sort( mps, baseUriForModel ); + fromModelTransformer.interpolateModelProperties( mps, interpolatorProperties, domainModels.get(0)); + try { DomainModel domainModel = toModelTransformer.transformToDomainModel( mps );