maven pom's resource element - maven

i am new to maven and came across the resources section. The reference for the maven project descriptor (version 4.0) states that "this element describes all of the classpath resources associated with a project or unit tests" (link). However this description i do not understand/seems to abstract for me. Why would i want to describe classpath resources? How does it affect the projects lifecycle?
matthias

usually you have the src/main/resources folder which contains resources which will be packaged into a jar. This everything which is in the above folder will automatically be packaged into a jar and is accessible via getResource... Via the resources area in the pom you can filter files from their way src/main/resources to the target/classes folder.
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
...
</resources>
...
</build>
...
</project>
The lifecycle of Maven is not influenced by this.

This feature allows you, for example, to use Maven variables inside your .properties files. Just declare the directory, where your .properties are, in <resources> section, and you'll be able to use ${project.name} or whatever.
pom.xml:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
version.properties:
version=${pom.version}
timestamp=${timestamp}
(${timestamp} is created with Maven Build Number plugin).

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> ```

How to include resources in jar

I need to include contents of resources folder into my jar. I know to include from src/main/resources folder. But my structure is as follow
enter image description here
Any help will be appreciated.
You can change the folders for your resources:
<build>
...
<resources>
<resource>
<directory>resources</directory>
</resource>
<resource>
<directory>other/optional/resource/path</directory>
</resource>
</resources>
...
</build>
It's best to stick with the src/main/resources convention but if you insist you can use maven-antrun-plugin and create a task to copy the files into your target/classes folder.

Why spring only picks from src/main/resources folder to add to classpath?

Why spring only picks from src/main/resources folder to add to classpath ?
That is the default folder for spring spring maven project config.
If you want to change it you can see the example here:
http://www.mkyong.com/maven/how-to-change-maven-resources-folder-location/
Otherwise for Annotated version you can view the Web Tutorial on the Spring.IO website:
http://spring.io/guides/tutorials/web/
Maven uses default classpath for the resource folder which is "src/main/resources"
if you want to include your own custom folder to class path, you have to update pom.xml file to indicate maven to include your new folder into classpath. use following code template to update pom.xml
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
.....
<resource>
<directory>path to your folder to put in classpath</directory>
</resource>
</resources>
</build>
if your pom.xml file doesn't have build or resource tag already then just add only add following piece of code to pom.xml
<build>
</resources>
<resource>
<directory>path to your folder to put in classpath</directory>
</resource>
</resources>
</build>
and check your Effective POM to verify that your custom folder and resource folder both are present there or not.

Include the POM version in Doxygen?

I use Doxygen to document my project. My project uses Maven and thus contains a POM file with a version.
Is it possible to reuse this version number as PROJECT_NUMBER in Doxygen?
Maven has the ability to filter files and replace Maven properties with actual values. If your doxygen configuration file is in your src/main/resources/... path, you can simply add:
<project>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
Then you can reference ${project.version} in your doxygen file and it will be replaced with the version number.

Maven: how to place resource file together with jar?

I have \src\main\resources\logback.xml file. When I run mvn package it is placed into the jar by default. How do I make Maven place it next to jar, not inside?
So, well, I just want Maven to copy the resource to the same folder where jar will be.
No plugin definitions needed, just edit the build resources:
<build>
<resources>
<!-- regular resource processsing for everything except logback.xml -->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>logback.xml</exclude>
</excludes>
</resource>
<!-- resource processsing with a different output directory
for logback.xml -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>logback.xml</include>
</includes>
<!-- relative to target/classes
i.e. ${project.build.outputDirectory} -->
<targetPath>..</targetPath>
</resource>
</resources>
</build>
Reference:
process-resources
(Maven Book)
<build><resources> (POM
Reference)
You can build a ZIP file containing both using the Maven Assembly plugin.
You can also use Maven Antrun plugin or similar to put whatever file you want but the idea of single artifact per project is built deep into Maven internals so there is a chance it will hunt you down elsewhere.
Exclude that file:
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
And then use the Maven Antrun plugin to copy the file.
However, the latter part does not make much sense. If it is a configuration file to put in a server, simply manually copy it.

Resources