-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressMojo.java
More file actions
145 lines (127 loc) · 4.91 KB
/
CompressMojo.java
File metadata and controls
145 lines (127 loc) · 4.91 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
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
/*
* Copyright 2026 the original author or authors.
*
* 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
*
* https://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 am.ik.maven.compression;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* Compresses static resource files using configured compressors. Scans configured
* resource directories for files matching the specified extensions and creates compressed
* variants.
*/
@Mojo(name = "compress", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true)
public class CompressMojo extends AbstractMojo {
/**
* The base output directory containing the resource directories to compress.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", property = "compression.outputDirectory")
private File outputDirectory;
/**
* Subdirectories under the output directory to scan for compressible files.
*/
@Parameter(property = "compression.resourceDirectories")
private List<String> resourceDirectories;
/**
* File extensions to target for compression (without dots).
*/
@Parameter(property = "compression.fileExtensions")
private List<String> fileExtensions;
/**
* Brotli compression quality (0-11). Higher values produce smaller files but take
* longer.
*/
@Parameter(property = "compression.brotli.quality", defaultValue = "11")
private int brotliQuality;
/**
* Whether to skip compression.
*/
@Parameter(property = "compression.skip", defaultValue = "false")
private boolean skip;
/**
* Whether to enable Brotli compression.
*/
@Parameter(property = "compression.brotli.enabled", defaultValue = "true")
private boolean brotliEnabled;
/**
* Whether to enable Gzip compression.
*/
@Parameter(property = "compression.gzip.enabled", defaultValue = "true")
private boolean gzipEnabled;
/**
* Whether to enable Zstandard compression.
*/
@Parameter(property = "compression.zstd.enabled", defaultValue = "true")
private boolean zstdEnabled;
/**
* Zstandard compression level. The practical range is 1-22, where 22 is the maximum
* ("ultra" mode). Higher values produce smaller files but take longer.
*/
@Parameter(property = "compression.zstd.level", defaultValue = "22")
private int zstdLevel;
@Override
public void execute() throws MojoExecutionException {
if (this.skip) {
getLog().info("Compression plugin is skipped.");
return;
}
List<String> directories = (this.resourceDirectories != null && !this.resourceDirectories.isEmpty())
? this.resourceDirectories : List.of("META-INF/resources", "resources", "static", "public");
Set<String> extensions = (this.fileExtensions != null && !this.fileExtensions.isEmpty())
? new LinkedHashSet<>(this.fileExtensions) : StaticResourceCompressor.DEFAULT_EXTENSIONS;
StaticResourceCompressor.Builder compressorBuilder = StaticResourceCompressor.builder().extensions(extensions);
if (this.brotliEnabled) {
compressorBuilder.addCompressor(new BrotliCompressor(this.brotliQuality));
}
if (this.gzipEnabled) {
compressorBuilder.addCompressor(new GzipCompressor());
}
if (this.zstdEnabled) {
compressorBuilder.addCompressor(new ZstdCompressor(this.zstdLevel));
}
StaticResourceCompressor compressor = compressorBuilder.build();
Path basePath = this.outputDirectory.toPath();
for (String directory : directories) {
Path resourceDir = basePath.resolve(directory);
try {
List<CompressionResult> results = compressor.compress(resourceDir);
for (CompressionResult result : results) {
logResult(result);
}
}
catch (IOException ex) {
throw new MojoExecutionException("Failed to compress resources in " + resourceDir, ex);
}
}
}
private void logResult(CompressionResult result) {
String fileName = result.sourceFile().getFileName().toString();
StringBuilder message = new StringBuilder();
message.append("Compressed ").append(fileName).append(": ").append(result.originalSize()).append(" ->");
for (Map.Entry<String, Long> entry : result.compressedSizes().entrySet()) {
message.append(" ").append(entry.getKey()).append(":").append(entry.getValue());
}
getLog().info(message.toString());
}
}