From 9501ad88354927bdd7d1b88d1f4fb7a0a802a4c7 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Mon, 1 Jun 2009 15:55:41 +0000 Subject: [PATCH] o Resurrected the path translator component (on this occasion, splitting it to separate the single path translation from the model translation) git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@780698 13f79535-47bb-0310-9956-ffa450edef68 --- .../project/path/DefaultPathTranslator.java | 1 + .../maven/project/path/PathTranslator.java | 1 + .../interpolator/DefaultInterpolator.java | 81 +------------- .../path/DefaultModelPathTranslator.java | 103 ++++++++++++++++++ .../model/path/DefaultPathTranslator.java | 63 +++++++++++ .../maven/model/path/ModelPathTranslator.java | 43 ++++++++ .../maven/model/path/PathTranslator.java | 42 +++++++ 7 files changed, 258 insertions(+), 76 deletions(-) create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java diff --git a/maven-compat/src/main/java/org/apache/maven/project/path/DefaultPathTranslator.java b/maven-compat/src/main/java/org/apache/maven/project/path/DefaultPathTranslator.java index 3b9f82152d..48ea41f1aa 100644 --- a/maven-compat/src/main/java/org/apache/maven/project/path/DefaultPathTranslator.java +++ b/maven-compat/src/main/java/org/apache/maven/project/path/DefaultPathTranslator.java @@ -30,6 +30,7 @@ import org.apache.maven.model.Reporting; import org.apache.maven.model.Resource; import org.codehaus.plexus.component.annotations.Component; +@Deprecated @Component(role = PathTranslator.class) public class DefaultPathTranslator implements PathTranslator diff --git a/maven-compat/src/main/java/org/apache/maven/project/path/PathTranslator.java b/maven-compat/src/main/java/org/apache/maven/project/path/PathTranslator.java index 82580ef8a5..27c0b9dc5c 100644 --- a/maven-compat/src/main/java/org/apache/maven/project/path/PathTranslator.java +++ b/maven-compat/src/main/java/org/apache/maven/project/path/PathTranslator.java @@ -27,6 +27,7 @@ import org.apache.maven.model.Model; * @author Jason van Zyl * @version $Id$ */ +@Deprecated public interface PathTranslator { String ROLE = PathTranslator.class.getName(); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolator/DefaultInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolator/DefaultInterpolator.java index e1621e67aa..e043992ae0 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolator/DefaultInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolator/DefaultInterpolator.java @@ -21,12 +21,10 @@ import java.util.Properties; import java.util.Set; import java.util.Map.Entry; -import org.apache.maven.model.Build; import org.apache.maven.model.Model; -import org.apache.maven.model.Reporting; -import org.apache.maven.model.Resource; import org.apache.maven.model.io.ModelReader; import org.apache.maven.model.io.ModelWriter; +import org.apache.maven.model.path.ModelPathTranslator; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.util.IOUtil; @@ -46,6 +44,9 @@ public class DefaultInterpolator @Requirement private ModelWriter modelWriter; + @Requirement + private ModelPathTranslator modelPathTranslator; + public Model interpolateModel( Model model, Properties properties, File projectDirectory ) throws IOException { @@ -191,10 +192,7 @@ public class DefaultInterpolator { String xml = unmarshalModelPropertiesToXml( modelProperties, ProjectUri.baseUri ); Model m = modelReader.read( new StringReader( xml ), null ); - if ( projectDirectory != null ) - { - alignPaths( m, projectDirectory ); - } + modelPathTranslator.alignToBaseDirectory( m, projectDirectory ); return m; } catch ( IOException e ) @@ -203,75 +201,6 @@ public class DefaultInterpolator } } - /** - * Post-processes the paths of build directories by aligning relative paths to the project - * directory and normalizing file separators to the platform-specific separator. - * - * @param model The model to process, must not be {@code null}. - * @param basedir The project directory, must not be {@code null}. - */ - private static void alignPaths( Model model, File basedir ) - { - Build build = model.getBuild(); - if ( build != null ) - { - build.setDirectory( getAlignedPathFor( build.getDirectory(), basedir ) ); - build.setOutputDirectory( getAlignedPathFor( build.getOutputDirectory(), basedir ) ); - build.setTestOutputDirectory( getAlignedPathFor( build.getTestOutputDirectory(), basedir ) ); - build.setSourceDirectory( getAlignedPathFor( build.getSourceDirectory(), basedir ) ); - build.setTestSourceDirectory( getAlignedPathFor( build.getTestSourceDirectory(), basedir ) ); - build.setScriptSourceDirectory( getAlignedPathFor( build.getScriptSourceDirectory(), basedir ) ); - - for ( Resource r : build.getResources() ) - { - r.setDirectory( getAlignedPathFor( r.getDirectory(), basedir ) ); - } - - for ( Resource r : build.getTestResources() ) - { - r.setDirectory( getAlignedPathFor( r.getDirectory(), basedir ) ); - } - - List filters = new ArrayList(); - for ( String f : build.getFilters() ) - { - filters.add( getAlignedPathFor( f, basedir ) ); - } - build.setFilters( filters ); - } - - Reporting reporting = model.getReporting(); - if ( reporting != null ) - { - reporting.setOutputDirectory( getAlignedPathFor( reporting.getOutputDirectory(), basedir ) ); - } - - } - - private static String getAlignedPathFor( String path, File basedir ) - { - if ( path != null ) - { - File file = new File( path ); - if ( file.isAbsolute() ) - { - // path was already absolute, just normalize file separator and we're done - path = file.getPath(); - } - else if ( file.getPath().startsWith( File.separator ) ) - { - // drive-relative Windows path, don't align with project directory but with drive root - path = file.getAbsolutePath(); - } - else - { - // an ordinary relative path, align with project directory - path = new File( new File( basedir, path ).toURI().normalize() ).getAbsolutePath(); - } - } - return path; - } - private static void interpolateModelProperties( List modelProperties, List interpolatorProperties ) { if ( modelProperties == null ) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java new file mode 100644 index 0000000000..0dca677ef2 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java @@ -0,0 +1,103 @@ +package org.apache.maven.model.path; + +/* + * 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 java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.model.Build; +import org.apache.maven.model.Model; +import org.apache.maven.model.Reporting; +import org.apache.maven.model.Resource; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; + +/** + * Resolves relative paths within a model against a specific base directory. + * + * @author Benjamin Bentmann + */ +@Component( role = ModelPathTranslator.class ) +public class DefaultModelPathTranslator + implements ModelPathTranslator +{ + + @Requirement + private PathTranslator pathTranslator; + + public void alignToBaseDirectory( Model model, File basedir ) + { + if ( model == null || basedir == null ) + { + return; + } + + Build build = model.getBuild(); + + if ( build != null ) + { + build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) ); + + build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) ); + + build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) ); + + build.setScriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) ); + + for ( Resource resource : build.getResources() ) + { + resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) ); + } + + for ( Resource resource : build.getTestResources() ) + { + resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) ); + } + + if ( build.getFilters() != null ) + { + List filters = new ArrayList( build.getFilters().size() ); + for ( String filter : build.getFilters() ) + { + filters.add( alignToBaseDirectory( filter, basedir ) ); + } + build.setFilters( filters ); + } + + build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) ); + + build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) ); + } + + Reporting reporting = model.getReporting(); + + if ( reporting != null ) + { + reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) ); + } + } + + private String alignToBaseDirectory( String path, File basedir ) + { + return pathTranslator.alignToBaseDirectory( path, basedir ); + } + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java new file mode 100644 index 0000000000..423654a4c7 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java @@ -0,0 +1,63 @@ +package org.apache.maven.model.path; + +/* + * 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 java.io.File; + +import org.codehaus.plexus.component.annotations.Component; + +/** + * Resolves relative paths against a specific base directory. + * + * @author Benjamin Bentmann + */ +@Component( role = PathTranslator.class ) +public class DefaultPathTranslator + implements PathTranslator +{ + + public String alignToBaseDirectory( String path, File basedir ) + { + String result = path; + + if ( path != null && basedir != null ) + { + File file = new File( path ); + if ( file.isAbsolute() ) + { + // path was already absolute, just normalize file separator and we're done + result = file.getPath(); + } + else if ( file.getPath().startsWith( File.separator ) ) + { + // drive-relative Windows path, don't align with project directory but with drive root + result = file.getAbsolutePath(); + } + else + { + // an ordinary relative path, align with project directory + result = new File( new File( basedir, path ).toURI().normalize() ).getAbsolutePath(); + } + } + + return result; + } + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java new file mode 100644 index 0000000000..183c5a6227 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java @@ -0,0 +1,43 @@ +package org.apache.maven.model.path; + +/* + * 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 java.io.File; + +import org.apache.maven.model.Model; + +/** + * Resolves relative paths of a model against a specific base directory. + * + * @author Jason van Zyl + */ +public interface ModelPathTranslator +{ + + /** + * Resolves the well-known paths of the specified model against the given base directory. Paths within plugin + * configuration are not processed. + * + * @param model The model whose paths should be resolved, may be {@code null}. + * @param basedir The base directory to resolve relative paths against, may be {@code null}. + */ + void alignToBaseDirectory( Model model, File basedir ); + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java new file mode 100644 index 0000000000..3d136a32d4 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java @@ -0,0 +1,42 @@ +package org.apache.maven.model.path; + +/* + * 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 java.io.File; + +/** + * Resolves relative paths against a specific base directory. + * + * @author Jason van Zyl + */ +public interface PathTranslator +{ + + /** + * Resolves the specified path against the given base directory. The resolved path will be absolute and uses the + * platform-specified file separator. + * + * @param path The path to resolve, may be {@code null}. + * @param basedir The base directory to resolve relative paths against, may be {@code null}. + * @return The resolved path or {@code null} if the input path was {@code null}. + */ + String alignToBaseDirectory( String path, File basedir ); + +}