View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.path;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Objects;
29  import java.util.function.Function;
30  
31  import org.apache.maven.api.model.Build;
32  import org.apache.maven.api.model.Model;
33  import org.apache.maven.api.model.Reporting;
34  import org.apache.maven.api.model.Resource;
35  import org.apache.maven.model.building.ModelBuildingRequest;
36  
37  /**
38   * Resolves relative paths within a model against a specific base directory.
39   *
40   */
41  @Named
42  @Singleton
43  public class DefaultModelPathTranslator implements ModelPathTranslator {
44  
45      private final PathTranslator pathTranslator;
46  
47      @Inject
48      public DefaultModelPathTranslator(PathTranslator pathTranslator) {
49          this.pathTranslator = pathTranslator;
50      }
51  
52      @Override
53      public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request) {
54          if (modelV3 == null || basedir == null) {
55              return;
56          }
57  
58          Model model = modelV3.getDelegate();
59          Build build = model.getBuild();
60          Build newBuild = null;
61          if (build != null) {
62              newBuild = Build.newBuilder(build)
63                      .directory(alignToBaseDirectory(build.getDirectory(), basedir))
64                      .sourceDirectory(alignToBaseDirectory(build.getSourceDirectory(), basedir))
65                      .testSourceDirectory(alignToBaseDirectory(build.getTestSourceDirectory(), basedir))
66                      .scriptSourceDirectory(alignToBaseDirectory(build.getScriptSourceDirectory(), basedir))
67                      .resources(map(build.getResources(), r -> alignToBaseDirectory(r, basedir)))
68                      .testResources(map(build.getTestResources(), r -> alignToBaseDirectory(r, basedir)))
69                      .filters(map(build.getFilters(), s -> alignToBaseDirectory(s, basedir)))
70                      .outputDirectory(alignToBaseDirectory(build.getOutputDirectory(), basedir))
71                      .testOutputDirectory(alignToBaseDirectory(build.getTestOutputDirectory(), basedir))
72                      .build();
73          }
74  
75          Reporting reporting = model.getReporting();
76          Reporting newReporting = null;
77          if (reporting != null) {
78              newReporting = Reporting.newBuilder(reporting)
79                      .outputDirectory(alignToBaseDirectory(reporting.getOutputDirectory(), basedir))
80                      .build();
81          }
82          if (newBuild != build || newReporting != reporting) {
83              modelV3.update(Model.newBuilder(model)
84                      .build(newBuild)
85                      .reporting(newReporting)
86                      .build());
87          }
88      }
89  
90      private <T> List<T> map(List<T> resources, Function<T, T> mapper) {
91          List<T> newResources = null;
92          if (resources != null) {
93              for (int i = 0; i < resources.size(); i++) {
94                  T resource = resources.get(i);
95                  T newResource = mapper.apply(resource);
96                  if (newResource != null) {
97                      if (newResources == null) {
98                          newResources = new ArrayList<>(resources);
99                      }
100                     newResources.set(i, newResource);
101                 }
102             }
103         }
104         return newResources;
105     }
106 
107     private Resource alignToBaseDirectory(Resource resource, File basedir) {
108         if (resource != null) {
109             String newDir = alignToBaseDirectory(resource.getDirectory(), basedir);
110             if (newDir != null) {
111                 return resource.withDirectory(newDir);
112             }
113         }
114         return resource;
115     }
116 
117     private String alignToBaseDirectory(String path, File basedir) {
118         String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
119         return Objects.equals(path, newPath) ? null : newPath;
120     }
121 }