Configure encoding for different filetypes in maven? - maven

I use the maven-resource-plugin to filter some resources in my maven project. In my parent project I have:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
In a sub project I have a test.properties file which is a plain java properties file with default encoding=ISO-8859-1. This file contains:
aboutText=Version ${project.version} © 2012 blabla
To make sure this file filters correctly I have split the maven-resource-plugin into separate executions each with its encoding:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>ico</nonFilteredFileExtension>
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
<executions>
<execution>
<id>filter-properties-files</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- java properties files are encoded in ISO-8859-1 so when
filtering those files we stick with that encoding. -->
<encoding>ISO-8859-1</encoding>
<outputDirectory>${basedir}/after</outputDirectory>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/before</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>filter-non-properties-files</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<outputDirectory>${basedir}/after</outputDirectory>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/before</directory>
<includes>
<include>**/*.product</include>
<include>**/*.inf</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This seems overkill and I have a feeling that I am either not using the plugin correctly or that this problem should be handled in another way. Maybe stick to encoding special characters in properties files directly:
aboutText=Version ${project.version} \u00a9 2012 blabla
?

The question is an invaluable answer in itself, as apparently the complicated procedure the author provides is the only way to configure different encodings for various filtered file types. The example given, however, is specific to the author's non-standard use-case, and glosses over a few important details, without which actual use of the example is fraught with gotchas:
It isn't obvious, but in the author's example the default resource copying goal resources is still enabled and runs in addition to the two defined goals!
You'll notice that the author used the lifecycle phase generate-resources instead of the default process-resources. This is a trick to get around the first point above; by making the two copy-resources goals occur in an earlier lifecycle phase, the resources are copied according to the given rules, and then when the default-resources goal comes along the original resource copying is left intact, apparently because its overwrite setting defaults false. But it would be better to disable altogether the default-resources execution.
The author provides an outputDirectory declaration. It would be natural to think that the author only provided this because a custom output directory was desired; after all, the resources goal provides a default value for this setting. Strangely, though, for the copy-resources goal this setting is actually required! There is a standard Maven variable ${project.build.outputDirectory} which can be used as the value, however.
Building on the author's own example in the question, here is a cut-and-paste way to filter properties files using ISO-8859-1, copy other files without filtering, and prevent the default resource copying from occurring; all using the standard source and target directories:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<!-- Turn off default resource copying. -->
<id>default-resources</id>
<phase />
</execution>
<execution>
<!-- Filter resources in properties files. -->
<id>filter-properties-files</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>ISO-8859-1</encoding>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<!-- Do not do property substitution in files that are not properties files, such as binary files. -->
<id>copy-other-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Update: After more research, I believe that instead of disabling the default execution, one could simply modify the default execution to ignore properties files, and add filtering of properties files as an additional execution. Moreover if the resources goal were used instead of copy-resources, there would be no need to indicate an output directory or a phase, as resources:resources automatically binds to the process-resources phase and outputs to ${project.build.outputDirectory}. Note that I've updated to version 3.1.0 of the plugin, so perhaps some of these options were not available in the version I used above. I have not tested this new, shorter configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<!-- Ignore properties files by default. -->
<id>default-resources</id>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<!-- Filter resources in properties files using ISO-8859-1. -->
<id>filter-properties-files</id>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<encoding>ISO-8859-1</encoding>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Maybe someone could test this new configuration and let me know if it works the same as the original one I gave above.

You are right that you have to configure executions... You could get away with two executions rather that the three you seem to be using if you were following the standard directory layout but you seem to be putting files from ${basedir}/before into ${basedir}/after so the default execution of resources:resources coupled with adding multiple resources to the build section will not work for you.
The encoding of filtered files was an oversight in the original pom model specification and will likely be fixed in some as yet unspecified version of Maven

Related

can you provide overwrite on a per resource basis using the maven-resources-plugin?

So I'm having a problem with sharing a resources root because of IntelliJ, We have some resources we already add with the resource plugin, some of these resources I do not want to overwrite for performance reasons. In other cases I would like them overwritten So we don't forget we have to delete them if we change them. Can I just specify (mostly copy-pasta-ed from the official documentation
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<overwrite>true</overwrite> <!-- will this work -->
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
in the resources I want to overwrite? really this question is simple Can I put overwrite directly on the resource? Will it work?

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

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>

Apache Maven Resources Plugin exclude a directory

I'm trying to copy some resources from one point to an other during the build process. Therefore I use the Apache Maven Resources Plugin. Actually I exclude some files, I don't need. But I want also to exclude a directory. I tried serveral ways but it didn't work.
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-client-product</id>
<phase>verify</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/pro/client</outputDirectory>
<resources>
<resource>
<directory>target\products\client\win32\win32\x86\</directory>
<excludes>
<exclude>p2</exclude>
<exclude>eclipsec.exe</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
In this example I tried to exclude the folder "p2".
<exclude>*/p2/**</exclude>
<exclude>p2/**</exclude>
<exclude>**/p2</exclude>
Also don't work.
<exclude>**/p2/**</exclude>
is the correct answer thanks to #khmarbaise.
Try this
<resource>
<directory>p2</directory>
<excludes>
<exclude>p2/**</exclude>
</excludes>
</resource>

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