1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
34
35 public class DefaultBuildPomXMLFilterFactory extends BuildToRawPomXMLFilterFactory {
36 private final TransformerContext context;
37
38
39
40
41
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 }