Where to put property file in config folder of tomcat - java

I am using a spring boot project where I have two module Search and Web. I build war from web module and search module in injected as jar. I have a property file index.properties in search module, So every time I make a jar it goes into search jar, But I want to remove this jar from Search module and want to externalise it to config folder of tomcat. I am using sprinboot project, I don't know how to do this.
System.setProperty("spring.config.name", "search_index");
Does doing this will work ?

If I understand your question correctly
1) You would like to externalized .properties file
2) And you don't want to include .properties in jar file
To answer the first issue there are multiple ways to externalized props file
Try - spring.config.location
Either set this value from your main method or - Inside Tomcat put following value in setenv.sh
Dspring.config.location=/filelocation/application.properties
Reference - https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
To solve the second problem try using exclusion tag in maven config.
like -
<excludes>
<exclude>*.properties</exclude>
</excludes>
My answer is based on the brief understanding provided by you, Please post all the specifics which will helpful to provide correct solution for the issue.

Related

How to specify a profile when deploying a Spring boot war file to Tomcat?

At the moment, all my properties are defined in the file src/main/resources/application.properties. However, I would like to have properties files relating to different profiles in the src/main/resources/config folder, and I want to be able to choose any of them. such as:
application-DEV.properties
application-TEST.properties
application-SERVER1.properties
So, the question is how to select these properties. If I was compiling to a jar file, I could do that easily by specifiying the profile when running the jar file, but here I just copy the generated war file to a Tomcat webapps directory.
Well, I've found a way to do that. In the conf directory of Tomcat, add this line to the file catalina.properties there.
spring.profiles.active=<YOUR_PROFILE>
Replace <YOUR_PROFILE> here of course with your profile's name. For example if you are using application-TEST.properties, it would be the following.
spring.profiles.active=TEST
You can define Jvm Argument -Dspring.profiles.active=<PROFILE> on server start up file (.bat/.sh) depending on your environment.

Spring Boot classpath

In the Spring Boot's docs here, about serving static content, it says:
By default Spring Boot will serve static content from a directory
called /static (or /public or /resources or /META-INF/resources) in
the classpath.
I found that all the content in the directory:
src/main/resources
will be copied inside the classpath, so I can put my static content in:
src/main/resources/static
and all will work fine and I'm happy since I can have my static content under the src directory.
But, I have some questions about this:
Why the documentation doesn't say to put static content in src/main/resources/static instead of speaking about the classpath (I think this is a bit confusing)?
Is it good to assume that the content in src/main/resources/ will be always copied in the classpath?
Is there some Spring Boot official documentation explaining what I'm supposed to find in the classpath other than Java classes and packages (up to now I only know I can found all the content from src/main/resources/)?
/src/main/resources is a Maven project structure convention. It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .jar, some physical file system location used in the classpath (with java's -cp option), etc.
I could choose to build my application myself or with a different build tool. In such a case, /src/main/resources would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class files.
The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.
The classpath also contains additional libraries (JARs), which also can have a static folder, which would then be included for serving static resources. So if the documentation would only state the folder src/main/resources/static, it would be incomplete.
Ad 2: As long as you don't mess with the default Maven configuration, then it's safe to assume this.
Ad 3: Maybe start with the official Oracle documentation: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html. Hint: Of course, it's not only the contents of the resources folder, which are in the classpath, but also of course all compiled classes, hence its name.
To add to previous answers, it's the Spring Boot Maven Plugin (spring-boot-maven-plugin in the pom.xml) that that enables you to run the application using Maven. One of its functions is to ensure contents in the app including dependency libraries are available on the runtime classpath, such as within the the executable JAR file.

Spring Tool Suite and Spring Configurator - directory structure

I'm wondering what is the best way to organize Spring Configurator directories in my Maven project ? I wan't to have prod,dev and test config sets with seperate *.xml and *.conf files.
And also I'd like to have a seperate master *.xml file that is used in every config set.
Do I have to place *.xml and *.conf files in seperate directories ?
Or could I just places them in the same dir , like so:
spring/
test/application.xml
test/params.conf
dev/application.xml
dev/params.conf
prod/application.xml
prod/params.conf
Thanks for help :)
Place them in a directory structure in a way that no redundancy exist on classpath. Eg: if your code searches for */application.xml file and you have all 3 on the classpath it will cause confusion
Also leverage the use of Maven build profile and assembly plugin. Configure it such that only relevant files are packaged for each build profile.

Spring config xml files management, db connection info

maven module spring application.
1 module is a spring mvc application, another is a non-web but spring managed application.
In my project root, I have:
/src/main/conf
This folder contains my non-web managed spring xml configuration. I added this folder to my class path in intellij.
IntelliJ doesn't pickup the file correctly, meaning I don't get code completion or anything (allot of the names etc. are in bold red i.e. intellij is telling me something is wrong). Can this be fixed somehow?
I have kept my spring mvc config file inside its module (not in the conf folder) because the code completion doesn't work and it's a pain to work with without the IDE helping. But it makes managing things during deployment harder.
In both of my spring config files (for the web app and non-web app), I have my dataSource settings hard-coded in the file, I want to extract this somehow into a properties file, how can I do this?
1 - You can have different spring configuration files all in the same folder, just you need to use different names form them. So, I would use src/main/resources instead to create a new folder in the maven project structure. You will avoid problems.
2- Datasource in both files? why? If you have already two spring configuration files, I would create a third one (application-context-dao.xml) and share the dataSource. How to move properties to a configuration file? See.

Rename to web.xml when packaging using Eclipse

I have a J2EE application which has two web.xml files. One is called web.live.xml another is web.dev.xml. I am building this application with maven using profiles. So maven knows which file to choose when packaging.
I was wondering is it possible to make Eclipse use web.dev.xml when packaging my project and deploying it to Tomcat. This would be very useful because web.dev.xml sets some options which decrease start up time of the application.
You can designate the dev xml to be default (and name it web.xml). You can also have maven move (from a different folder) rather than rename.
You can use the Maven resources plugin (http://maven.apache.org/plugins/maven-resources-plugin/copy-resources-mojo.html) and configure it with a property in the profile and use that property in the configuration of the filename.

Categories

Resources