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