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.building;
20  
21  import java.nio.file.Path;
22  import java.util.Optional;
23  import java.util.function.BiFunction;
24  import java.util.function.Function;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.transform.BuildToRawPomXMLFilterFactory;
27  import org.apache.maven.model.transform.RelativeProject;
28  
29  /**
30   * A BuildPomXMLFilterFactory which is context aware
31   *
32   * @author Robert Scholte
33   * @since 4.0.0
34   */
35  public class DefaultBuildPomXMLFilterFactory extends BuildToRawPomXMLFilterFactory {
36      private final TransformerContext context;
37  
38      /**
39       *
40       * @param context a set of data to extract values from as required for the build pom
41       * @param consume {@code true} if this factory is being used for creating the consumer pom, otherwise {@code false}
42       */
43      public DefaultBuildPomXMLFilterFactory(TransformerContext context, boolean consume) {
44          super(consume);
45          this.context = context;
46      }
47  
48      @Override
49      protected Function<Path, Optional<RelativeProject>> getRelativePathMapper() {
50          return p -> Optional.ofNullable(context.getRawModel(p)).map(DefaultBuildPomXMLFilterFactory::toRelativeProject);
51      }
52  
53      @Override
54      protected BiFunction<String, String, String> getDependencyKeyToVersionMapper() {
55          return (g, a) -> Optional.ofNullable(context.getRawModel(g, a))
56                  .map(DefaultBuildPomXMLFilterFactory::toVersion)
57                  .orElse(null);
58      }
59  
60      @Override
61      protected Optional<String> getChangelist() {
62          return Optional.ofNullable(context.getUserProperty("changelist"));
63      }
64  
65      @Override
66      protected Optional<String> getRevision() {
67          return Optional.ofNullable(context.getUserProperty("revision"));
68      }
69  
70      @Override
71      protected Optional<String> getSha1() {
72          return Optional.ofNullable(context.getUserProperty("sha1"));
73      }
74  
75      private static RelativeProject toRelativeProject(final Model m) {
76          String groupId = m.getGroupId();
77          if (groupId == null && m.getParent() != null) {
78              groupId = m.getParent().getGroupId();
79          }
80  
81          String version = m.getVersion();
82          if (version == null && m.getParent() != null) {
83              version = m.getParent().getVersion();
84          }
85  
86          return new RelativeProject(groupId, m.getArtifactId(), version);
87      }
88  
89      private static String toVersion(final Model m) {
90          String version = m.getVersion();
91          if (version == null && m.getParent() != null) {
92              version = m.getParent().getVersion();
93          }
94  
95          return version;
96      }
97  }