001    package 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    
022    import java.io.File;
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.apache.maven.model.Build;
027    import org.apache.maven.model.Model;
028    import org.apache.maven.model.Reporting;
029    import org.apache.maven.model.Resource;
030    import org.apache.maven.model.building.ModelBuildingRequest;
031    import org.codehaus.plexus.component.annotations.Component;
032    import 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 )
040    public 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        public void alignToBaseDirectory( Model model, File basedir, ModelBuildingRequest request )
054        {
055            if ( model == null || basedir == null )
056            {
057                return;
058            }
059    
060            Build build = model.getBuild();
061    
062            if ( build != null )
063            {
064                build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
065    
066                build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
067    
068                build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
069    
070                build.setScriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
071    
072                for ( Resource resource : build.getResources() )
073                {
074                    resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
075                }
076    
077                for ( Resource resource : build.getTestResources() )
078                {
079                    resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
080                }
081    
082                if ( build.getFilters() != null )
083                {
084                    List<String> filters = new ArrayList<String>( build.getFilters().size() );
085                    for ( String filter : build.getFilters() )
086                    {
087                        filters.add( alignToBaseDirectory( filter, basedir ) );
088                    }
089                    build.setFilters( filters );
090                }
091    
092                build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
093    
094                build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
095            }
096    
097            Reporting reporting = model.getReporting();
098    
099            if ( reporting != null )
100            {
101                reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
102            }
103        }
104    
105        private String alignToBaseDirectory( String path, File basedir )
106        {
107            return pathTranslator.alignToBaseDirectory( path, basedir );
108        }
109    
110    }