View Javadoc
1   package org.apache.maven.model.path;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.model.Build;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.Reporting;
29  import org.apache.maven.model.Resource;
30  import org.apache.maven.model.building.ModelBuildingRequest;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.component.annotations.Requirement;
33  
34  /**
35   * Resolves relative paths within a model against a specific base directory.
36   *
37   * @author Benjamin Bentmann
38   */
39  @Component( role = ModelPathTranslator.class )
40  public class DefaultModelPathTranslator
41      implements ModelPathTranslator
42  {
43  
44      @Requirement
45      private PathTranslator pathTranslator;
46  
47      public DefaultModelPathTranslator setPathTranslator( PathTranslator pathTranslator )
48      {
49          this.pathTranslator = pathTranslator;
50          return this;
51      }
52  
53      @Override
54      public void alignToBaseDirectory( Model model, File basedir, ModelBuildingRequest request )
55      {
56          if ( model == null || basedir == null )
57          {
58              return;
59          }
60  
61          Build build = model.getBuild();
62  
63          if ( build != null )
64          {
65              build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
66  
67              build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
68  
69              build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
70  
71              build.setScriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
72  
73              for ( Resource resource : build.getResources() )
74              {
75                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
76              }
77  
78              for ( Resource resource : build.getTestResources() )
79              {
80                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
81              }
82  
83              if ( build.getFilters() != null )
84              {
85                  List<String> filters = new ArrayList<>( build.getFilters().size() );
86                  for ( String filter : build.getFilters() )
87                  {
88                      filters.add( alignToBaseDirectory( filter, basedir ) );
89                  }
90                  build.setFilters( filters );
91              }
92  
93              build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
94  
95              build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
96          }
97  
98          Reporting reporting = model.getReporting();
99  
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 }