1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.buildmetadata.data;
17
18 import java.util.Locale;
19 import java.util.Properties;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.maven.project.MavenProject;
24 import org.apache.maven.scm.manager.NoSuchScmProviderException;
25 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
26 import org.apache.maven.scm.repository.ScmRepository;
27 import org.apache.maven.scm.repository.ScmRepositoryException;
28 import org.codehaus.plexus.util.StringUtils;
29
30 import de.smartics.maven.plugin.buildmetadata.common.RevisionHelper;
31 import de.smartics.maven.plugin.buildmetadata.common.ScmControl;
32 import de.smartics.maven.plugin.buildmetadata.common.ScmCredentials;
33 import de.smartics.maven.plugin.buildmetadata.common.ScmInfo;
34 import de.smartics.maven.plugin.buildmetadata.scm.maven.ScmAccessInfo;
35 import de.smartics.maven.plugin.buildmetadata.scm.maven.ScmConnectionInfo;
36
37
38
39
40
41
42
43
44 public class ScmMetaDataProvider extends AbstractMetaDataProvider
45 {
46
47
48
49
50
51
52
53 private static final Log LOG = LogFactory.getLog(ScmMetaDataProvider.class);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public ScmMetaDataProvider(final MavenProject project, final ScmInfo scmInfo)
69 {
70 this.project = project;
71 this.scmInfo = scmInfo;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public final void provideBuildMetaData(final Properties buildMetaDataProperties)
91 {
92 final ScmControl scmControl = scmInfo.getScmControl();
93 if (scmControl.isAddScmInfo() && !scmControl.isOffline()
94 && project.getScm() != null)
95 {
96 try
97 {
98 final ScmConnectionInfo scmConnectionInfo = loadConnectionInfo();
99 final ScmAccessInfo scmAccessInfo = createScmAccessInfo();
100 final RevisionHelper helper =
101 new RevisionHelper(scmInfo.getScmManager(), scmConnectionInfo,
102 scmAccessInfo, scmInfo.getBuildDatePattern());
103 helper.provideScmBuildInfo(buildMetaDataProperties, scmControl);
104 }
105 catch (final ScmRepositoryException e)
106 {
107 throw new IllegalStateException(
108 "Cannot fetch SCM revision information.", e);
109 }
110 catch (final NoSuchScmProviderException e)
111 {
112 throw new IllegalStateException(
113 "Cannot fetch SCM revision information.", e);
114 }
115 }
116 else
117 {
118 LOG.debug("Skipping SCM data since addScmInfo="
119 + scmControl.isAddScmInfo() + ", offline="
120 + scmControl.isOffline() + ", scmInfoProvided="
121 + (project.getScm() != null) + ".");
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 private ScmConnectionInfo loadConnectionInfo() throws IllegalStateException,
138 ScmRepositoryException, NoSuchScmProviderException
139 {
140 final String scmConnection = getConnection();
141 final ScmCredentials credentials = scmInfo.getScmCrendentials();
142 if (credentials.getUserName() == null || credentials.getPassword() == null)
143 {
144 final ScmRepository repository =
145 scmInfo.getScmManager().makeScmRepository(scmConnection);
146 if (repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost)
147 {
148 final ScmProviderRepositoryWithHost repositoryWithHost =
149 (ScmProviderRepositoryWithHost) repository.getProviderRepository();
150 final String host = createHostName(repositoryWithHost);
151 credentials.configureByServer(host);
152 }
153 }
154
155 final ScmConnectionInfo info = new ScmConnectionInfo();
156 info.setUserName(credentials.getUserName());
157 info.setPassword(credentials.getPassword());
158 info.setPrivateKey(credentials.getPrivateKey());
159 info.setScmConnectionUrl(scmConnection);
160 info.setTagBase(scmInfo.getTagBase());
161 return info;
162 }
163
164
165
166
167
168
169
170
171
172
173 protected final String getConnection() throws IllegalStateException
174 {
175 if (project.getScm() == null)
176 {
177 throw new IllegalStateException("SCM Connection is not set.");
178 }
179
180 final String scmConnection = project.getScm().getConnection();
181 final String connectionType = scmInfo.getConnectionType();
182 if (StringUtils.isNotEmpty(scmConnection)
183 && "connection".equals(connectionType.toLowerCase(Locale.ENGLISH)))
184 {
185 return scmConnection;
186 }
187
188 final String scmDeveloper = project.getScm().getDeveloperConnection();
189 if (StringUtils.isNotEmpty(scmDeveloper)
190 && "developerconnection".equals(connectionType
191 .toLowerCase(Locale.ENGLISH)))
192 {
193 return scmDeveloper;
194 }
195
196 throw new IllegalStateException("SCM Connection is not set.");
197 }
198
199
200
201
202
203
204
205 private String createHostName(
206 final ScmProviderRepositoryWithHost repositoryWithHost)
207 {
208 final String host = repositoryWithHost.getHost();
209 final int port = repositoryWithHost.getPort();
210 if (port > 0)
211 {
212 return host + ":" + port;
213 }
214 return host;
215 }
216
217
218
219
220
221
222
223 private ScmAccessInfo createScmAccessInfo()
224 {
225 final ScmAccessInfo accessInfo = new ScmAccessInfo();
226 accessInfo.setDateFormat(scmInfo.getScmDateFormat());
227 accessInfo.setRootDirectory(scmInfo.getBasedir());
228 accessInfo.setFailOnLocalModifications(scmInfo.getScmControl()
229 .isFailOnLocalModifications());
230 accessInfo.setIgnoreDotFilesInBaseDir(scmInfo.getScmControl()
231 .isIgnoreDotFilesInBaseDir());
232 accessInfo.setQueryRangeInDays(scmInfo.getQueryRangeInDays());
233 return accessInfo;
234 }
235
236
237
238 }