Rename static files on maven war build - maven

I am trying to figure out a way by which I could rename the static files I have in my web project, all of them at the same time.
Exactly what I need is to be able of filtering all my statics filenames to add a version number or something similar just to avoid them from being cached by browsers.
Something like converting custom.css into custom-1.23.css where 1.23 would be the value of a given filter.
This behaviour looks really similar to what resources plugin does with the content of the files, but I couldn't find a way of doing the same with the filenames.
Does anyone knows something similar?
Thanks a lot

You could change the directory they are served from rather than the file name e.g.
/static/${version}/custom.css
The resources plugin would let you change the target directory in the war.

1) Use any Maven plugin that is able to copy and rename resources. For example, copy-rename-maven-plugin. Configure the plugin to copy all static resources which you want to version and put them into a new directory inside the target directory, for example target/static_versioned:
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>copy-file</id>
<phase>generate-sources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<sourceFile>src/main/webapp/css/style.css</sourceFile>
<destinationFile>target/static_versioned/css/style_${project.version}.css</destinationFile>
</fileSet>
<fileSet>
<sourceFile>src/main/webapp/js/app.js</sourceFile>
<destinationFile>target/static_versioned/js/app_${project.version}.js</destinationFile>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
2) Configure maven-war-plugin to add versioned static files inside you war file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webResources>
<resource>
<directory>target/static_versioned</directory>
</resource>
</webResources>
</configuration>
</plugin>
3) Maven copies both versioned and original files into the result war file, so the original files need to be excluded:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<packagingExcludes>**/css/style.css,**/js/app.js</packagingExcludes>
<webResources>
<resource>
<directory>target/static_versioned</directory>
</resource>
</webResources>
</configuration>
</plugin>
It is worth mentioning that the target directory still has original and versioned files, but the war file contains only versioned files.
4) Use maven-war-plugin's filtering functionality to rename static resource links so all links point to versioned resources:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<packagingExcludes>**/css/style.css,**/js/app.js</packagingExcludes>
<webResources>
<resource>
<directory>target/static_versioned</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
For example, a link like <script type="text/javascript" src="js/app_${project.version}.js"></script> will be renamed into <script type="text/javascript" src="js/app_1.0-SNAPSHOT.js"></script>
Complete example is here: https://github.com/dmitrysobolev/maven-war-plugin-js-versioning-example

Related

Maven copy resources conditionally

In my Web Application, I have some .properties and .xml files that are processed and copied from my resources directory into classes.
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
I have defined a variable in pom.xml:
<stage>Development</stage>
Now I want to copy some resources according to this variable. Basically:
if $stage == Development --> copy
resources/logging-development.properties to
classes/logging.properties
if $stage == Production --> copy
resources/logging-production.properties to
classes/logging.propertiesinstead.
How can I achive this in my pom.xml?
(note - If possible, I am looking for a solution that does not require modifying the way maven is called by my IDE, so only modifications in pom.xml and the like)
NOTE: These are the only files that change from one environment to another
Thanks to the pointers in the comments I was able to do it. It is simple, but works. It just copies the desired file to the right filename and then it is moved as usual. The files that are not desired are excluded from being moved. I leave here my solution in case it can help others:
<properties>
<stage>Development</stage>
...
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-file</id>
<phase>generate-sources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>src/main/resources/logging-${stage}.properties</sourceFile>
<destinationFile>src/main/resources/logging.properties</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
....
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>logging-Development.properties</exclude>
<exclude>logging-Production.properties</exclude>
</excludes>
</resource>
....

generated file in the wrong place

I'm using the maven resources plugin to generate a plain file via filtering within a war file. I have the template file with variables in a the folder src/main/webapp/app
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp/app</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
the problem is that the generated file is in the WEB-INF/classes folder and I need it to be at the app folder at the root of the war.
The war structure should be
*.jsf
app/<generated_file>
WEB-INF/
How can I do it?
You have to add a copy-resources goal if you want to copy something to another structure during your build, with that you can point to a custom output path:
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
This can be found here in the documentation.
With the comments I could finally get the solution. It was simply to set the outputDirectory parameter to src/main/webapp/app and put the template in another place outside src/main/webapp. In my case, I put it in a folder src/main/jnlp. So finally this was the solution.. hope it helps others
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp/app</outputDirectory>
<resources>
<resource>
<directory>src/main/jnlp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Do I have to build maven webapp project everytime i make changes into static files?

I am using JBOSS AS7.1, Eclipse Luna for development. My eclipse installation does have a plugin installed for maven.
I have created my webapp project using maven command-line.
In my current set up, I have to build my maven project using mvn clean install every time for all changes, even for the static files like HTML, CSS.
Then, I have to deploy the generated WAR file using JBOSS console running at http://localhost:9990/console.
I am quite sure that there must be another way to do this. Surely, it does take a hell lot of time.
Please guide me to the approaches I can adopt for faster development.
One option is jrebel. It's not free though.
If you are not bound to JBOSS, you could use spring boot. It also supports automatic restart (spring boot devtools)
You can replace the static file in your target folder and launch a build skipping the compile phase.
It will save you a lot of time, when only updating static files.
It is not a good practice, but should let you achieve your goal.
How to:
Use the maven-clean-plugin to remove the files to replace from the target folder (or they will not be overwritten);
Use the resources tag if your static files are not the only content of the resources folder you want to copy (or your static files are not in resources folder at all);
Use the maven-compiler-plugin to skip the compile phase.
Customize this profile (and use it with mvn clean install -P skip-compile):
<profile>
<id>skip-compile</id>
<build>
<resources> <!-- optional -->
<resource>
<directory>src/main/resources/META-INF</directory>
<targetPath>META-INF</targetPath>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
<includes>
<include>**/*.html</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>${project.build.outputDirectory}/META-INF</directory>
<excludes>
<exclude>**/not_to_delete.xml</exclude>
</excludes>
<includes>
<include>**/*.html</include>
<include>**/*.css</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<skipMain>true</skipMain>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Filtering resources with different encodings

I have a maven project. In /src/main/resources/ I have two files foo.properties and bar.xml. The first is encoded in ISO-8859-1 and the second one in UTF-8.
Is there an easy way to get filtering for both files without breaking encoding?
As far as I see, I cannot specify an encoding in a <resource> block. It seems only possible to specify an encoding for the plugin itself, but this would lead in both files being filtered using the same encoding.
Ideally I would like to specify something like:
*.properties -> filter using ISO-8859-1
*.xml -> filter using UTF-8
*.pdf -> do not filter
anything else -> filter using default encoding
This doesn't feel like a very unusual situation, is there an easy way to configure this?
First i would suggest to put those resources which are iso based into a different directory like src/main/iso-resources and use something like this:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<name>Resource Test</name>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-iso-part</id>
<goals><goal>copy-resources</goal></goals>
<phase>process-resources</phase>
<configuration>
<encoding>ISO-8859-1</encoding>
<resources>
<resource>
<directory>src/main/iso-resources/</directory>
</resource>
</resources>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you don't want to keep your resources in separate directories you can also keep them in the default directory src/main/resources and play with exclusions.
The idea is:
file extensions that should not be filtered should be added to the default configuration using nonFilteredFileExtensions
let the default execution of the resources plugin process resources with the default encoding
exclude files that do no use this encoding from the build/resources configuration
for each additional encoding you want to handle, add an execution of goal "copy-resources" with explicit includes that selects only the targeted files
This example only handles .properties but you can easily generalize:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.properties</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
...
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
<executions>
<execution>
<id>properties</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>ISO-8859-1</encoding>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application*.properties</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Maven-resources-plugin won't copy .metadata folder

I'm trying to copy a folder or following structure with maven-resources-plugin:
root
|- .metadata
|- Project
\- .gitignore
Project directory and .gitignore files are copied, but .metadata directory is left out for some reason.
How do I copy all contents of root folder?
Here is configuration I tried:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/aut-ws</outputDirectory>
<useBuildFilters>false</useBuildFilters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>metadata</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<resources>
<resource>
<directory>H:\rcptt\workspaces\root</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
You're looking for the configuration parameter addDefaultExcludes. See the documentation page.
So your configuration section should look like the following:
<configuration>
<outputDirectory>${project.build.directory}/aut-ws</outputDirectory>
<addDefaultExcludes>false</addDefaultExcludes>
...
<resources>
<resource>
<directory>H:\rcptt\workspaces\root</directory>
</resource>
</resources>
</configuration>
If this was relating to the maven-assembly-plugin then I had this problem, and had to use the useDefaultExcludes property (recent versions of the plugin only); by default it is true and it needs to be set to false to include directories like .metadata. This doesn't seem to be applicable to maven-resources-plugin though, or it might just not be a documented property.
My first fix attempt would be trying modifying the resources element.
<resources>
<resource>
<directory>H:\rcptt\workspaces\root</directory>
<includes>
<include>**/*</include>
<include>**/.*</include>
</includes>
</resource>
</resources>
Also, if you comment out the <nonFilteredFileExtensions> element, does it work?
Edit to show full plugin configuration that works with Maven 3.2.2, Resources plugin 2.7, on both Windows 7 and RedHat Linux. Command for testing is mvn validate.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/testing123</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
<include>**/.*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Just add addDefaultExcludes as #Matthew Wise said.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
By default files like .gitignore, .cvsignore etc. are excluded which
means they will not being copied. If you need them for a particular
reason you can do that by settings this to false.
Documentation: https://maven.apache.org/plugins/maven-resources-plugin/copy-resources-mojo.html#addDefaultExcludes

Resources