001package org.apache.maven.model.path;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.model.Build;
027import org.apache.maven.model.Model;
028import org.apache.maven.model.Reporting;
029import org.apache.maven.model.Resource;
030import org.apache.maven.model.building.ModelBuildingRequest;
031import org.codehaus.plexus.component.annotations.Component;
032import org.codehaus.plexus.component.annotations.Requirement;
033
034/**
035 * Resolves relative paths within a model against a specific base directory.
036 *
037 * @author Benjamin Bentmann
038 */
039@Component( role = ModelPathTranslator.class )
040public class DefaultModelPathTranslator
041    implements ModelPathTranslator
042{
043
044    @Requirement
045    private PathTranslator pathTranslator;
046
047    public DefaultModelPathTranslator setPathTranslator( PathTranslator pathTranslator )
048    {
049        this.pathTranslator = pathTranslator;
050        return this;
051    }
052
053    @Override
054    public void alignToBaseDirectory( Model model, File basedir, ModelBuildingRequest request )
055    {
056        if ( model == null || basedir == null )
057        {
058            return;
059        }
060
061        Build build = model.getBuild();
062
063        if ( build != null )
064        {
065            build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
066
067            build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
068
069            build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
070
071            build.setScriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
072
073            for ( Resource resource : build.getResources() )
074            {
075                resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
076            }
077
078            for ( Resource resource : build.getTestResources() )
079            {
080                resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
081            }
082
083            if ( build.getFilters() != null )
084            {
085                List<String> filters = new ArrayList<String>( build.getFilters().size() );
086                for ( String filter : build.getFilters() )
087                {
088                    filters.add( alignToBaseDirectory( filter, basedir ) );
089                }
090                build.setFilters( filters );
091            }
092
093            build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
094
095            build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
096        }
097
098        Reporting reporting = model.getReporting();
099
100        if ( reporting != null )
101        {
102            reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
103        }
104    }
105
106    private String alignToBaseDirectory( String path, File basedir )
107    {
108        return pathTranslator.alignToBaseDirectory( path, basedir );
109    }
110
111}