该回答引用GPTᴼᴾᴱᴺᴬᴵ
要使用 Maven htmlcompressor 插件,您需要在 Maven 配置文件 pom.xml 中添加以下插件配置:
<build>
<plugins>
<plugin>
<groupId>com.googlecode.htmlcompressor</groupId>
<artifactId>htmlcompressor-maven-plugin</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>${project.build.directory}/classes/templates</sourceDirectory>
<outputDirectory>${project.build.directory}/classes/templates</outputDirectory>
<preserveComments>false</preserveComments>
<compressJavaScript>true</compressJavaScript>
<compressCss>true</compressCss>
<removeIntertagSpaces>true</removeIntertagSpaces>
</configuration>
</plugin>
</plugins>
</build>
这个插件会在 process-resources 构建阶段执行,并压缩 HTML 文件。注意,此处使用了 ${project.build.directory} 变量,它将指向项目构建输出目录。
·
要压缩 Thymeleaf 模板文件,您需要将这些文件放在 Maven 项目的 src/main/resources/templates 目录下,并在上面的配置中设置正确的源和输出目录。
·
您可以根据需要调整其他配置选项,如是否保留注释、压缩 JavaScript 和 CSS 等。有关更多信息,请查看 htmlcompressor-maven-plugin 的文档。
·
最后,在命令行中执行 mvn clean package 命令即可构建项目并压缩 HTML 文件。