maven clean install lost resource folder - maven

I was trying to use maven to build my project
when I create a source folder src/main/resource and put a file into that folder.
But when I do "mvn clean install eclipse:clean eclipse:eclipse", src/main/resource folder gone, it appears to the src folder instead
The problem of this new structure for me is that I have to manually create a src/main/resource folder again to make sure that those files in the resource folder could be deployed into classpath when I run it. I wonder if there is a way to let maven do it automatically for me? Thanks

You can specify the resources directory in pom.xml. Following is the example:
<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<finalName>${artifactId}-${version}</finalName>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>
reference: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

Related

Maven shade plugin is not creating "src/main/resources" directory,but it is placing the files directly under root

I am building uber jar with Maven shade plugin. But the jar has config/properties files directly in root instead of under "src/main/resources". Any help is appreciated on how to troubleshoot it ?
Tried by adding the below section in pom, but no luck
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources> ```

.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 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 resources plugin flat copy of resources

Given a folder 'database' containing JAR-connectors for different RDBMS. Each JAR is located in its own folder:
+---database
+---db2
+---db2.jar
+---derby
+---derby.jar
+---h2
+---h2.jar
+---mysql
+---mysql.jar
I need all of those JAR-files to be copied into WEB-INF\lib.
Here's my pom.xml:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>../target/${project.artifactId}/classes/database</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
Problem is, that those JARs are copied with their directories:
+---WEB-INF/lib
+---db2
+---db2.jar
+---derby
+---derby.jar
+---h2
+---h2.jar
+---mysql
+---mysql.jar
This is how it should be:
+---WEB-INF/lib
+---db2.jar
+---derby.jar
+---h2.jar
+---mysql.jar
I have 20 connectors and I don't want to hard code them.
The most correct way of doing so will be to install your jar files into Maven repository and then use maven-dependency-plugin:copy goal. Or if you want to solve this roughly then use maven-antrun-plugin (copy rule).
You're having problems because you're trying to bend maven into something it's not supposed to do. Binary artifacts should be deployed into your artifacts repository (or local maven repository) and not included into your project. This way having them defined as dependencies in pom would ensure that they're copied into your WEB-INF/lib.

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