Eclipse - maven warning when using parent project resources folder - java

I have parent ROOT maven project, it holds resources folder which every child project use using the following lines in pom.xml:
<build>
<resources>
<resource>
<directory>../ROOT/resources</directory>
</resource>
</resources>
But it produce a warning
Description Resource Path Location Type
Access "..\ROOT\resources" directory outside of project base directory. (org.apache.maven.plugins:maven-resources-plugin:2.6:resources:default-resources:process-resources) pom.xml /Proudct line 1 Maven Build Participant Problem
How to avoid such warning? is my setup wrong for sharing resources in child projects? or is it a false positive warning?

The best solution is to make a separate module which contains only the resources folder. All other modules which need the resources just define a dependency to that new module. The result is that the new module is on the classpath and the resources are accessible.

Related

Include files in Maven JAR package

I have a maven project with the following structure:
project
-src
--main
---java
----(All my .java files are here in their packages)
---resource
----(All my resources are here)
-database (HSQLDB files)
--db.script
--db.properties
--db.data
-target
--Maven build directory
pom.xml
I want the maven jar plugin to package in the database folder when it builds a JAR file. I've tried including as outlined here: https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html, but it doesn't find the directory "../database" (or ../../database OR ../../../database!), and also it now doesn't include any of the classes or resources (It was including them before).
So what should I have in the configuration of the maven jar plugin?
Which is the correct path to include and how do I keep the files it was including before?
Thanks
By default, only resources located in src/main/resources are added in the JAR.
What you tried to do by configuring the maven-jar-plugin defines only the filetset to exclude or include in the built JAR.
But these files still have to be located in a resources folder where Maven looks for resources.
So, you have two ways to solve your requirement:
move database in the standard directory : src/main/resources.
specifies two resource folders for resources :
To do the latter, in the pom.xml add in the build tag :
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>database-resources</directory>
</resource>
</resources>
Then add the database folder that you want package inside the database-resources.

exclude file from maven's resource directory

I am using maven for my spring boot application(1.5 version). There are some files in src/main/resources like abc.properties, app.json. Below are some pointer what i want to achieve.
Exclude these files getting into the jar.
When i run my application through intellij these files should be available in classpath.
I looked at related answers on SO but none matches my case. Any suggestion?
you can use the resouce tag in maven pom file:
<resources>
<resource>
<directory>[your directory]</directory>
<excludes>
<exclude>[non-resource file #1]</exclude>
<exclude>[non-resource file #2]</exclude>
<exclude>[non-resource file #3]</exclude>
...
<exclude>[non-resource file #n]</exclude>
</excludes>
</resource>
...
</resources>
For more informations see: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
My understanding is
You want some config file, that is available in classpath during runtime
Such config file will be changed based on environment
The way I used to do is:
Create a separate directory for such kind of resources, e.g. src/main/appconfig/
Do NOT include this in POM's resources (i.e. they are not included in resulting JAR)
In IDE, add this directory manually as source folder (I usually put this in testResource in POM. When I use Eclipse + M2E, testResources will be added as source folder. Not sure how IntelliJ though)
For point 2, I used to do it slightly differently. Instead of excluding, I will include in the result JAR but in a separate directory (e.g. appconfig-template), so that people using the application can take this as reference to configure their environment.
An alternative of 3 is: create a separate profile which include appconfig as resource. Use this profile only for your IDE setup but not building your artifact.

How to add an external folder to the class path?

So I have the following folder structure:
Project
lib
(running jar from this folder)
properties (property file to load is in this folder)
I am trying to load a property file via X.class.getClassLoader().getResource("properties/fileName"). This method works in eclipse but when I build the jar using maven it fails to find the file, giving a file not found exception.
I suspect the folder is not in the classpath because if I run getClassLoader().getResources("") the property folder never shows up. I tried all the suggestions in previous questions on stackoverflow but none have worked so far.
I also tried running java -cp and -classpath but it still failed.
When using Maven, files like *.properties and any other not-compilable files must lie at src/main/resources folder, by default, to be available.
Additionally, I would recommend you to use Thread.currentThread().getContextClassLoader() to get a proper classloader, in order to load resources.
Anyway, if you want to have your custom folder at classpath, I suggest you to add as a resource, at the pom.xml, like this:
<project>
...
<build>
...
<resources>
<resource>
<!-- The folder you want to be a resource (from project root folder), like: project/properties -->
<directory>properties</directory>
<!-- Filtering if Maven should post-process text files to find and replace ${key} params: in most cases, leave it false -->
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>

How to make maven put resource files of a dependancy in parent war and not dependancy jar

I have two projects A and B.
B is dependant on A.
A has some resource files.
When using mvn install on B, it uses the resources that were packaged in A.jar (as that was built previously)
I would like the resources to be packaged in B so I could edit them easily. (Especially since B is deployed as WAR)
Copying the resources to project B might not help since the code in A uses the following to retrieve info
InputStream stream = this.getClass().getResourceAsStream(
"/resourceFile.properties");
do I need to change the path to relate to project B? if so how should one do it?
What is the best approach for handling this?
Easiest way I've found
it to add this to your B project POM
<build>
....
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>../A/src/main/resources</directory>
</resource>
</resources>
...
</build>
You could use the maven-shade-plugin's shade mojo to repackage select artifacts from your project A into B. I haven't entirely looked at the jarjar-maven-plugin, but it could also assist you in doing that.
Look at the "Relocating classes" section of the shade plugin documentation in particular to understand how to do it. It should work equally well with resource files in theory.
Your code will work as it always does - it does not matter whether the resource being loaded is in the same jar or in a different jar in the B's classpath or inside B's WEB-INF/lib as long as it's in the classpath and referred by the right path in your code.
It's good that you didn't consider moving the property file into B project's codebase - had you done that, A would no longer be self-contained.
As long as project B is in the same classpath as project A, that code will continue to work.
It may be clearer, however, to use ClassLoader.getResoucreAsStream rather than Class.getResourceAsStream. The method on Class by default looks for resources relative to the package name of the Class. You have provided an absolute path (leading '/'), indicating that the package relative logic is not needed.

Add generated build file to classpath

During my build I generate a build.properties files via the maven properties plugin (properties-maven-plugin) containing build information.
What's the best way to have this file included in the generated jar? I don't want to put it into the src/main/resources directory as this would pollute my default resource directory.
Is there not a "generated-resources" directory as there is with source?
I thought there was a default generated-resources directory, but I can't find any documentation on that at the moment. You can always configure additional resource directories in your pom:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
</resources>
...
</build>
Place generated sources in target/generated-sources
There is a plugin called build-helper that allows you to add that folder to the source-folders list.
You can use maven assembly plugin to organize files and filesets in packages. have a look at http://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html
I think it is what you need.
you can output your files to any directory and add that resource directory to your <resources> of your <build>
You should put it in the target/classes directory
(fixed now from just target) and I think that this is better than the accepted one. As there is no need to process this resource as resource anymore

Categories