Exclude property files from resources - maven-2

How to exclude files from src/main/resources, for ex : I have a folder named "map" in there, which I wanna keep and I want to delete everything from war(or not to package it inside at firstplace).
Or alternative but same result, exclude all *.resources files from src/main/resources and put in war everything else?
Thank you

You may configure your resources like this:
<build>
<resources>
<resource>
<directory>src/main/resources/map</directory>
</resource>
</resources>
</build>
or this:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.log</exclude>
</excludes>
</resource>
</resources>
</build>
For more details, click here.

If you don't want some resources to be copied into target/classes, you can define includes or excludes in the resource element as documented in Including and excluding files and directories. For example:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/map/*.*</exclude>
</excludes>
</resource>
</resources>
</build>
If you want resources to be still copied into target/classes but for some reason don't want them to be packaged in the final artifact, then configure the maven war plugin to use packagingExcludes.

The official documentation for the maven resources plugin describes how you can perform includes and excludes.
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

Related

Maven : Excluding src/main/webapp/resources from the WAR is not working

I have src and src_backup directories that are identical except that I have created src_backup from src by renaming (cache busting purpose) all the .js and .css files in
src/main/webapp/resources
directory.
I am using maven-war-plugin 2.4. I want the WAR file to include renamed files from
src_backup/main/webapp/resources
and exclude original files from
src/main/webapp/resources
I am able to include from src_backup but cannot exclude from src. What I end up getting is the WAR file containing both versions of files (original ones as well as renamed ones). I have read more than 5 different sources (such as stackoverflow) and tried more than 20 different ways to do so but it does not work.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>targetWAR</warName>
<warSourceExcludes>/${baseDir}/src/main/webapp/resources</warSourceExcludes>
<webResources>
<resource>
<directory>src_backup/main/webapp/resources</directory>
<targetPath>resources</targetPath>
</resource>
<resource>
<directory>src_backup/main/webapp/WEB-INF/views</directory>
<targetPath>WEB-INF/views</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
I have also tried to use resources tag in the build tag but did not work.
<resources>
<resource>
<directory>src/main/webapp/resources</directory>
<excludes>
<exclude>*</exclude>
</excludes>
</resource>
</resources>
I am sure I am missing something. Any help would be deeply appreciated.
You could change warSourceDirectory, see warSourceDirectory in war mojo. For instance:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>${basedir}/src_backup/main/webapp/resources</warSourceDirectory>
</configuration>
</plugin>
See also my answer to rename files, and not use different directories: Renaming static files when building WAR file using Maven

Maven Resources Not Copying Files

I want to use maven for replacing some of my *.properties files in my war file.
Therefore I created folders dev, test and prod in my resources folder. Now I only want to have one of these folders used in the resources folder path in my war file when executing the corresponding profile. The result is that maven is copying all other folders as well so those are double in the classpath. Here is my configuration:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-dev-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- source -->
<directory>src/main/resources/dev</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
How do I configure Maven now that its only copying the contents of folder src/main/resources/dev to target/classes?
Thank you for you help
Maven by default copies all files in "src/main/resources" to the output folder, so in your current configuration it would probably create "dev", "test" and "prod" folders with their contents and then additionally copy the development resources without a "dev" prefix.
I would suggest to leave the default resources folder as it is and use it only for profile-independent resources. In your profile you can then configure additional resource folders:
<profile>
<id>dev</id>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources-dev</directory>
</resource>
</resources>
</build>
</profile>
It should also be possible to configure this in the common build section using properties defined in the profiles:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources-${projectStage}</directory>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<projectStage>dev</projectStage>
</properties>
</profile>
</profiles>
If you for some reason can't change your current directory structure then you would have to configure exclusions for the default resource folder to not copy the nested profile-dependant folders:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>dev/**</exclude>
<exclude>test/**</exclude>
<exclude>prod/**</exclude>
</excludes>
</resource>
</resources>
</build>
The "main" part in the maven directory structure exists for this purpose.
Instead of creating directories inside the resource dir, you should create more dirs in your src directory.
This is the default behavior, and it's already implemented by letting test resources override main resources during the test phases of the maven lifecycle.
Your structure should look like this:
src
main
resources
a.properties
b.properties
c.properties
test
resources
b.properties
c.properties
dev
resources
b.properties
With this structure, your "main" target will have the default property files, the test phases would temporarily overwrite b and c, and the dev profile will only overwrite b with yet another custom version, in the final deployable.
In order to achieve this, you'd have to configure the maven-resources-plugin the way you already do in our question, for a dev profile.
And when you do, also consider: what version of property files should you use when running unit tests while building using the dev profile?
I was using the assembly.xml file in maven and used this set of code to make sure that my resources file was at the root directory of the app...
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>distribution</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>bin/**</include>
<include>webapps/**</include>
<include>data/**</include>
</includes>
</fileSet>
<fileSet>
<directory>${basedir}/target</directory>
<includes>
<include>*.jar</include>
</includes>
<outputDirectory>lib</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/</directory>
<includes>
<include>resources/**</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>
Don't know if this is the best way, but it was one I could visualize.
Adding to the earlier answers, you can also copy file from custom path to specific directory with maven copy-resources.(though its not suggested, ideally the)
<configuration>
<!-- Destination target path -->
<outputDirectory>${basedir}/target/classes/com/db/abc/util/</outputDirectory>
<resources>
<resource>
<!-- source file path -->
<directory>C:/Users/abc/Work/filepath</directory>
</resource>
</resources>
</configuration>

Maven 2 Weblogic.xml replacement

I am having a problem about placing weblogic.xml under WEB-INF folder after mvn install. My weblogic.xml file is under src/main/resources/weblogic.xml and I want it to be placed under WEB-INF after install.(packaging is "war" by the way)
I tried this:
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>../resources</targetPath>
<excludes><exclude>web.xml</exclude><exclude>weblogic.xml</exclude></excludes>
</resource>
<resource>
<directory>src/main/resources/config</directory>
<targetPath>..</targetPath>
<includes><include>weblogic.xml</include></includes>
</resource>
</resources>
It is working with install but when I want a classpath using eclipse:eclipse, It gives the error :
Description Resource Path Location Type Cannot nest output folder 'ResponseManager/target/WEB-INF/resources' inside output folder
'ResponseManager/target/WEB-INF' ResponseManager Build path Build
Path Problem
because of this conf in classpath:
<classpathentry kind="src" path="src/main/resources" output="target/WEB-INF/resources" excluding="web.xml|weblogic.xml|**/*.java"/>
<classpathentry kind="src" path="src/main/resources/config" output="target/WEB-INF" including="weblogic.xml" excluding="**/*.java"/>
Any ideas?
Normally files under src/main/resources get packaged along the compiled class files, in a webapp they would be placed in WEB-INF/classes. Is there any reason why you can't put these under the standard path src/main/webapp?
If you need to package additional files which are not in the src/main/webapp folder then it would be better to configure these resources in the war plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/config</directory>
<targetPath>WEB-INF</targetPath>
<includes>
<include>weblogic.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
It should be possible to specify the targetPath as above but I think it would be cleaner to reproduce the wanted directory structure inside your source folder.

Using maven using non standard directory layout

I am trying to apply maven to an existing project which already has a directory structure in place. All I can find from previous question is the following.
Maven directory structure
However, my requirement is more detailed. Please see below for the directory structure:
<root dir>
|
+--src-java
|
+--src-properties
|
+--WEB-INF
I know we could have something like
<build>
<sourceDirectory>src-java</sourceDirectory>
...
</build>
But sourceDirectory is for JAVA source code only, if I'm not mistaken.
For the above structure, how do I declare it in pom.xml? Moving the directory is my last option right now.
I guess you need to have something similar to below.
Seeing WEB-INF, I assume you want to build a war. Maven war plugin does this. You will need to configure this a bit since the folder structure is non-standard - for instance you may need to specify the location of web.xml using webXml property. These are documented in the usage page.
<build>
<sourceDirectory>src-java</sourceDirectory>
...
<resources>
<resource>
<directory>src-properties</directory>
</resource>
</resources>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>WEB-INF</warSourceDirectory>
...
</configuration>
</plugin>
</plugins>
</build>
You can change the default directory structure declared in the Super POM by overwriting them in your pom.
For your example, e.g.
<sourceDirectory>src-java</sourceDirectory>
<resources>
<resource>
<directory>src-properties</directory>
</resource>
</resources>
Maven will copy all resources to the jar file. If you want to include WEB-INF to the jar it would be best to move it into the specified resource directory. Otherwise you have to copy it by your own (with maven plugins) to the target directory - I suppose.
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
From here.

How do I exclude files/directories from the target directory using the maven war plugin?

I followed the instructions here, and it worked but only if it was mapped to a directory other than the default.
Here's a sample that I tried:
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<!-- the below works when uncommented and does exclude *.tmp -->
<!-- <targetPath>test/WEB-INF</targetPath> -->
<!-- the below does not -->
<targetPath>WEB-INF</targetPath>
<excludes>
<exclude>*.tmp</exclude>
</excludes>
</resource>
</webResources>
</configuration>
So my assumptions is that I have something else overriding the configuration. However, this is my first project using maven, so I'm not sure what to test or investigate next.
change your exclusion to
**/*.tmp
You could also leave off the WEB-INF from the directory and remove the targetDirectory altogether. For instance, here's one that would include all xml, xhtml, x*ml etc. and excludes all *.tmp in any directory
<webResources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<includes>
<include>**/*.x*ml</include>
</includes>
<excludes>
<exclude>**/*.tmp</exclude>
</excludes>
</resource>
</webResources>