1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.codehaus.mojo.nbm; |
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.IOException; |
21 | |
import java.text.SimpleDateFormat; |
22 | |
import java.util.Calendar; |
23 | |
import java.util.Date; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import org.apache.maven.artifact.Artifact; |
27 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
28 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
29 | |
import org.apache.maven.model.Developer; |
30 | |
import org.apache.maven.model.License; |
31 | |
import org.apache.maven.model.Organization; |
32 | |
import org.apache.maven.plugin.MojoExecutionException; |
33 | |
import org.apache.maven.plugin.MojoFailureException; |
34 | |
import org.apache.maven.plugins.annotations.Component; |
35 | |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
36 | |
import org.apache.maven.plugins.annotations.Mojo; |
37 | |
import org.apache.maven.plugins.annotations.Parameter; |
38 | |
import org.apache.maven.plugins.annotations.ResolutionScope; |
39 | |
import org.apache.maven.project.MavenProjectHelper; |
40 | |
import org.apache.tools.ant.BuildException; |
41 | |
import org.apache.tools.ant.util.FileUtils; |
42 | |
import org.codehaus.plexus.PlexusConstants; |
43 | |
import org.codehaus.plexus.PlexusContainer; |
44 | |
import org.codehaus.plexus.context.Context; |
45 | |
import org.codehaus.plexus.context.ContextException; |
46 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
47 | |
import org.netbeans.nbbuild.MakeNBM; |
48 | |
import org.netbeans.nbbuild.MakeNBM.Blurb; |
49 | |
import org.netbeans.nbbuild.MakeNBM.Signature; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
@Mojo(name="nbm", |
58 | |
requiresProject=true, |
59 | |
threadSafe = true, |
60 | |
requiresDependencyResolution= ResolutionScope.RUNTIME, |
61 | |
defaultPhase= LifecyclePhase.PACKAGE ) |
62 | 0 | public class CreateNbmMojo |
63 | |
extends CreateNetBeansFileStructure |
64 | |
implements Contextualizable |
65 | |
{ |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
@Parameter(property="keystore") |
71 | |
private String keystore; |
72 | |
|
73 | |
|
74 | |
|
75 | |
@Parameter(property="keystorepass") |
76 | |
private String keystorepassword; |
77 | |
|
78 | |
|
79 | |
|
80 | |
@Parameter(property="keystorealias") |
81 | |
private String keystorealias; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
@Parameter(defaultValue="false", property="maven.nbm.skip") |
89 | |
private boolean skipNbm; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Parameter(defaultValue="false") |
96 | |
private boolean requiresRestart; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
@Parameter(defaultValue="${project.url}") |
105 | |
private String homePageUrl; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
@Parameter(defaultValue="${project.organization.name}") |
112 | |
private String author; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
@Parameter(property="maven.nbm.distributionURL") |
139 | |
private String distributionUrl; |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
@Parameter |
148 | |
private String licenseName; |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
@Parameter |
155 | |
private File licenseFile; |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
private PlexusContainer container; |
164 | |
|
165 | |
@Component |
166 | |
private ArtifactFactory artifactFactory; |
167 | |
|
168 | |
|
169 | |
|
170 | |
@Component |
171 | |
private MavenProjectHelper projectHelper; |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | 0 | private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy/MM/dd"); |
177 | |
|
178 | |
public void execute() |
179 | |
throws MojoExecutionException, MojoFailureException |
180 | |
{ |
181 | 0 | if ( skipNbm ) |
182 | |
{ |
183 | 0 | getLog().info( "Skipping generation of NBM file." ); |
184 | 0 | return; |
185 | |
} |
186 | |
|
187 | 0 | if ( "pom".equals( project.getPackaging() ) ) |
188 | |
{ |
189 | 0 | getLog().info( |
190 | |
"Skipping " + project.getId() + ", no nbm:nbm execution for 'pom' packaging" ); |
191 | 0 | return; |
192 | |
} |
193 | 0 | super.execute(); |
194 | |
|
195 | |
|
196 | |
|
197 | 0 | File nbmFile = new File( nbmBuildDir, finalName + ".nbm" ); |
198 | 0 | MakeNBM nbmTask = (MakeNBM) antProject.createTask( "makenbm" ); |
199 | 0 | nbmTask.setFile( nbmFile ); |
200 | 0 | nbmTask.setProductDir( clusterDir ); |
201 | |
|
202 | 0 | nbmTask.setModule( "modules" + File.separator + moduleJarName + ".jar" ); |
203 | 0 | boolean reqRestart = requiresRestart; |
204 | 0 | if (!reqRestart && module.isRequiresRestart()) { |
205 | 0 | reqRestart = module.isRequiresRestart(); |
206 | 0 | getLog().warn( "Module descriptor's requiresRestart field is deprecated, use plugin's configuration in pom.xml"); |
207 | |
} |
208 | 0 | nbmTask.setNeedsrestart( Boolean.toString( reqRestart ) ); |
209 | 0 | String moduleAuthor = author; |
210 | 0 | if (module.getAuthor() != null) { |
211 | 0 | moduleAuthor = module.getAuthor(); |
212 | 0 | getLog().warn( "Module descriptor's requiresRestart field is deprecated, use plugin's configuration in pom.xml"); |
213 | |
} |
214 | 0 | nbmTask.setModuleauthor( moduleAuthor ); |
215 | 0 | if ( keystore != null && keystorealias != null && keystorepassword != null ) |
216 | |
{ |
217 | 0 | File ks = new File( keystore ); |
218 | 0 | if ( !ks.exists() ) |
219 | |
{ |
220 | 0 | getLog().warn( "Cannot find keystore file at " + ks.getAbsolutePath() ); |
221 | |
} |
222 | |
else |
223 | |
{ |
224 | 0 | Signature sig = nbmTask.createSignature(); |
225 | 0 | sig.setKeystore( ks ); |
226 | 0 | sig.setAlias( keystorealias ); |
227 | 0 | sig.setStorepass( keystorepassword ); |
228 | 0 | getLog().debug( "Setup the Ant task to sign the NBM file." ); |
229 | |
} |
230 | 0 | } |
231 | 0 | else if ( keystore != null || keystorepassword != null || keystorealias != null ) |
232 | |
{ |
233 | 0 | getLog().warn( |
234 | |
"If you want to sign the nbm file, you need to define all three keystore related parameters." ); |
235 | |
} |
236 | 0 | String licName = licenseName; |
237 | 0 | File licFile = licenseFile; |
238 | 0 | if (module.getLicenseName() != null) { |
239 | 0 | licName = module.getLicenseName(); |
240 | 0 | getLog().warn( "Module descriptor's licenseName field is deprecated, use plugin's configuration in pom.xml"); |
241 | |
} |
242 | 0 | if (module.getLicenseFile() != null) { |
243 | 0 | File lf = new File( project.getBasedir(), module.getLicenseFile() ); |
244 | 0 | licFile = lf; |
245 | 0 | getLog().warn( "Module descriptor's licenseFile field is deprecated, use plugin's configuration in pom.xml"); |
246 | |
|
247 | |
} |
248 | 0 | if ( licName != null && licFile != null ) |
249 | |
{ |
250 | 0 | if ( !licFile.exists() || !licFile.isFile() ) |
251 | |
{ |
252 | 0 | getLog().warn( "Cannot find license file at " + licFile.getAbsolutePath() ); |
253 | |
} |
254 | |
else |
255 | |
{ |
256 | 0 | Blurb lb = nbmTask.createLicense(); |
257 | 0 | lb.setFile( licFile ); |
258 | 0 | lb.addText( licName ); |
259 | 0 | } |
260 | |
} |
261 | 0 | else if ( licName != null || licFile != null ) |
262 | |
{ |
263 | 0 | getLog().warn( |
264 | |
"To set license for the nbm, you need to specify both licenseName and licenseFile parameters." ); |
265 | |
} |
266 | |
else |
267 | |
{ |
268 | 0 | Blurb lb = nbmTask.createLicense(); |
269 | 0 | lb.addText( createDefaultLicenseHeader() ); |
270 | 0 | lb.addText( createDefaultLicenseText() ); |
271 | |
} |
272 | 0 | String hpUrl = homePageUrl; |
273 | 0 | if (module.getHomepageUrl() != null) { |
274 | 0 | getLog().warn( "Module descriptor's homePageUrl field is deprecated, use plugin's configuration in pom.xml"); |
275 | 0 | hpUrl = module.getHomepageUrl(); |
276 | |
} |
277 | 0 | if ( hpUrl != null ) |
278 | |
{ |
279 | 0 | nbmTask.setHomepage( hpUrl ); |
280 | |
} |
281 | 0 | String distribUrl = distributionUrl; |
282 | 0 | if (module.getDistributionUrl() != null) { |
283 | 0 | distribUrl = module.getDistributionUrl(); |
284 | 0 | getLog().warn( "Module descriptor's distributionUrl field is deprecated, use plugin's configuration in pom.xml"); |
285 | |
} |
286 | 0 | if ( distribUrl != null ) |
287 | |
{ |
288 | 0 | ArtifactRepository distRepository = CreateUpdateSiteMojo.getDeploymentRepository( |
289 | |
distribUrl, container, getLog() ); |
290 | 0 | String dist = null; |
291 | 0 | if ( distRepository == null ) |
292 | |
{ |
293 | 0 | if ( !distribUrl.contains( "::" ) ) |
294 | |
{ |
295 | 0 | dist = |
296 | |
distribUrl + ( distribUrl.endsWith( "/" ) ? "" : "/" ) |
297 | |
+ nbmFile.getName(); |
298 | |
} |
299 | |
} |
300 | |
else |
301 | |
{ |
302 | 0 | Artifact art = artifactFactory.createArtifact( |
303 | |
project.getGroupId(), project.getArtifactId(), |
304 | |
project.getVersion(), null, "nbm-file" ); |
305 | |
|
306 | 0 | dist = |
307 | |
distRepository.getUrl() + ( distRepository.getUrl().endsWith( "/" ) ? "" : "/" ) |
308 | |
+ distRepository.pathOf( art ); |
309 | |
|
310 | |
} |
311 | 0 | nbmTask.setDistribution( dist ); |
312 | 0 | } |
313 | |
else |
314 | |
{ |
315 | 0 | nbmTask.setDistribution( nbmFile.getName() ); |
316 | |
} |
317 | 0 | if ( ! "extra".equals( cluster ) ) |
318 | |
{ |
319 | 0 | nbmTask.setTargetcluster( cluster ); |
320 | |
} |
321 | |
|
322 | 0 | nbmTask.setReleasedate( DATE_FORMAT.format(new Date(System.currentTimeMillis())) ); |
323 | |
try |
324 | |
{ |
325 | 0 | nbmTask.execute(); |
326 | |
} |
327 | 0 | catch ( BuildException e ) |
328 | |
{ |
329 | 0 | throw new MojoExecutionException( "Cannot Generate nbm file:" + e.getMessage(), e ); |
330 | 0 | } |
331 | |
try |
332 | |
{ |
333 | 0 | File nbmfile = new File( buildDir, nbmFile.getName() ); |
334 | 0 | FileUtils.getFileUtils().copyFile( nbmFile, nbmfile ); |
335 | 0 | projectHelper.attachArtifact( project, "nbm-file", null, nbmfile ); |
336 | |
} |
337 | 0 | catch ( IOException ex ) |
338 | |
{ |
339 | 0 | throw new MojoExecutionException( "Cannot copy nbm to build directory", ex ); |
340 | 0 | } |
341 | 0 | } |
342 | |
|
343 | |
public void contextualize( Context context ) |
344 | |
throws ContextException |
345 | |
{ |
346 | 0 | this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
347 | 0 | } |
348 | |
|
349 | |
private String createDefaultLicenseHeader() |
350 | |
{ |
351 | 0 | String organization = ""; |
352 | 0 | Organization org = project.getOrganization(); |
353 | 0 | if (org != null) { |
354 | 0 | organization = org.getName(); |
355 | |
} |
356 | 0 | if (organization == null) { |
357 | 0 | List devs = project.getDevelopers(); |
358 | 0 | if (devs.size() > 0) { |
359 | 0 | Iterator dvs = devs.iterator(); |
360 | 0 | String devsString = ""; |
361 | 0 | while (dvs.hasNext()) { |
362 | 0 | Developer d = ( Developer )dvs.next(); |
363 | 0 | devsString = devsString + "," + d.getName() != null ? d.getName() : d.getId(); |
364 | 0 | } |
365 | 0 | organization = devsString.substring( 1 ); |
366 | |
} |
367 | |
} |
368 | 0 | if (organization == null) { |
369 | 0 | organization = ""; |
370 | |
} |
371 | 0 | String date = ""; |
372 | 0 | if (project.getInceptionYear() != null) { |
373 | 0 | date = project.getInceptionYear(); |
374 | |
} |
375 | 0 | String year = Integer.toString( Calendar.getInstance().get( Calendar.YEAR )); |
376 | 0 | if (!year.equals( date ) ) { |
377 | 0 | date = date.length() == 0 ? year : date + "-" + year; |
378 | |
} |
379 | 0 | return "Copyright " + organization + " " + date; |
380 | |
} |
381 | |
|
382 | |
private String createDefaultLicenseText() { |
383 | 0 | String toRet = "License terms:\n"; |
384 | |
|
385 | 0 | List licenses = project.getLicenses(); |
386 | 0 | if (licenses != null && licenses.size() > 0) { |
387 | 0 | Iterator lic = licenses.iterator(); |
388 | 0 | while (lic.hasNext()) { |
389 | 0 | License ll = ( License )lic.next(); |
390 | |
|
391 | 0 | if (ll.getName() != null) { |
392 | 0 | toRet = toRet + ll.getName() + " - "; |
393 | |
} |
394 | 0 | if (ll.getUrl() != null) { |
395 | 0 | toRet = toRet + ll.getUrl(); |
396 | |
} |
397 | 0 | if (lic.hasNext()) { |
398 | 0 | toRet = toRet + ",\n"; |
399 | |
} |
400 | 0 | } |
401 | 0 | } else { |
402 | 0 | toRet = toRet + "Unknown"; |
403 | |
} |
404 | 0 | return toRet; |
405 | |
} |
406 | |
} |