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 if (!StringUtils.isBlank(value)) {
91 return value;
92 }
93 }
94 return defaultValue;
95 }
96
97 protected List<String> getKeys() {
98 List<String> keys = new ArrayList<String>();
99 keys.add(property + "." + groupId + "." + artifactId + "." + version);
100 keys.add(property + "." + groupId + "." + artifactId);
101 keys.add(property + "." + groupId);
102 keys.add(property);
103 return keys;
104 }
105
106 protected String getProperty(String key) {
107 String sys = System.getProperty(key);
108 String proj = project.getProperties().getProperty(key);
109 if (!StringUtils.isBlank(sys)) {
110 return sys;
111 } else {
112 return proj;
113 }
114
115 }
116
117 public MavenProject getProject() {
118 return project;
119 }
120
121 public String getGroupId() {
122 return groupId;
123 }
124
125 public void setGroupId(String groupId) {
126 this.groupId = groupId;
127 }
128
129 public String getArtifactId() {
130 return artifactId;
131 }
132
133 public void setArtifactId(String artifactId) {
134 this.artifactId = artifactId;
135 }
136
137 public String getVersion() {
138 return version;
139 }
140
141 public void setVersion(String version) {
142 this.version = version;
143 }
144
145 public String getProperty() {
146 return property;
147 }
148
149 public void setProperty(String property) {
150 this.property = property;
151 }
152
153 public String getDefaultValue() {
154 return defaultValue;
155 }
156
157 public void setDefaultValue(String defaultValue) {
158 this.defaultValue = defaultValue;
159 }
160
161 public void setProject(MavenProject project) {
162 this.project = project;
163 }
164
165 public String getSuffix() {
166 return suffix;
167 }
168
169 public void setSuffix(String suffix) {
170 this.suffix = suffix;
171 }
172
173 }