1 | |
package org.kuali.maven.plugins; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileInputStream; |
5 | |
import java.io.FileNotFoundException; |
6 | |
import java.io.IOException; |
7 | |
import java.io.InputStream; |
8 | |
import java.util.jar.Attributes; |
9 | |
import java.util.jar.Manifest; |
10 | |
|
11 | |
import org.apache.maven.plugin.AbstractMojo; |
12 | |
import org.apache.maven.plugin.MojoExecutionException; |
13 | |
import org.apache.maven.plugin.MojoFailureException; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | 0 | public class GetSVNRevisionNumberMojo extends AbstractMojo { |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
private String filename; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
private String attribute; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
private String prefix; |
46 | |
|
47 | |
@Override |
48 | |
public void execute() throws MojoExecutionException, MojoFailureException { |
49 | |
try { |
50 | 0 | Manifest manifest = getManifest(filename); |
51 | 0 | Attributes attributes = manifest.getMainAttributes(); |
52 | 0 | String revisionNumber = attributes.getValue(attribute); |
53 | 0 | validate(revisionNumber); |
54 | 0 | String key = prefix + "." + attribute; |
55 | 0 | System.setProperty(key, revisionNumber); |
56 | 0 | getLog().info(key + "=" + revisionNumber); |
57 | 0 | } catch (Exception e) { |
58 | 0 | throw new MojoExecutionException("Error handling " + filename, e); |
59 | 0 | } |
60 | 0 | } |
61 | |
|
62 | |
protected void validate(String revisionNumber) throws MojoExecutionException { |
63 | 0 | if (revisionNumber == null || revisionNumber.trim().equals("")) { |
64 | 0 | throw new MojoExecutionException("Could not locate a value for " + attribute + " in " + filename); |
65 | |
} |
66 | 0 | Integer number = Integer.parseInt(revisionNumber); |
67 | 0 | if (number < 0) { |
68 | 0 | throw new MojoExecutionException("Negative revision number"); |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
protected Manifest getManifest(String filename) throws IOException { |
73 | 0 | File file = new File(filename); |
74 | 0 | if (!file.exists()) { |
75 | 0 | throw new FileNotFoundException("File not found: " + filename); |
76 | |
} |
77 | 0 | InputStream in = null; |
78 | |
try { |
79 | 0 | in = new FileInputStream(file); |
80 | 0 | return new Manifest(in); |
81 | |
} finally { |
82 | 0 | close(in); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
protected void close(InputStream in) { |
87 | 0 | if (in == null) { |
88 | 0 | return; |
89 | |
} |
90 | |
try { |
91 | 0 | in.close(); |
92 | 0 | } catch (IOException e) { |
93 | |
; |
94 | 0 | } |
95 | 0 | } |
96 | |
|
97 | |
public String getFilename() { |
98 | 0 | return filename; |
99 | |
} |
100 | |
|
101 | |
public void setFilename(String filename) { |
102 | 0 | this.filename = filename; |
103 | 0 | } |
104 | |
|
105 | |
public String getAttribute() { |
106 | 0 | return attribute; |
107 | |
} |
108 | |
|
109 | |
public void setAttribute(String attribute) { |
110 | 0 | this.attribute = attribute; |
111 | 0 | } |
112 | |
|
113 | |
public String getPrefix() { |
114 | 0 | return prefix; |
115 | |
} |
116 | |
|
117 | |
public void setPrefix(String prefix) { |
118 | 0 | this.prefix = prefix; |
119 | 0 | } |
120 | |
|
121 | |
} |