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
Related
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.
So I have 2 files that have a list of other files that I would like to package. This is what the directory would look like
/mydir
|-pom.xml
|-a.out
|-b.out
|-c.out
|-d.out
|-read1.list
|-read2.list
Here are the contents of read1.list and read2.list
read1.list
a.out
b.out
read2.list
c.out
So I'd like maven to read the read1.list and read2.list files, take the list of files, and copy them to another directory. I think I have the reading of the files part but I don't know what to do after that.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>read property file</id>
<phase>process-resources</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/read1.list</file>
<file>${basedir}/read2.list</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
I got this code from this post but I was unsure what to do next.
Maven: Read property file and copy resources
Any help would get greatly appreciated.
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.
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>
im at the point in my project where im moving data connections to the beta and production databases for testing. obviously, having the alpha database credentials stored in the source repository is fine, but the beta and production credentials, id be put in front of a firing squad for that one.
i know maven can have a {userdir}/build.properties file. this is the file i want to use to keep the db credentials out of the source repository. but i can't seem to get maven to figure out that for file x.cfg.xml it has to replace values.
so i have in one of my hibernate.cfg.xml files this line
<property name="hibernate.connection.url">#ssoBetaUrl#</property>
now how do i get maven to replace that variable with the value thats in the {userdir}/build.properties file?
edit-------------
ive been playing with the properties-maven-plugin plugin but i seem to not be able to get it to fire. i put this in my parent pom
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>read-properties</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
</execution>
</executions>
</plugin>
but when it builds, it does not fire. if im reading http://maven.apache.org/maven-1.x/reference/properties.html right it should find the build properties file in the ~/build.properties folder and go from there, but im not sure.
I think you are approaching this the wrong way around. Instead of having the build process bake the appropriate connection details into the JAR file you should instead have the program look for a configuration file at startup.
Typically, my hibernate based apps, will look for a file under %user.home&/.appname/config.properties and load DB credentials and other deployment specfic data from there. If the file is missing, a default version can be included in the JAR and copied to this location (on initial startup so you don't have to copy-paste the file to new systems) that is then edited with appropriate settings.
This way, you can use the same build to produce JAR (or WAR) files for test and production servers, the differences will be in the (presumably already deployed) configuration files. This also makes it possible to have multiple production deployments, each talking to a different database, without any complications in the build process.
You could use two plugins.
properties-maven-plugin
replacer
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>{userdir}/build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/**/*.*</include>
</includes>
<replacements>
<replacement>
<token>#ssoBetaUrl#</token>
<value>http://[anyURL]</value>
</replacement>
</replacements>
</configuration>
</plugin>