我正在寻找 maven 配置来缩小 spring boot 应用程序的 js/css 文件。 对于普通的 Java Web 应用程序,以下配置有效
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<nosuffix>true</nosuffix>
<webappDirectory>${project.build.directory}/min</webappDirectory>
<excludes>
<exclude>**/*-min.js</exclude>
<exclude>**/*.min.js</exclude>
<exclude>**/*-min.css</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/min</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
但是当我用 spring boot 尝试这个时
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<nosuffix>true</nosuffix>
<webappDirectory>${project.build.directory}</webappDirectory>
<excludes>
<exclude>**/*-min.js</exclude>
<exclude>**/*.min.js</exclude>
<exclude>**/*-min.css</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<excludes>
<exclude>**/*.js</exclude>
</excludes>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
现在 spring-boot-maven 插件配置无法指定 webResources 目录。
当压缩目标运行时,它会缩小 js 和 css。然后重新打包的目标只是在复制 webapp 资源阶段用未压缩的版本覆盖缩小的脚本/css。有没有办法跳过复制步骤或在复制步骤后运行压缩目标? 基本上是我可以在 spring 引导构建期间压缩 css/脚本吗?
最佳答案
我能够使用名为 minify-maven-plugin 的不同缩小插件来完成此操作.
我能够利用这个插件的 webappTargetDir
属性来指示它把缩小的文件放在 ${project.build.outputDirectory}
目录中(通过默认解析为 ${project.build.directory}/classes
)。
然后将插件执行绑定(bind)到 prepare-package
阶段,我能够让它缩小我的源文件并将它们复制到 ${project.build.outputDirectory}
code> 在执行 package
阶段之前。这会导致在打包发生之前用缩小的源文件覆盖原始源文件的预期行为。
下面是我的 POM 文件的摘录,我在其中配置了 minify-maven-plugin 以这种方式运行。
<build>
. . .
<plugins>
. . .
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<id>default-minify</id>
<phase>prepare-package</phase>
<configuration>
<charset>UTF-8</charset>
<webappSourceDir>${basedir}/src/main/resources/static</webappSourceDir>
<webappTargetDir>${project.build.outputDirectory}/static</webappTargetDir>
<jsSourceDir>js</jsSourceDir>
<jsSourceFiles>
<jsSourceFile>myscript.js</jsSourceFile>
</jsSourceFiles>
<jsTargetDir>js</jsTargetDir>
<jsFinalFile>myscript.js</jsFinalFile>
<jsEngine>CLOSURE</jsEngine>
<skipMerge>true</skipMerge>
<nosuffix>true</nosuffix>
<verbose>true</verbose>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
请注意,我已选择将我的原始(即未压缩)Javascript 源文件放在项目树的 ${basedir}/src/main/resources/static/js
中。我还启用了 nosuffix
和 skipMerge
标志,以便将 myscript.js
的缩小输出保存到具有相同名称的文件中(根据您的情况,这可能不是您想要的)。
更新(2018-04-13):根据要求,以下是我使用 minify-maven-plugin 的项目的整个 POM 文件。关于这个 POM 文件,有几点需要注意:
- 我删除了一些不相关的依赖项和其他特定于我的项目的配置参数,但完成任务所需的一切都应该在这里。
- 我的项目仅在使用
release-profile
时执行 Javascript 压缩,因此您需要使用类似mvn clean install -P release-profile
为了进行缩小。 - 我已将 Maven 配置为将项目构建为可执行 jar,可以使用
java -jar target/project-0.0.1-SNAPSHOT-exec.jar
运行。这将启动 Spring Boot 应用程序。 - 我留下了 Groovy 和 Spock testing framework 的依赖项,因为我在我的项目中使用这些来编写测试。
希望这对您有所帮助。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>project</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-boot.version>1.5.10.RELEASE</spring-boot.version>
<spock.version>1.1-groovy-2.4</spock.version>
<groovy.version>2.4.6</groovy.version>
<maven.javadoc.skip>true</maven.javadoc.skip>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.project.Application</mainClass>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>external.atlassian.jgitflow</groupId>
<artifactId>jgitflow-maven-plugin</artifactId>
<version>1.0-m5.1</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<pushReleases>true</pushReleases>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Spec.*</include>
</includes>
<runOrder>random</runOrder>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*SpecIT.*</include>
</includes>
<runOrder>random</runOrder>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>release-profile</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<id>default-minify</id>
<phase>prepare-package</phase>
<configuration>
<charset>UTF-8</charset>
<webappSourceDir>${basedir}/src/main/resources/static</webappSourceDir>
<jsSourceDir>js</jsSourceDir>
<jsSourceFiles>
<jsSourceFile>myscript.js</jsSourceFile>
</jsSourceFiles>
<webappTargetDir>${project.build.outputDirectory}/static</webappTargetDir>
<jsFinalFile>myscript.js</jsFinalFile>
<jsEngine>CLOSURE</jsEngine>
<skipMerge>true</skipMerge>
<nosuffix>true</nosuffix>
<verbose>true</verbose>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Skip source jar generation -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<configuration>
<skipSource>true</skipSource>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
关于javascript - Spring Boot 缩小 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000121/