mirror of https://github.com/apache/maven.git
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
This commit is contained in:
parent
bdcb479015
commit
9501ad8835
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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<String> filters = new ArrayList<String>();
|
||||
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<ModelProperty> modelProperties, List<InterpolatorProperty> interpolatorProperties )
|
||||
{
|
||||
if ( modelProperties == null )
|
||||
|
|
|
@ -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<String> filters = new ArrayList<String>( 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 );
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 );
|
||||
|
||||
}
|
|
@ -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 );
|
||||
|
||||
}
|
Loading…
Reference in New Issue