/
AbstractChecksumMojo.java
272 lines (242 loc) · 8.83 KB
/
AbstractChecksumMojo.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* checksum-maven-plugin - http://checksum-maven-plugin.nicoulaj.net
* Copyright © 2010-2021 checksum-maven-plugin contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.nicoulaj.maven.plugins.checksum.mojo;
import net.nicoulaj.maven.plugins.checksum.Constants;
import net.nicoulaj.maven.plugins.checksum.artifacts.ArtifactAttacher;
import net.nicoulaj.maven.plugins.checksum.artifacts.ArtifactListener;
import net.nicoulaj.maven.plugins.checksum.execution.Execution;
import net.nicoulaj.maven.plugins.checksum.execution.ExecutionException;
import net.nicoulaj.maven.plugins.checksum.execution.FailOnErrorExecution;
import net.nicoulaj.maven.plugins.checksum.execution.NeverFailExecution;
import net.nicoulaj.maven.plugins.checksum.execution.target.*;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.io.FileUtils;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Base class for {@code checksum-maven-plugin} mojos.
*
* @author <a href="mailto:julien.nicoulaud@gmail.com">Julien Nicoulaud</a>
* @since 1.1
*/
abstract class AbstractChecksumMojo
extends AbstractMojo
{
/**
* Fail if no files no process.
*/
private boolean defaultFailIfNoFiles;
/**
* Fail if no files no process.
*/
private final boolean failIfNoAlgorithms;
/**
* Fail if no files no process.
*/
private final boolean failIfNoTargets;
/**
* The Maven project.
*
* @since 1.0
*/
@Parameter( property = "project", required = true, readonly = true )
protected MavenProject project;
/**
* The Maven Project Helper.
*
* Used to attach checksums as build artifacts.
*
* @since 1.3
*/
@Component
protected MavenProjectHelper projectHelper;
/**
* The list of checksum algorithms used.
*
* <p>
* Default value is MD5 and SHA-1.
*
* <p>
* Allowed values are Cksum, CRC32, BLAKE2B-160, BLAKE2B-256, BLAKE2B-384,
* BLAKE2B-512, GOST3411, GOST3411-2012-256, GOST3411-2012-512, KECCAK-224, KECCAK-256, KECCAK-288, KECCAK-384,
* KECCAK-512, MD2, MD4, MD5, RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320, SHA, SHA-1, SHA-224, SHA-256, SHA3-224,
* SHA3-256, SHA3-384, SHA3-512, SHA-384, SHA-512, SHA-512/224, SHA-512/256, SKEIN-1024-1024, SKEIN-1024-384,
* SKEIN-1024-512, SKEIN-256-128, SKEIN-256-160, SKEIN-256-224, SKEIN-256-256, SKEIN-512-128, SKEIN-512-160,
* SKEIN-512-224, SKEIN-512-256, SKEIN-512-384, SKEIN-512-512, SM3, TIGER and WHIRLPOOL.
*
* <p>
* Use the following syntax:
* <pre><algorithms>
* <algorithm>MD5</algorithm>
* <algorithm>SHA-1</algorithm>
* </algorithms></pre>
*
* @since 1.0
*/
@Parameter
protected List<String> algorithms = Arrays.asList( Constants.DEFAULT_EXECUTION_ALGORITHMS );
/**
* Indicates whether the build will fail if there are errors.
*
* @since 1.0
*/
@Parameter( defaultValue = "true" )
protected boolean failOnError;
/**
* Encoding to use for generated files.
*
* @since 1.0
*/
@Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
protected String encoding = Constants.DEFAULT_ENCODING;
/**
* Should the checksums be attached as build artifacts.
*
* @since 1.3
*/
@Parameter( property = "attachChecksums", defaultValue = "false")
protected boolean attachChecksums;
/**
* Indicates whether the build will print checksums in the build log.
*
* @since 1.0
*/
@Parameter( defaultValue = "false" )
protected boolean quiet;
/**
* Indicates whether the build will output relative path information as well.
*
* @since 1.3
*/
@Parameter( defaultValue = "false" )
protected boolean includeRelativePath;
/**
* Sub path to use as the root of the relative path when including relative path in xml/csv files
*
* @since 1.3
*/
@Parameter( defaultValue = "" )
protected String relativeSubPath = "";
/**
* Constructor.
*
* @param failIfNoFiles fail if no files to process
* @param failIfNoAlgorithms fail if no algorithms to process
* @param failIfNoTargets fail if no targets to process
*/
AbstractChecksumMojo(boolean failIfNoFiles, boolean failIfNoAlgorithms, boolean failIfNoTargets) {
this.defaultFailIfNoFiles = failIfNoFiles;
this.failIfNoAlgorithms = failIfNoAlgorithms;
this.failIfNoTargets = failIfNoTargets;
}
/**
* {@inheritDoc}
*/
@Override
public void execute()
throws MojoFailureException
{
// Prepare an execution.
Execution execution = ( failOnError ) ? new FailOnErrorExecution() : new NeverFailExecution( getLog() );
execution.setAlgorithms( algorithms );
execution.setFiles( getFilesToProcess() );
execution.setFailIfNoFiles(isFailIfNoFiles());
execution.setFailIfNoAlgorithms(failIfNoAlgorithms);
execution.setFailIfNoTargets(failIfNoTargets);
if ( !quiet )
{
execution.addTarget( new MavenLogTarget( getLog() ) );
}
if ( includeRelativePath )
{
execution.setSubPath( relativeSubPath );
} else {
execution.setSubPath( null );
}
if ( isIndividualFiles() )
{
File outputDirectory = null;
if ( StringUtils.isNotEmpty( getIndividualFilesOutputDirectory() ) )
{
outputDirectory = FileUtils.resolveFile( new File( project.getBuild().getDirectory() ),
getIndividualFilesOutputDirectory() );
}
execution.addTarget( new OneHashPerFileTarget( encoding, outputDirectory, createArtifactListeners(), isAppendFilename()) );
}
if ( isCsvSummary() )
{
execution.addTarget( new CsvSummaryFileTarget(
FileUtils.resolveFile( new File( project.getBuild().getDirectory() ), getCsvSummaryFile() ),
encoding, createArtifactListeners()) );
}
if ( isXmlSummary() )
{
execution.addTarget( new XmlSummaryFileTarget(
FileUtils.resolveFile( new File( project.getBuild().getDirectory() ), getXmlSummaryFile() ),
encoding, createArtifactListeners()) );
}
if ( isShasumSummary() )
{
execution.addTarget( new ShasumSummaryFileTarget(
FileUtils.resolveFile( new File( project.getBuild().getDirectory() ), getShasumSummaryFile() ), encoding, createArtifactListeners()) );
}
// Run the execution.
try
{
execution.run();
}
catch ( ExecutionException e )
{
getLog().error( e.getMessage() );
throw new MojoFailureException( e.getMessage() );
}
}
private Iterable<? extends ArtifactListener> createArtifactListeners() {
if (!attachChecksums) {
return Collections.emptyList();
}
return Collections.singletonList(new ArtifactAttacher(project, projectHelper));
}
/**
* Build the list of files from which digests should be generated.
*
* @return the list of files that should be processed.
*/
protected abstract List<ChecksumFile> getFilesToProcess();
protected abstract boolean isIndividualFiles();
protected boolean isAppendFilename(){
return false;
}
protected abstract String getIndividualFilesOutputDirectory();
protected abstract boolean isCsvSummary();
protected abstract String getCsvSummaryFile();
protected abstract boolean isXmlSummary();
protected abstract String getXmlSummaryFile();
protected abstract boolean isShasumSummary();
protected abstract String getShasumSummaryFile();
protected boolean isFailIfNoFiles(){
return defaultFailIfNoFiles;
}
}