Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot minify javascript

I'm looking for maven config to minify js/css files for a spring boot application. For a normal java web app the following config works

            <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>

But when I try this with 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>

now spring-boot-maven plugin configuration does not have a way to specify webResources directory.

When the compress goal runs it minifies the js and css. Then the repackage goal just overwrites the minified scripts/css with the uncompressed versions during copying webapp resources phase. Is there a way to skip the copy step or to run the compress goal after the copying step? Basically is can I compress css/scripts during a spring boot build?

like image 236
Ujjwal Pathak Avatar asked Oct 16 '25 15:10

Ujjwal Pathak


1 Answers

I was able to accomplish this using a different minification plugin called the minify-maven-plugin.

I was able to take advantage of this plugin's webappTargetDir property to instruct it to place the minified files in the ${project.build.outputDirectory} directory (which by default resolves to ${project.build.directory}/classes).

By then binding the plugin execution to the prepare-package phase, I was able to have it minify my source files and copy them into ${project.build.outputDirectory} before the package phase is executed. This results in the desired behavior of overwriting the raw source files with the minified source files before packaging takes place.

Below is the excerpt from my POM file where I configured the minify-maven-plugin to behave in this way.

<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>

Note that I have chosen to place my raw (i.e., not minified) Javascript source files in ${basedir}/src/main/resources/static/js in my project tree. I have also enabled the nosuffix and skipMerge flags so that the minified output of myscript.js is saved to a file of the same exact name (this might not be what you want, in your case).

Update (2018-04-13): As requested, following is my entire POM file for the project in which I am using the minify-maven-plugin. There are a few things to note about this POM file:

  • I have removed some irrelevant dependencies and other configuration parameters which are specific to my project, but everything you need to accomplish your task should be here.
  • My project only performs Javascript minification when the release-profile is in use, so you will need to build this project with something like mvn clean install -P release-profile in order for minification to be performed.
  • I have configured Maven to build the project as an executable jar, which may be run using java -jar target/project-0.0.1-SNAPSHOT-exec.jar. This will start up the Spring Boot application.
  • I have left in the dependencies for Groovy and the Spock testing framework, since I use these for writing tests in my project.

Hopefully this proves helpful.

<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>
like image 159
Tom Catullo Avatar answered Oct 18 '25 03:10

Tom Catullo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!