Maven : Excluding src/main/webapp/resources from the WAR is not working - maven-war-plugin

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

Related

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.

Exclude property files from resources

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

Is there any way to tell maven war plugin to use folder other than target/classes

when i use maven war plugin, by default, this plug-in will copy all class files(*.class) from target/classes to {warfile}/web-inf/classes.
The problem is if i have compiled classes (*.class) that stay in another folder : basedir/other-classes (they are *.class file not *.java file, i know, it is weird. But those classes is generated from 3rd party).
Is there any way to tell maven war plugin to copy all classes in (basedir/other-classes) and (target/classes) into {warfile}/web-inf/classes
This might work for you. Make sure the directory and targetPath are what you need.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/other-classes</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>

How to add a file to a war with Maven

I have developed a maven plugin that downloads the release notes from JIRA.
It's bound by default to the 'generate-sources' phase and creates a 'release.txt' file in the build folder (${project.build.directory}).
My question: how can I add this file in the 'WEB-INF' folder of the war file built by Maven ?
I know I can use the 'maven-war-plugin' to include additional external resources from the 'src' folder, but I don't want my 'release.txt' file generated there (=not commitable to svn).
Thanks for your help. I wish you a nice day!
Maxence
I think this can be done using this feature of that plugin:
Adding and Filtering External Web Resources:
http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
Which would allow you to generate your release.txt into a separate folder (not src) and have the plugin treat it as an extra resources folder.
Hope that helps.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}</directory>
<targetPath>WEB-INF</targetPath> <!-- introduced in plugin v 2.1 -->
<includes>
<include>release.txt</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>