Using Samaxes Minify nosuffix to overwrite original files - maven

I'm using Samaxes minify maven plugin to minify js and css files.
The plugin works fine in creating new files with a minifies js and css, but I'd like that the minified files will have the same name of the original files (overwriting them).
So I've tried to add the nosuffix true in the pom.xml, as below:
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.2</version>
<executions>
<execution>
<id>default-minify</id>
<configuration>
<charset>UTF-8</charset>
<closureCompilationLevel>WHITESPACE_ONLY</closureCompilationLevel>
<nosuffix>true</nosuffix>
<skipMerge>true</skipMerge>
<verbose>true</verbose>
<yuiPreserveAllSemiColons>true</yuiPreserveAllSemiColons>
<webappSourceDir>${basedir}/WebContent</webappSourceDir>
<cssSourceDir>css</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceDir>scripts</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
This is the plugin configuration in the pom.xml, all the options there are working fine (it identifies the sources, skips the merging, etc) but the nosuffix set to true doesn't overwrite the original files.
What I don't understand is that the plugin understands perfectly the source and the destination, it even logs that it saved some space, but the output is not minified:
[INFO] Creating the minified file [/mydir/css/styles.css].
[INFO] Uncompressed size: 32490 bytes.
[INFO] Compressed size: 24188 bytes minified (5833 bytes gzipped).
What am I missing?

Minify Maven Plugin runs during the Maven phase process-resources by default.
Since you are setting both the options nosuffix and skipMerge to true, which cause the output files to have the same path as the originals, it is causing the output files to be overridden by the source files during the phase package.
To fix this, change the plugin execution phase to package:
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.2</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<configuration>
<!-- ... -->
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
And exclude your source files from the WAR package.

Related

minify-maven-plugin (com.samaxes.maven v1.7.6) is not replacing the minified files with the original files

I want to minify JS and CSS files in maven project. I have used minify-maven-plugin (com.samaxes.maven v1.7.6). As per the documentation (https://samaxes.github.io/minify-maven-plugin/minify-mojo.html), I have set <nosuffix> and <skipMerge> as true because I want to maintain the file structure and replace the minified files with original files. I have also set the <phase>package</phase>.
After generating and deploying the WAR file, the JS and CSS files are not minified, they stay the same as before.
I also referred to some stackoverflow answers and set the <warSourceExcludes> option as per the suggestion provided at https://stackoverflow.com/questions/22117824/using-samaxes-minify-nosuffix-to-overwrite-original-files
After using the <warSourceExcludes> option, when I deploy the WAR file on the server, the JS and CSS files are not available and the application is showing 404 errors for the same. Please refer to my pom.xml configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
</configuration>
</plugin>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<configuration>
<charset>UTF-8</charset>
<webappSourceDir>${project.basedir}/WebContent</webappSourceDir>
<cssSourceDir>./</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceDir>./</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
<jsEngine>CLOSURE</jsEngine>
<nosuffix>true</nosuffix>
<skipMerge>true</skipMerge>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
Please suggest a proper solution. Thanks in advance !
The answer given by #Kristof Neirynck for the question How to get maven to build a war with minified files using yuicompressor-maven-plugin works for the issue mentioned.
I changed the <phase> property as prepare-package and added
<webappTargetDir>${project.build.directory}/minify</webappTargetDir>
in minify-maven-plugin after <webappSourceDir> and in the maven-war-plugin I set the following configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<webResources>
<resource>
<directory>${project.build.directory}/minify</directory>
</resource>
</webResources>
</configuration>
</plugin>
After using this configuration, when I deploy the WAR file in the Tomcat the original files are replaced with the minified files.

Unpack WAR Is Not Unpacking

I am trying to achieve the same objective to the question posted here, namely to assemble a war dependency into a deployable zip, with both the war and some of its unpacked *.yml files in the final zip. I have the war file correctly assembled into the zip, but I can't get the maven-dependency-plugin to unpack the *.yml files out of the war for re-packing.
For clarity, this is what I have:
Project 1
Name: myapp-war
Packaging: war
Project 2
Name: myapp-deploy
Packaging: pom
Dependency: myapp-war:war
Desired output: myapp.zip which includes:
bin/myapp.war
conf/application-*.yml
Here's what my plugin declaration looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>myapp</groupId>
<artifactId>myapp-war</artifactId>
<type>war</type>
<outputDirectory>${project.build.directory}/unpack</outputDirectory>
<includes>**/*.yml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
I've also tried a variant where I declared the <outputDirectory> and <includes> just underneath the <configuration>, which is permissible per the unpack mojo docs.
In either case, I get an empty myapp-deploy/target/unpack folder, along with [INFO] and [DEBUG] messages in the maven output stating:
[INFO] Configured Artifact: myapp:myapp-war:?:war
[INFO] Unpacking /build/myapp-war/target/myapp-war-0.0.1-SNAPSHOT.war to /build/myapp-deploy/target/unpack with includes "**/*.yml" and excludes ""
[DEBUG] Found unArchiver by type: org.codehaus.plexus.archiver.zip.ZipUnArchiver$__sisu2#692a65b1
[DEBUG] Expanding: /build/myapp-war/target/myapp-war-0.0.1-SNAPSHOT.war into /build/myapp-deploy/target/unpack
[DEBUG] expand complete
It appears that maven has understood my instructions, but doesn't seem to be unpacking as it claims to. Any idea what I'm doing wrong?
There is apparently a problem with the creation of the *.war dependency. It is first created with maven-war-plugin:war, and then repackaged with the spring-boot-maven-plugin:repackage. Unzipping the war from the command line succeeds, but produces a warning:
$ unzip myapp-war-0.0.1-SNAPSHOT.war
Archive: myapp-war-0.0.1-SNAPSHOT.war
warning [myapp-war-0.0.1-SNAPSHOT.war]: 7318 extra bytes at beginning or within zipfile
If I configure spring-boot-maven-plugin:repackage to disable the prepending of an executable *nix launch script to the war, maven-dependency-plugin:unpack works as expected:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>false</executable>
</configuration>
</plugin>

Place minified files into jar

I will use the maven dependency com.samaxes.maven#minify-maven-plugin to minify my frontend project. This frontend- project is packaged into a jar (java archive) - so I have no war file because I use Spring boot.
With the configuration at the bottom *.min.js and *.min.css files are generated but they are not pacakged into jar file. I have read some threads and I have tried something but with no success: in the JAR file there are still the unminified css and js files.
Does anyone have any hint what I can do. I have a lot of css and js files in my project in different folder structures and the minified files should be placed in the same place like the unminified files are.
<build>
<plugins>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.4</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<configuration>
<charset>UTF-8</charset>
<skipMerge>true</skipMerge>
<nosuffix>true</nosuffix>
<closureCompilationLevel>WHITESPACE_ONLY</closureCompilationLevel>
<webappSourceDir>src/main/resources</webappSourceDir>
<cssSourceDir>./</cssSourceDir>
<cssTargetDir>./minified</cssTargetDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
<jsEngine>CLOSURE</jsEngine>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
[EDIT]
I have edit the Maven plugin in this way.
I really have to use src/main/recoures - it a specification at our project.
My problem now is that the files in the different folders unter the under the main folder path public/app are minified BUT the folders with the minified files are at same level than my public folder is.
So I would need something like this:
<webappTargetDir>**public/app/**${project.build.outputDirectory}</webappTargetDir>
Is there a possibility to do it like this?
<build>
<plugins>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.4</version>
<executions>
<execution>
<id>default-minify</id>
<phase>prepare-package</phase><!-- When omitted defaults to 'process-resources' -->
<configuration>
<charset>UTF-8</charset>
<skipMerge>true</skipMerge>
<closureCompilationLevel>WHITESPACE_ONLY</closureCompilationLevel>
<webappSourceDir>src/main/resources/public/app</webappSourceDir>
<webappTargetDir>${project.build.outputDirectory}</webappTargetDir>
<cssSourceDir>./</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<jsSourceDir>./</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsEngine>CLOSURE</jsEngine>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
With the minify-maven-plugin configuration above I get the following folder structure at the left side but I would need the folder structure at the rigth side. Is there any possibility to do that?
The files are copied relatively from webappSourceDir to webappTargetDir. So if you want the structure to match, you should use similar patterns for those. Meaning, it should be
<webappSourceDir>src/main/resources</webappSourceDir>
<webappTargetDir>${project.build.outputDirectory}</webappTargetDir>
or
<webappSourceDir>src/main/resources/public/app</webappSourceDir>
<webappTargetDir>${project.build.outputDirectory}/public/app</webappTargetDir>
I've commited the working example here

build dependency on shared JavaScript file

Context:
We use a third-party maven plugin to compile lesscss into css
(lesscss-maven-plugin)
We cannot modify or update this plugin.
We cannot switch to another plugin, use grunt, or any other build
tool.
The plugin allows us to specify a JS file as a workaround to
upgrading the plugin itself.
Currently we have to have a copy of the
file included with each and every project
Question:
How can the JS file be packaged and a build/compile time dependency defined in the POM so that the file can be shared across projects easily?
Is it possible to use a URL for the file path instead of the {project.basedir}?
Example POM Snippet:
<plugin>
<groupId>org.lesscss</groupId>
<artifactId>lesscss-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/less</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/webapp/css</outputDirectory>
<compress>true</compress>
<includes>
<include>example.less</include>
</includes>
<lessJs>${project.basedir}/src/main/webapp/lib/less-1.7.0.min.js</lessJs>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

How to exclude files from being copied, by maven, into the exploded war

I have a Java web app, in which I have some folders within the standard webapp source directory (src/main/webapp) that I don't want to get copied over into the war (exploded or packaged).
One of the reasons I don't want these files copying over is that we run the YUI JS & CSS minimizer & compressor on .js and .css files within the exploded war. The files that I want to exclude produce errors during the compression phase. The other reason I don't want them adding to the war is that they support testing a single page JS app that lives within the webapp (they are client side JS test scripts that rely on node / angular.js).
Below are the relevant sections from the POM.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>parent-resources</id>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
</configuration>
<phase>generate-sources</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
I have tried, unsuccessfully, to use warSourceExcludes to exclude certain paths, but to no avail. An example of my usage is shown below, where client/ is a folder directly beneath src/main/webapp:
<configuration>
...
<warSourceExcludes>
<excludes>
<exclude>
client/
</exclude>
</excludes>
</warSourceExcludes>
...
</configuration>
What is the correct way to exclude certain paths, and or individual files, within the web app source directory from being included in the exploded war?
UPDATE
Following on from the suggestion from #maba I updated the configuration as follows:
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
<warSourceExcludes>client/</warSourceExcludes>
</configuration>
The folder, client/, still is getting copied across. Any ideas?
Thanks to #alexksandr & #maba for their answers - which though correct didn't fully resolve my issue.
The solution seems to be - though I am not sure exactly why this is the case - that the configuration section is not picked up on when it is placed within the execution section.
Taking my original pom.xml and re-factoring it to make it work gives:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
<warSourceExcludes>client/</warSourceExcludes>
</configuration>
<executions>
<execution>
<id>parent-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
The important detail seems to be that the configuration should be at the top level of the plugin and not within the execution section - though clearly the xml in my first attempt to use warSourceExcludes was way off target (see original question prior to the update section).
I could find no configuration of <warSourceExcludes> which worked, however <packagingExcludes> did work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<packagingExcludes>excludeMe/**</packagingExcludes>
</configuration>
</plugin>
In order to exclude all files from folder use wildcards like that client/**
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
<warSourceExcludes>client/**</warSourceExcludes>
</configuration>
Your warSourceExcludes is wrong. Here is what the maven-war-plugin says regarding warSourceExludes:
The comma separated list of tokens to exclude when copying the content of the warSourceDirectory.
So in your case this is what your configuration should look like:
<configuration>
...
<warSourceExcludes>client/</warSourceExcludes>
...
</configuration>
This means also that if you want to add some more excludes, just add them separated with a comma:
<configuration>
...
<warSourceExcludes>client/,otherDir/,evenMoreDir/</warSourceExcludes>
...
</configuration>

Resources