1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.codehaus.mojo.license;
27
28 import org.codehaus.mojo.license.model.License;
29
30 import java.io.File;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class UpdateProjectLicenseMojo
47 extends AbstractLicenseNameMojo
48 {
49
50
51
52
53
54
55
56
57
58 protected File licenseFile;
59
60
61
62
63
64
65
66
67
68 protected File outputDirectory;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 protected boolean generateBundle;
85
86
87
88
89
90
91
92
93
94
95 protected String bundleLicensePath;
96
97
98
99
100
101
102
103 protected boolean force;
104
105
106
107
108
109
110
111 protected boolean skipUpdateProjectLicense;
112
113
114
115
116 private boolean doGenerate;
117
118 @Override
119 protected void init()
120 throws Exception
121 {
122
123 if ( isSkip() )
124 {
125 return;
126 }
127
128 super.init();
129
130
131 File licenseFile = getLicenseFile();
132 if ( licenseFile != null )
133 {
134 File pomFile = getProject().getFile();
135
136 setDoGenerate( isForce() || !licenseFile.exists() || licenseFile.lastModified() <= pomFile.lastModified() );
137 }
138 }
139
140 @Override
141 protected void doAction()
142 throws Exception
143 {
144
145 License license = getLicense();
146
147 File target = getLicenseFile();
148
149 if ( isDoGenerate() )
150 {
151
152 getLog().info( "Will create or update license file [" + license.getName() + "] to " + target );
153 if ( isVerbose() )
154 {
155 getLog().info( "detail of license :\n" + license );
156 }
157
158 if ( target.exists() && isKeepBackup() )
159 {
160 if ( isVerbose() )
161 {
162 getLog().info( "backup " + target );
163 }
164
165 FileUtil.backupFile( target );
166 }
167 }
168
169
170 String licenseContent = license.getLicenseContent( getEncoding() );
171
172 if ( isDoGenerate() )
173 {
174
175
176 FileUtil.writeString( target, licenseContent, getEncoding() );
177 }
178
179 if ( hasClassPath() )
180 {
181
182
183 File resourceTarget = new File( getOutputDirectory(), target.getName() );
184 FileUtil.copyFile( getLicenseFile(), resourceTarget );
185
186 addResourceDir( getOutputDirectory(), "**/" + resourceTarget.getName() );
187
188 if ( isGenerateBundle() )
189 {
190
191
192 File bundleTarget = FileUtil.getFile( getOutputDirectory(), getBundleLicensePath() );
193 FileUtil.copyFile( target, bundleTarget );
194
195 if ( !resourceTarget.getName().equals( bundleTarget.getName() ) )
196 {
197
198 addResourceDir( getOutputDirectory(), "**/" + bundleTarget.getName() );
199 }
200 }
201
202
203 }
204 }
205
206 public File getLicenseFile()
207 {
208 return licenseFile;
209 }
210
211 public boolean isGenerateBundle()
212 {
213 return generateBundle;
214 }
215
216 public File getOutputDirectory()
217 {
218 return outputDirectory;
219 }
220
221 public String getBundleLicensePath()
222 {
223 return bundleLicensePath;
224 }
225
226 public boolean isDoGenerate()
227 {
228 return doGenerate;
229 }
230
231 public boolean isForce()
232 {
233 return force;
234 }
235
236 @Override
237 public boolean isSkip()
238 {
239 return skipUpdateProjectLicense;
240 }
241
242 public void setLicenseFile( File licenseFile )
243 {
244 this.licenseFile = licenseFile;
245 }
246
247 public void setGenerateBundle( boolean generateBundle )
248 {
249 this.generateBundle = generateBundle;
250 }
251
252 public void setOutputDirectory( File outputDirectory )
253 {
254 this.outputDirectory = outputDirectory;
255 }
256
257 public void setBundleLicensePath( String bundleLicensePath )
258 {
259 this.bundleLicensePath = bundleLicensePath;
260 }
261
262 public void setDoGenerate( boolean doGenerate )
263 {
264 this.doGenerate = doGenerate;
265 }
266
267 @Override
268 public void setSkip( boolean skipUpdateProjectLicense )
269 {
270 this.skipUpdateProjectLicense = skipUpdateProjectLicense;
271 }
272
273 public void setForce( boolean force )
274 {
275 this.force = force;
276 }
277 }