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  import java.util.Objects;
26  import java.util.function.Function;
27  
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  import javax.inject.Singleton;
31  
32  import org.apache.maven.api.model.Build;
33  import org.apache.maven.api.model.Model;
34  import org.apache.maven.api.model.Reporting;
35  import org.apache.maven.api.model.Resource;
36  import org.apache.maven.model.building.ModelBuildingRequest;
37  
38  /**
39   * Resolves relative paths within a model against a specific base directory.
40   *
41   * @author Benjamin Bentmann
42   */
43  @Named
44  @Singleton
45  public class DefaultModelPathTranslator
46      implements ModelPathTranslator
47  {
48  
49      private final PathTranslator pathTranslator;
50  
51      @Inject
52      public DefaultModelPathTranslator( PathTranslator pathTranslator )
53      {
54          this.pathTranslator = pathTranslator;
55      }
56  
57      @Override
58      public void alignToBaseDirectory( org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request )
59      {
60          if ( modelV3 == null || basedir == null )
61          {
62              return;
63          }
64  
65          Model model = modelV3.getDelegate();
66          Build build = model.getBuild();
67          Build newBuild = null;
68          if ( build != null )
69          {
70              newBuild = Build.newBuilder( build )
71                      .directory( alignToBaseDirectory( build.getDirectory(), basedir ) )
72                      .sourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) )
73                      .testSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) )
74                      .scriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) )
75                      .resources( map( build.getResources(), r -> alignToBaseDirectory( r, basedir ) ) )
76                      .testResources( map( build.getTestResources(), r -> alignToBaseDirectory( r, basedir ) ) )
77                      .filters( map( build.getFilters(), s -> alignToBaseDirectory( s, basedir ) ) )
78                      .outputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) )
79                      .testOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) )
80                      .build();
81          }
82  
83          Reporting reporting = model.getReporting();
84          Reporting newReporting = null;
85          if ( reporting != null )
86          {
87              newReporting = Reporting.newBuilder( reporting )
88                      .outputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) )
89                      .build();
90          }
91          if ( newBuild != build || newReporting != reporting )
92          {
93              modelV3.update( Model.newBuilder( model )
94                      .build( newBuild )
95                      .reporting( newReporting )
96                      .build() );
97          }
98      }
99  
100     private <T> List<T> map( List<T> resources, Function<T, T> mapper )
101     {
102         List<T> newResources = null;
103         if ( resources != null )
104         {
105             for ( int i = 0; i < resources.size(); i++ )
106             {
107                 T resource = resources.get( i );
108                 T newResource = mapper.apply( resource );
109                 if ( newResource != null )
110                 {
111                     if ( newResources == null )
112                     {
113                         newResources = new ArrayList<>( resources );
114                     }
115                     newResources.set( i, newResource );
116                 }
117             }
118         }
119         return newResources;
120     }
121 
122     private Resource alignToBaseDirectory( Resource resource, File basedir )
123     {
124         if ( resource != null )
125         {
126             String newDir = alignToBaseDirectory( resource.getDirectory(), basedir );
127             if ( newDir != null )
128             {
129                 return resource.withDirectory( newDir );
130             }
131         }
132         return resource;
133     }
134 
135     private String alignToBaseDirectory( String path, File basedir )
136     {
137         String newPath = pathTranslator.alignToBaseDirectory( path, basedir );
138         return Objects.equals( path, newPath ) ? null : newPath;
139     }
140 
141 }