查看 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 javax.inject.Inject;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  
30  import org.apache.maven.model.Build;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.model.Reporting;
33  import org.apache.maven.model.Resource;
34  import org.apache.maven.model.building.ModelBuildingRequest;
35  
36  /**
37   * Resolves relative paths within a model against a specific base directory.
38   *
39   * @author Benjamin Bentmann
40   */
41  @Named
42  @Singleton
43  public class DefaultModelPathTranslator
44      implements ModelPathTranslator
45  {
46  
47      @Inject
48      private PathTranslator pathTranslator;
49  
50      public DefaultModelPathTranslator setPathTranslator( PathTranslator pathTranslator )
51      {
52          this.pathTranslator = pathTranslator;
53          return this;
54      }
55  
56      @Override
57      public void alignToBaseDirectory( Model model, File basedir, ModelBuildingRequest request )
58      {
59          if ( model == null || basedir == null )
60          {
61              return;
62          }
63  
64          Build build = model.getBuild();
65  
66          if ( build != null )
67          {
68              build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
69  
70              build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
71  
72              build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
73  
74              build.setScriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
75  
76              for ( Resource resource : build.getResources() )
77              {
78                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
79              }
80  
81              for ( Resource resource : build.getTestResources() )
82              {
83                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
84              }
85  
86              if ( build.getFilters() != null )
87              {
88                  List<String> filters = new ArrayList<>( build.getFilters().size() );
89                  for ( String filter : build.getFilters() )
90                  {
91                      filters.add( alignToBaseDirectory( filter, basedir ) );
92                  }
93                  build.setFilters( filters );
94              }
95  
96              build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
97  
98              build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
99          }
100 
101         Reporting reporting = model.getReporting();
102 
103         if ( reporting != null )
104         {
105             reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
106         }
107     }
108 
109     private String alignToBaseDirectory( String path, File basedir )
110     {
111         return pathTranslator.alignToBaseDirectory( path, basedir );
112     }
113 
114 }