-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompressMojo.java
More file actions
90 lines (78 loc) · 2.68 KB
/
CompressMojo.java
File metadata and controls
90 lines (78 loc) · 2.68 KB
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
/*
* NodeJS Maven Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZOutputStream;
@Mojo(name = "compress")
public class CompressMojo extends AbstractMojo {
@Parameter(required = true)
private String baseDirectory;
@Parameter(required = true)
private List<String> filenames;
@Parameter(required = true)
private String targetDirectory;
@Parameter(defaultValue = "9")
private int compressionLevel;
@Override
public void execute() {
try {
this.compress(
this.filenames.stream()
.map(filename -> Path.of(this.baseDirectory, filename).toAbsolutePath())
.toList()
);
} catch (IOException e) {
throw new IllegalStateException(
"Error while compressing " + Arrays.toString(filenames.toArray()),
e
);
}
}
protected void compress(List<Path> filenames) throws IOException {
for (var file : filenames) {
var outputFile = Path.of(
this.targetDirectory,
Path.of(this.baseDirectory).toAbsolutePath().relativize(file) + ".xz"
);
this.getLog().info("Compressing " + file + " to " + outputFile);
if (!Files.exists(file)) {
throw new FileNotFoundException(String.format("File %s does not exist.", file));
}
if (Files.exists(outputFile)) {
this.getLog()
.info(String.format("Skipping compression, file %s already exists.", outputFile));
continue;
}
outputFile.toFile().getParentFile().mkdirs();
try (
var is = Files.newInputStream(file);
var outfile = Files.newOutputStream(outputFile);
var outxz = new XZOutputStream(outfile, new LZMA2Options(this.compressionLevel))
) {
is.transferTo(outxz);
}
}
}
}