Exclude resource folder from WAR using Maven - maven

I am converting our ant build to maven using eclipse STS m2e. Now I have a problem:
I have a mapping folder under src/main/resources like below:
and the mapping folder has some .xml files.
Now I want to copy all the .xml files under mapping folder to WEB-INF/classes folder in the final WAR and for that I did this:
<resources>
...
<resource>
<directory>${basedir}\src\main\resources</directory>
</resource>
<resource>
<directory>${basedir}\src\main\resources\mapping</directory>
</resource>
...
</resources>
By doing this it is also creating a mapping folder in the WAR under WEB-INF/classes. How can I exclude this mapping folder, as all the contents are already added under WEB-INF/classes path directly.
Please Help.

<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes><exclude>mapping/</exclude></excludes>
</resource>
<resource>
<directory>${basedir}/src/main/resources/mapping</directory>
</resource>

Related

Maven shade plugin is not creating "src/main/resources" directory,but it is placing the files directly under root

I am building uber jar with Maven shade plugin. But the jar has config/properties files directly in root instead of under "src/main/resources". Any help is appreciated on how to troubleshoot it ?
Tried by adding the below section in pom, but no luck
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources> ```

Maven override files in classpath if folder Y exists

I want to add some local config files to the classpath of maven mavenprojekt.
These files "config-local" shall override existing default config file out of my resources dir.
So only when the directory "config-local" exists the default config-files shall be replaced by the local config.
I tried to add the dir as resource to my maven Build, but it doesn't worked and I'm not sure what might happen if the config-local does not exist.
The answer was very easy:
Maven Resources Plugin adds the resources in reverse declaration order:
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<!-- The resources will be placed in reversed order in the war
that means first entry will be added as last resource and may override other resources -->
<resource>
<!-- ATTENTION! we need the config-local declration at first cause it shall be placed as last resource in the classpath -->
<directory>config-local</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
Just moving the config-local resource on top and it will be added as last resource and may override other default files.
If the folder not exists Maven will skip it without any problem.

How to include resources in jar

I need to include contents of resources folder into my jar. I know to include from src/main/resources folder. But my structure is as follow
enter image description here
Any help will be appreciated.
You can change the folders for your resources:
<build>
...
<resources>
<resource>
<directory>resources</directory>
</resource>
<resource>
<directory>other/optional/resource/path</directory>
</resource>
</resources>
...
</build>
It's best to stick with the src/main/resources convention but if you insist you can use maven-antrun-plugin and create a task to copy the files into your target/classes folder.

Why spring only picks from src/main/resources folder to add to classpath?

Why spring only picks from src/main/resources folder to add to classpath ?
That is the default folder for spring spring maven project config.
If you want to change it you can see the example here:
http://www.mkyong.com/maven/how-to-change-maven-resources-folder-location/
Otherwise for Annotated version you can view the Web Tutorial on the Spring.IO website:
http://spring.io/guides/tutorials/web/
Maven uses default classpath for the resource folder which is "src/main/resources"
if you want to include your own custom folder to class path, you have to update pom.xml file to indicate maven to include your new folder into classpath. use following code template to update pom.xml
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
.....
<resource>
<directory>path to your folder to put in classpath</directory>
</resource>
</resources>
</build>
if your pom.xml file doesn't have build or resource tag already then just add only add following piece of code to pom.xml
<build>
</resources>
<resource>
<directory>path to your folder to put in classpath</directory>
</resource>
</resources>
</build>
and check your Effective POM to verify that your custom folder and resource folder both are present there or not.

Maven - Exclude certain resource files into WAR from the default src/main/resources location

Currently, I want to exclude some files from the default src/main/resources folder into my WAR when packaging
I tried using maven-war-plugin with the following configuration but failed.
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
</webResources>
...WEB-INF/classes will still contain the XML files.
How to do so?
As pointed out in https://stackoverflow.com/a/2737635/722997, a quick way to exclude files from the WAR package is to exclude them in the build->resources section, like:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
</resources>
...
</build>
Note: take into account that the following configuration will only affect default executions of Maven (WAR package, JAR package, ...), but not assemblies or other user configurations.
This is somewhat late to this question, but I was just trying to do the same thing, and have found that (with the maven-war-plugin 3.1.0 version), adding:
<packagingExcludes>WEB-INF/classes/*.xml</packagingExcludes>
to the configuration should do what was asked for (it worked for me to remove properties files we didn't want to distribute with the war file).
From the documentation of maven war plugin, you can include and exclude resources as follows:
...
<configuration>
<webResources>
<resource>
<!-- the default value is ** -->
<includes>
<include>**/pattern1</include>
<include>*pattern2</include>
<includes>
<!-- there's no default value for this -->
<excludes>
<exclude>*pattern3/pattern3</exclude>
<exclude>pattern4/pattern4</exclude>
</excludes>
</resource>
</webResources>
</configuration>
...
Are you following this and it still does not work? If so, can you post your pom snippet?

Resources