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.
Related
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.
I can see many posts regarding environment specific properties, but none of them is quite solving my problem.
I'm working on my spring boot app, which will have say two profiles dev/live.
I want my project to have the following structure:
--deployment
--dev
--myapp.properties
--live
--myapp.properties
--src
--main
--java
--resources
--test
--java
I know that if I had two application.properties in my resources folder, say application-live.properties and application-dev.properties I'd be able to tell maven which one to pick at application start time using:
Dspring.profiles.active=profile_name
But I don't like name of this generic properties file. So I found this way to tell the application where to look for profile specific properties in /deployment/<profile>/.
I did it using Maven build task configuration:
<build>
<resources>
<resource>
<directory>deployment/${environment}</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
</build>
This is going to search for resources in deployment/<profile> and include them. Good thing is that Maven finds the correct environmental property files, but unfortunately it includes them in target/classes/myapp.properties which seems invisible for Spring Boot? How can I tell Maven to place it in the same directory as application.propertiesor alternately how can I tell Spring Boot to find it where it is at the moment?
Thanks a lot!
How can I tell Maven to place it in the same directory as application.propertiesor alternately how can I tell Spring Boot to find it where it is at the moment?
1) you don't need to modify Maven settings because it places config file to the right place: target/classes is the root of the classpath, because all files from this directory will copied into the WEB-INF/classes directory of you WAR/JAR file.
2) to change name of the properties file from you can use spring.config.name property by explicitly passing it as -Dspring.config.name=myapp or as environment variable SPRING_CONFIG_NAME=myapp. More information you can find in the Change the location of external properties of an application chapter of the official documentation.
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>
This is from an open source project where there is nearly no support. Compilation of the project went well. But I cannot test or install because of a particular property file cannot be found. I have search up and down the internet and not able to find any solutions. The organization of the director is a little bit unconventional.
some directories.
core/src/main/java/org/mskcc/cbio/portal/util/Config.java.
String props = "portal.properties";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(props);
...
more directoires.
src/main/resources/portal.properties.
This last src directory has only one subdirectory main,
and the main directory has only one resources subdirectory.
The file that could not be found is located here.
I am using java 1.7. Maven 3.0.5
The command I used is mvn install
I must be missing one simple configuration somewhere.
There got to be some who can resolve this easily.
src/main/resources is, by default, the path of Aplication/Library resources in Maven. But, you can optionally force this in the pom.xml.
1) Add in the pom.xml
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
2) run maven -> mvn clean install
3) Search for portal.properties at the build generated at "target" folder. Assert that portal.properties is in the root of the artifact classpath (root of .jar, .ear or /classes in .war).
Imagine I have a project I want to build using maven. My project doesn't, unfortunately, respect the maven default layout. So I'm having two source folders A & B containing .properties files with .java sources.
After an mvn install, my .properties files are not packaged in the jar with my .class generated files. Is there a way to do this automatically, you'd probably propose to use <resources> tag to solve this kind of problems; this obviously works I know, but i'll have to each time specify where my .properties files are stored, and where I want them to be packaged in the JAR, this is not a solution for me since I have multiple source folders (hundreds) regularly updated.
<resource>
<targetPath>com\vermeg\jar2</targetPath>
<filtering>true</filtering>
<directory>${basedir}/a/jar2</directory>
<includes>
<include>*.properties</include>
</includes>
<excludes>
<exclude>*.java</exclude>
</excludes>
</resource>
Will I have, for each source folder, to write this in my POM, does anyone knows a simpler automatic way to do this ?
Regards,
Generally you can use Maven properties and profiles to solve this kind of problem.
For example edit your pom to:
<resource>
<targetPath>${myresources.targetpath}</targetPath>
<directory>${myresources.directory}</directory>
...
</resource>
and define myresources.directory and myresources.targetpath in command line using -Dname=value or in conditional profile or by using other properties available in your build.
If you explain your project structure and/or conditions and their relation to your variable (targetPath and directory) I may be able to help more with your question.
Specify it once in a parent pom instead, and all the children should inherit the same setup...
Would it be impossible to change to using the standard Maven layout, with all your properties in a parallel 'package' hierarchy under src/main/resources? Then you don't need to specify anything; all .properties files will be packaged.
If you do this, you'll need to enable filtering as it's off by default. You may have to explicitly declare the resource directories again, as when you declare them, this seems to override the ones you get 'for free'.
As for your multiple source folders, a multi-module Maven project would probably be the best fit, with A and B being children of some new parent project.
This might seem like quite a lot of work, but Maven's conventions are fairly reasonable, and it tends to be a painful experience if you go against them.