1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.properties;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.project.MavenProject;
25
26
27
28
29
30
31
32
33
34
35 public class GetGAVProperty extends AbstractMojo {
36
37
38
39
40
41
42 private MavenProject project;
43
44
45
46
47 private String groupId;
48
49
50
51
52 private String artifactId;
53
54
55
56
57 private String version;
58
59
60
61
62 private String property;
63
64
65
66
67 private String defaultValue;
68
69
70
71
72 private String suffix;
73
74 @Override
75 public void execute() throws MojoExecutionException {
76 List<String> keys = getKeys();
77 String value = getValue(project, keys);
78 if (StringUtils.isBlank(value)) {
79 getLog().info("No value for '" + property + "'");
80 } else {
81 String key = property + "." + suffix;
82 getLog().info("Setting " + key + "=" + value);
83 project.getProperties().setProperty(key, value);
84 }
85 }
86
87 protected String getValue(MavenProject project, List<String> keys) {
88 for (String key : keys) {
89 String value = getProperty(key);
90 getLog().debug(key + "=" + value);
91 if (!StringUtils.isBlank(value)) {
92 return value;
93 }
94 }
95 return defaultValue;
96 }
97
98 protected List<String> getKeys() {
99 List<String> keys = new ArrayList<String>();
100 keys.add(property + "." + groupId + "." + artifactId + "." + version);
101 keys.add(property + "." + groupId + "." + artifactId);
102 keys.add(property + "." + groupId);
103 keys.add(property);
104 return keys;
105 }
106
107 protected String getProperty(String key) {
108 String sys = System.getProperty(key);
109 String proj = project.getProperties().getProperty(key);
110 if (!StringUtils.isBlank(sys)) {
111 return sys;
112 } else {
113 return proj;
114 }
115
116 }
117
118 public MavenProject getProject() {
119 return project;
120 }
121
122 public String getGroupId() {
123 return groupId;
124 }
125
126 public void setGroupId(String groupId) {
127 this.groupId = groupId;
128 }
129
130 public String getArtifactId() {
131 return artifactId;
132 }
133
134 public void setArtifactId(String artifactId) {
135 this.artifactId = artifactId;
136 }
137
138 public String getVersion() {
139 return version;
140 }
141
142 public void setVersion(String version) {
143 this.version = version;
144 }
145
146 public String getProperty() {
147 return property;
148 }
149
150 public void setProperty(String property) {
151 this.property = property;
152 }
153
154 public String getDefaultValue() {
155 return defaultValue;
156 }
157
158 public void setDefaultValue(String defaultValue) {
159 this.defaultValue = defaultValue;
160 }
161
162 public void setProject(MavenProject project) {
163 this.project = project;
164 }
165
166 public String getSuffix() {
167 return suffix;
168 }
169
170 public void setSuffix(String suffix) {
171 this.suffix = suffix;
172 }
173
174 }