Reduce your code size and optimize performance by utilizing the YUI compressor to compress

While searching for a js/css compressing tool for my maven project, I came across this discussion on the topic - Maven Javascript Compressor

The tool functions well, but I am encountering one particular issue that I'm still trying to resolve.

If you do not specify a suffix with yui, it defaults to setting *-min and generates a new file alongside the original. For instance, if I had a file named scripts.js, after running the maven build with the plugin, I end up with both scripts.js and scripts-min.js files. What I actually need is only the *-min file. Even when I set <nosuffix> to true, it indicates that the compression was successful but does not override the file.

Is there a way to have only the compressed files retaining the original name?

Answer №1

Indeed, the maven Javascript Compressor in use successfully compressed the files. However, it seems that during the maven package lifecycle, compression occurred at the prepare-package phase. Subsequently, in the package phase, the maven-war-plugin reverted the original files and replaced the compressed versions.

    <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.5.1</version>
            <executions>
                <execution>
                <id>yui-compress</id>
                <phase>prepare-package</phase>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>**/*-min.js</exclude>
                    <exclude>**/*.min.js</exclude>
                    <exclude>**/*-min.css</exclude>
                    <exclude>**/*.min.css</exclude>
                    <exclude>**/amazeui*</exclude>
                    <exclude>**/*.css</exclude>
                </excludes>
                <encoding>UTF-8</encoding>
                <nosuffix>true</nosuffix>
                <statistics>false</statistics>
                <linebreakpos>5000</linebreakpos>
                <useSmallestFile>true</useSmallestFile>
                <jswarn>false</jswarn>
                <warSourceDirectory>${basedir}/web</warSourceDirectory>
                <webappDirectory>${project.build.directory}/min</webappDirectory>
                <skip>false</skip>
            </configuration>
        </plugin> 
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${project.build.directory}/min</directory>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

Answer №2

Here is the ant script I ended up creating for use with the maven ant plugin:

<delete includeEmptyDirs="false">
    <fileset dir="${basedir}/target/${project.build.finalName}/resources/js" excludes="**/*-min.js" />          
</delete>
<move todir="${basedir}/target/${project.build.finalName}/resources/js" includeemptydirs="false">
    <fileset dir="${basedir}/target/${project.build.finalName}/resources/js" />
    <mapper type="glob" from="*-min.js" to="*.js" />
</move>

<delete includeEmptyDirs="false">
<fileset dir="${basedir}/target/${project.build.finalName}/resources/css"
    excludes="**/*-min.css, **/*.ttf, **/*.png, **/*.jpg" />
</delete>
<move todir="${basedir}/target/${project.build.finalName}/resources/css" includeemptydirs="false">
    <fileset dir="${basedir}/target/${project.build.finalName}/resources/css" />
    <mapper type="glob" from="*-min.css" to="*.css" />
</move>

To provide a brief explanation: this script first removes any old uncompressed files and then changes the filenames of *-min.js and *-min.css to simply end in *.js and *.css.

I have tested it and it works perfectly. Additionally, please be cautious if you have other types of files in the /js or /css directories (files that do not end in .js or .css). You will want to exclude those file types by adding their extensions to the exclude property.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why is the BoxLayout panel in Java GUI behaving oddly when another BoxLayout panel is inside it?

Trying to create nested Panels inside each other. I am new to Java, so please bear with me. The issue I'm encountering is with the pnlLogin behaving strangely. It displays all three panels inside it only when they don't have a Layout set. When I ...

Issue: Failed to locate or load primary class in Maven converted project

After creating a new Java project and successfully running it, I decided to convert it into a Maven project. I made sure to resolve all errors in the POM.XML file as well. However, when I attempted to run the class file, I encountered the following error: ...

What is the best way to automatically add a space every four digits in an input field using JavaScript?

How can I format a card number input field so that it displays spaces after every 4 digits when the customer enters the full 16-digit number? <input type="text" id="input-number" placeholder="e.g 1234 5678 9123 0000"> ...

Performing a sequence of actions using Jquery Queue() function and iterating through each

I am facing an interesting challenge with an array called result[i]. My goal is to iterate through each field in the array and add it to a specific element on my webpage. $("tr:first").after(result[i]); However, I would like this process to happen with a ...

The Bootstrap nav-link class functions perfectly in Firefox, smoothly guiding users to the appropriate section. However, it seems to be experiencing some issues

I am currently working on customizing a one-page web template and I don't have much experience with Bootstrap. The template can be found at . My issue is that the menu items are not functional in Chrome. When I click on any menu item, nothing happens ...

Tips for changing a list item by pressing the Enter key

I am looking to enable the editing of an ordered list by using contenteditable. As the user makes changes or adds new elements to the list, I want to be able to manipulate the text (such as wrapping it in a span tag or replacing the text). Currently, I ha ...

Enhancing Java's download speed performance

Greetings, I have been working on improving a download tool and have noticed that the downloading speed is quite slow. Upon further investigation, it seems that my code may be the issue. Here is the snippet in question: float fileSize = Float.pars ...

Unable to establish a connection between MongoDB and Node.js

I recently installed mongoose using npm install mongoose, however, when I attempt to run my JavaScript file it's not connecting. Below is the code snippet: const config = require('config') const express= require('express') const ...

Opt for buttons for color selection instead of a checkbox toggle

I attempted different approaches to replace the existing checkbox with a button but encountered difficulty. Using the onClick method also proved unsuccessful. VIEW DEMO HERE: https://jsfiddle.net/yxez4a2u/ HTML: <div class="form-check form-switch ...

Unable to proceed with entering subsequent values into the input field after the initial input in reactjs

Issue with login form: Unable to provide second value after entering the first one. The text input field does not allow for entering more than one value before completing the existing entry. import React, { useCallback } from 'react'; import { Te ...

Shorten Text - React Native

I currently have a React Native application with a FlatList component. The logic I initially implemented was to display an Expand/Collapse arrow whenever the content at the 100th position in the list is not empty. However, I now realize that this approach ...

How to Detect Font Resizing in Google Web Toolkit (GWT)

I am trying to find a way in GWT to capture the font resize event that occurs when the user changes the size of the font by using Ctrl-Mouse Scroll or going to View -> Zoom. I have searched on Google and looked on StackOverflow but haven't found an ...

Usual method for managing delayed asynchronous callbacks

Hello, I'm currently delving into web application development using Javascript and have some questions. As I navigate the world of Javascript applications, it's clear that they operate asynchronously. I'm curious if there is a standard way ...

Obtain the printed value of a button via PHP and manipulate it using JavaScript

I have a dynamic table that displays results from a database. <table id="table" class="table datatable-responsive"> <thead> <tr class="bg-teal"> <th>#</th> <th>Date & Time</th& ...

Transforming the text to a new line

I have been attempting to format lengthy texts from a JSON file, but I haven't been successful. The text keeps displaying as multiple sections within a single response, which can be seen in the image below. I've tested solutions like word-break a ...

Selenium (Java) automation encounters missing 'value' error when calling a function

I am currently attempting to automate a specific page, with the goal of enabling the mouse to click anywhere on the page. However, I am encountering the following error: call function result missing 'value' The elements present on the page incl ...

Implementing PJAX requests with a touch of Instantclick style (loading essential content only upon hovering over the links)

When it comes to website loading, PJAX simplifies the process by only fetching the necessary content for a requested page, rather than reloading all common elements. This approach is similar to using a single-page app with Angular JS, complete with back bu ...

Retrieve the value of the key in my AngularJS JSON object

Describing my object object1 ={ name: xxx, Id: 212 } I want to transform it into: { 212:xxx } Is there anyone who can guide me on how to achieve this? for(var key in object1) { if(object1.hasOwnProperty(key)) { console.log( eac ...

It appears that the functionality of RegExp.prototype.exec() is not functioning as expected

const fs = require('fs') const jsdocFinder = /\/\*\*\n(.+?)\*\//gs /** * Implementing a function to convert JSDocs into JSON format. * @function * @param {String[] | String} dirs The directory or directories of ...

utilize jQuery and AngularJS to transform a JSON object into a nested JSON structure

Is there a way to convert my current JSON file into a nested JSON structure like the one below? I've attempted various methods (How to convert form input fields to nested JSON structure using jQuery), but I'm not achieving the desired outcome. Ca ...