Maven: how to place resource file together with jar? - maven

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.

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

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.

Maven: include folder in resource folder in the war build

I've a folder named extra-jars in the src/main/rescource, but how can I include these in the build? I want them to be put in the lib folder with the rest of the jars. I tried including them through , but that didnt work.
For jars that are not distributed by a Maven repository, the simplest way is place the extra jars in the src/main/webapp/WEB-INF/lib directory of your project. Maven will by convention, include everything under the src/main/webapp in the final war artifact.
An additional method is to use the Maven War Plugin. It has the ability to add additional files to the final war artifact though plugin configuration.
In the build section of the POM add something like the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resource/extra-jars</directory>
<includes>
<include>*.jar</include>
</includes>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The <configuration> section is the key to adding additional files.
The <directory> element defines the source location of the resource. The path is relative to pom.xml.
The <includes> element defines what files found in the above directory to include.
The <targetPath> element defines the destination directory in the WAR to which the files are copied.
These jars should be added as Maven dependencies, not by copying them into the lib folder. This is the sort of thing Maven is designed for.

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 pom's resource element

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).

Resources