Maven override files in classpath if folder Y exists - maven

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.

Related

Exclude resource folder from WAR using 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>

several resource rules are not properly recognized by maven eclipse:eclipse

I have a pom.xml with the following under <build>:
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<!-- exclude binary files. They get corrupted by filtering -->
<exclude>**/*.zip</exclude>
</excludes>
</resource>
<!-- copy binary files separately without filtering. They get corrupted by filtering -->
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.zip</include>
</includes>
</resource>
</resources>
Now this works perfectly when I build a jar using maven install. It then excludes the zip files from filtering, but copies them separately whithout filtering.
However, when I do a maven eclipse:eclipse, I see in eclipse under (right click) MyProject->Properties->Java Build Path->Source->MyProject/src/main/resources that **/*.zip is excluded. So apparently only the first <resource> element in my pom is recognized.
I need both recognized, since I also need the zip file copied to targetEclipse when working in dev environment.
Any idea how to accomplish this?
The maven docu about eclipse:eclipse states:
Invokes the execution of the lifecycle phase generate-resources prior to executing itself.
So regarding the resources it should just do the same as when I execute maven install.
So I figured it out myself...:
Apparently the eclipse:eclipse only allows one configuration per resource directory and ignores all others.
So I created a second resource directoryx, where I put all the binary files. I now only apply the filtering to the one containing no binary files. That way it works both with eclipse:eclipse and install:
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<!-- copy binary files separately without filtering. They get corrupted by filtering -->
<resource>
<directory>${basedir}/src/main/bin</directory>
<filtering>false</filtering>
</resource>
</resources>

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: how to place resource file together with jar?

I have \src\main\resources\logback.xml file. When I run mvn package it is placed into the jar by default. How do I make Maven place it next to jar, not inside?
So, well, I just want Maven to copy the resource to the same folder where jar will be.
No plugin definitions needed, just edit the build resources:
<build>
<resources>
<!-- regular resource processsing for everything except logback.xml -->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>logback.xml</exclude>
</excludes>
</resource>
<!-- resource processsing with a different output directory
for logback.xml -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>logback.xml</include>
</includes>
<!-- relative to target/classes
i.e. ${project.build.outputDirectory} -->
<targetPath>..</targetPath>
</resource>
</resources>
</build>
Reference:
process-resources
(Maven Book)
<build><resources> (POM
Reference)
You can build a ZIP file containing both using the Maven Assembly plugin.
You can also use Maven Antrun plugin or similar to put whatever file you want but the idea of single artifact per project is built deep into Maven internals so there is a chance it will hunt you down elsewhere.
Exclude that file:
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
And then use the Maven Antrun plugin to copy the file.
However, the latter part does not make much sense. If it is a configuration file to put in a server, simply manually copy it.

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