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