Maven Resource Filtering Breaking Downloads - maven

I have a Maven/Java application. Part of the application allows you to download some documents. Here is the project setup.
+src
+main
+resources
+downloads
MyDocument.docx
jdbc.properties
pom.xml
The downloads work correctly when jdbc.properties has the hardcoded values inside them. However, I am trying to update the application to use Maven profiles and specify the different database connections for the different environments. I manage to get it to work with the following in pom.xml.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
However, even though the jdbc.properties file gets correctly populated with the correct environment database information, the download feature stopped working. The file will be downloaded, but when you try to open it, it says The file MyDocument.docx cannot be opened because there are problems with the contents..
I tried changing the <directory> to src/main/resources/*.properties and adding an additional <resource> where I turn the <filtering> to false for src/main/resources/downloads. But neither approach worked. How can I prevent the Maven filtering from corrupting the files?
FYI - I looked inside the WAR and the documents cannot be opened from there either (they are already corrupt).

Update: Better solution from https://stackoverflow.com/a/10025058/516167
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
You should exclude files like MyDocument.docx (*.docs) from filtering.
<build>
<resources>
<resource>
<directory>src/main/resouces</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.docx</exclude>
</excludes>
</resource>
</resources>
</build>
or define alternate directory for downloads like:
<build>
<resources>
<resource>
<directory>src/main/downloads</directory>
</resource>
</resources>
</build>

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.

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

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.

Maven resources plugin exclude not working

below is my implementation of the resources plugin in my maven build.
according to http://maven.apache.org/plugins/maven-resources-plugin/index.html this config should be good but it seems to be ignoring the excludes stanza. I only want properties files to be copied and nothing else, but when I run the build I get all the xml, xsl, and any other file that is in the resources directory. am I missing something here or is this a bug with the resources plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${user.home}/cms/conf</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</plugin>
So it was a couple different things causing this. first maven version: the 2.0.2 version fails if the execution stanzas are added to the resources plugin, so I removed it. then I ran into another problem related to the version so I upgraded the version to 3.0.3 and that is when I started getting the files copied over but the excludes was getting ignored. at this point I had forgotten about the execution stanzas so now a few hours later I was re-reading the documentation and saw them and added them again. and all is right with the world

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