How to exclude certain resources from the maven war plugin war file? - maven

We've been using war:exploded for a while now and I'm trying to do away with it. I've added the following to my pom:
<resource>
<directory>src/main/webapp</directory>
</resource>
This copies files such as the in src/main/webapp/keystore into target/classes/keystore so my local UI launcher works and sees everything. Score!
However, by adding to the resources list, this means that the same files also show up in the war file as keystore (correct) and WEB-INF/classes/keystore (wrong). It also means there there is a WEB-INF/classes/WEB-INF directory (blah). I'm trying to exclude the resource files from src/main/webapp resource since src/main/webapp/WEB-INF is already a resource.
I'm trying not to specifically exclude keystore and other files since we add/delete from that list semi-often. I've tried to add the following (and a number of other variants) to the war plugin configuration without results:
<webResources>
<resource>
<directory>src/main/webapp</directory>
<excludes>
<exclude>**</exclude>
</excludes>
</resource>
</webResources>
I've also read a number of other SO questions and I've spent at least an hour reading docs on from the maven war plugin page and tried other configs without success.
Any idea what magic I need to do here? Thanks in advance.

You need
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<packagingExcludes>**/keystore</packagingExcludes>
</configuration>
</plugin>
Also See
Including and Excluding Files From the WAR

warSourceExcludes worked for me. for example.To exclude js folder and scss folder from my war i tried...
<configuration>
<warSourceExcludes>js/*.*,scss/*.*</warSourceExcludes>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
<includes>
<include>WEB-INF/web.xml</include>
</includes>
</resource>
</webResources>
</configuration>

However, by adding to the resources list, this means that the same files also show up in the war file as keystore (correct) and WEB-INF/classes/keystore (wrong).
I was not able to get the war-plugin to properly exclude the webapp directory while at the same time allowing the webapp/WEB-INF directory. Instead of adding the following to the pom:
<!-- bad idea, this didn't work -->
<resource>
<directory>src/main/webapp</directory>
</resource>
I switched to using the resources-plugin to be able to copy certain resources over during the validate phase:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/webapp</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I initially copied the webapp directory into target/classes but that also seemed to make it into the resulting war file which was what I was trying to avoid. I have the feeling that I could have solved this if necessary.
Instead I decided to copy the webapp files into the target/webapp directory and change my program to look for the resources there:
String RESOURCE_BASE = "target/webapp";
System.setProperty("server.jetty.webapp.resourceBase", RESOURCE_BASE + "/");
System.setProperty("server.jetty.ssl.keystore", RESOURCE_BASE + "/keystore");
System.setProperty("server.jetty.ssl.truststore", RESOURCE_BASE + "/keystore");
...
This seems to be working well. Testing locally now works (without war-exploded!!) and the files are not doubly included in the resulting war file. In addition, I added a symlink at runtime from target/webapp/views/cms to point to our CMS hierarchy which is in another project.

Related

.txt file not included in the war in a Spring MVC 3.0.5.RELEASE [duplicate]

I have a pom.xml file with the following:
<sourceDirectory>${basedir}/src/main/test</sourceDirectory>
<outputDirectory>${basedir}/src/main/bin </outputDirectory>
Inside ${basedir}/src/main/test I have some folders which do not contain any .java files. When I start a compilation they are not copied to ${basedir}/src/main/bin directory.
Only .java files are moved (after compilation of course) and stored on the right folder.
Can someone help me to solve this problem without using any plugin ?
I tried with
<resources>
<resource>
<filtering>false</filtering>
<directory>${basedir}/src/main/test/scenarios</directory>
<includes>
<include>*.xml</include>
</includes>
<targetPath>${basedir}/src/main/bin/scenarios</targetPath>
</resource>
<resource>
<filtering>false</filtering>
<directory>${basedir}/src/main/test/sut</directory>
<includes>
<include>*.xml</include>
</includes>
<targetPath>${basedir}/src/main/bin/sut</targetPath>
</resource>
</resources>
But it does not work. What is wrong?
If they are not java files you should move them to the src/main/resources and/or src/test/resources directory. That's the maven convention for storing the non java files.
Your other option is to use the maven-resources-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/test</directory>
<includes>
<include> <the_files_you_want_to_include></include>
</includes>
</resource>
</resources>
You have another option is to use the maven-antrun-plugin and execute an ant task to copy the files manually.
You can add src/main/test as a resource directory using the maven resources plugin. see http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html for basic information and http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html for information on only including certain files (since you won't want to copy your .java files)

Maven: Filtering persistence.xml

In our Peoject, the persistence.xml is located in the relative directory
/src/main/resources/META-INF,
and without giving any additional instructions in the pom in the file is copied to
/WEB-INF/classes/META-INF
during the Maven build process. This is fine so far as hibernate is able to pick the configuration file from there. But now we wanted to centralize some configuration values and therefore use the maven-war-plugin to replace the respictive wildcards in the persistence.xml
The filtering itself is performed for various other files and working as expected. For the persistence.xml however it seems to fail for various reasons:
When just adding the persistence.xml to the webResources:
<resource>
<directory>${basedir}/src/main/resources/META-INF/</directory>
<filtering>true</filtering>
<includes>
<include>persistence.xml</include>
</includes>
</resource>
The file is filtered properly, but ofc. thrown into the root of the outputdirectory, where hibernate is not looking for it.
When trying to provide the desired target-folder:
<resource>
<directory>${basedir}/src/main/resources/META-INF/</directory>
<filtering>true</filtering>
<includes>
<include>persistence.xml</include>
</includes>
<targetPath>WEB-INF/classes/META-INF</targetPath>
</resource>
the file is not filtered properly. I assume that filtering would actually work, but since the file has already been copied there, maven refuses to override the file still containing wildcards
Any ideas how to make this work without reinventing the wheel?
Here you have a snippet of XML code that works for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/META-INF</directory>
<includes>
<include>**/persistence.xml</include>
</includes>
<targetPath>WEB-INF/classes/META-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
I figured out the solution. Just posting, in case anybody stumbles over the same issue:
The filtering listed above was wrapped in <webResources> tags, cause it's actually about processing the webapp-content.
So, in a nutshell I told maven to deploy every resource without touching it, and when deploying webresources try to process a resource and deploy it to a resource-folder - which already has happened unprocessed.

Why does maven create two copies of my filtered persistence.xml?

Below is the relevant section of the maven project I'm working on. I would like Maven to filter the persistence-context.xml file inside of the WEB-INF directory, and then place it into the WEB-INF directory inside of my war. When I run mvn clean package on this project I see the following two things.
The successfully filtered persistence-context.xml in the target/projectname/ folder. This is NOT the correct place. I want it one directory up in the WEB-INF.
The unfiltered persistence-context.xml in the target/projectname/WEB-INF/ folder. This is NOT what I want. I want the filtered one here.
I'm not even sure how two copies of this file are being generated! Any help would be much appreciated.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<includes>
<include>persistence-context.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
I realized I had another plugin entry for the war plugin which was interfering (possibly overwriting?) with this one. Once I removed that my above entry worked okay!

Configure maven-shade-plugin to include src/main/webapp

I'm working with a simple project, with the webapp directory in the standard location (src/main/webapp). For some reason, it looks like the maven-shade-plugin doesn't include that in the final generated jar - it will only include artifacts src/main/resources by default.
I have tried to use the following configuration, with no luck:
<configuration>
<artifactSet>
<includes>
<include>*</include>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
Any suggestions on how to do that without having to move src/main/webapp into src/main/resources/webapp?
Thank you.
Eugen.
Have you tried to update your build section with your resource path ?
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</build>
EDIT
As already said, shade purpose is not to war packages.
You need to put it in src/main/resources/webapp or specify the appropriate location explicitly for it to be caught up properly. Annoying, but it works.
For those asking why this kind of thing makes sense to do, it's a typical setup for a jar with an embedded web server like Jetty.
I use the resource plugin to move the webapp to the classes folder.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/webapp</outputDirectory>
<resources>
<resource>
<directory>src/webapp/dist</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This way I can place the folder with my webapp anywhere, define the destination directory and it is included in my shaded jar.
It sounds you would like to package sources into a package. There exists a maven-source-plugin which will create a jar which contain the source files. Furthermore this is usually done during a release cycle. As already mentioned the maven-share-plugin intention is to package jar files together (their contents; keyword: ueberjar).
I would use the war plugin http://maven.apache.org/plugins/maven-war-plugin/faq.html#attached to create a jar of a webapp project. You have already set <packaging>war</packaging>?
What's the point in adding your webapp resources into a JAR? That's WAR-like.
Change your packaking type to war and it will work.

Maven YUI Compressor Plugin Causing process-resources phase to not run?

I have a maven project that builds a war file.
Including the yui compressor in my maven build file is causing files, found in src/main/resources/ unrelated to any js files, processed during the process-resources to be empty when they are copied into the target directory. Very weird, indeed. Once the yuicompressor plugin is removed from the cycle, the other resources are processed just fine.
Anyone ever seen that (please, say yes ;-) )?
Here's my config:
The YUI Compressor config:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
<phase>process-resources</phase>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/extjs*/**/*.js</exclude>
<exclude>**/extjs*/**/*.css</exclude>
</excludes>
<nosuffix>true</nosuffix>
</configuration>
</plugin>
And the Resources config, containing the files that are empty when copied into the target dir:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
To repeat: the files (log4j.xml, etc) in the resources directory are being copied over to the target directory, but they are empty.
Thanks for your help!
What is happening is the YUI Compressor plugin has the resources directory as one of the locations to include for compressing implicitly. It was being run after the resources plugin executed, overwriting the xml and .properties files in the resources dir with empty files (because xml and .properties files don't contain javascript). My fix was to add new excludes to the plugin's config:
<excludes>
<exclude>**/*.xml</exclude> <!-- <-- this one here -->
<exclude>**/*.properties</exclude> <!-- <-- and this one -->
<exclude>**/extjs*/**/*.js</exclude>
<exclude>**/extjs*/**/*.css</exclude>
</excludes>
This is still less than ideal, however, because any resources without an xml or .properties suffix will still be parsed by the yui compressor; I'm back to the original problem.
I tried this exclude, but it didn't work:
<exclude>**/resources/*.*</exclude>
does anyone have any idea why the above wouldn't work, or have an idea how to tell the yui plugin not to process anything in resources?

Resources