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

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>

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 Resources and docker-maven-plugin

I would like to understand better how Maven deals with resources and in particular how plugins can reuse the Resources infrastructure that Maven provides:
I have already read the resources section of Maven and looked at the Maven Resources Plugin.
Now I came across the Docker Maven Plugin from Spotify. Which contains the following configuration section:
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
Looking at the Readme of the plugin I could not find any explanation which child elements are allowed for a <resource> element. I am assuming that the <resource> element is in fact related to Maven's org.apache.maven.model.Resource class and that I can just reuse the documentation of Maven to understand who it works. But according to the Maven documentation I linked above, it seems that <include> elements must be nested inside an <includes> element which the above code listing does not do. Now I am confused.
To summarize: Where can I look to get a definitive answer if a plugin does not exactly document how its configuration works. Does there exist some kind of XML schema reference for plugins where I can lookup how particular elements work?
I think you want to set configs like:
<dockerDirectory>src/main/docker</dockerDirectory>
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>/Users/your_user/.docker/machine/machines/default</dockerCertPath>
these one you can use for example by following:
fixes this by:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>yourImageName</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>/Users/your_user/.docker/machine/machines/default</dockerCertPath>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
Important are these two tags:
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>/Users/your_user/.docker/machine/machines/default</dockerCertPath>
I am using a dockerfile, which path you have to define with this tag:
<dockerDirectory>src/main/docker</dockerDirectory>
Now you can build your jar and generate docker image via:
mvn package docker:build

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.

Maven Resource Filtering Breaking Downloads

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>

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