Disable /config sub directory to add them to spring environment (SpringBoot) - java

I have a setup of a Springboot applications with multiple profiles. I am trying to include the setup of profile in jar itself and remove it from the server side. However, after every release Spring creates a sub directory with /config including all my application-*.yml files. How can I disable the default creation of this folder?
I do not require the /config folder as I am using nssm service and providing which profile to use in the argument line.

Related

Using multiple log4j configuration files

I have an application which is deployed as jar and is executed by some other parent application. The parent application has defined a log4j configuration file and the file is mentioned while starting the parent application.
When I am trying to use log4j instance for child application, its configuration file is never picked. How can I use a different configuration file for my application without impacting configuration file of the parent application?

How do I use a given application.properties file when deploying a .war on Apache Tomcat?

I have been tasked to deploy a ROOT.war app on Apache Tomcat. Here's what the ROOT.war file looks like on the inside:
The app I was told uses a Postgresql as its database. I already have sorted that one out.
I just want to know how to run this app on Apache Tomcat using this application.properties configuration file they have provided. Here's what it looks like:
Thank you very much and regards,
Jeremy
There are may ways to specify the location of the application.properties file. See Spring Boot Documentation for full list of how properties are loaded.
As described in section 2.3. Application Property Files:
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
A /config subdirectory of the current directory
The current directory
A classpath /config package
The classpath root
It goes on to say:
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths).
Of the many options, I'd highlight:
Recommended: According to this answer, you can specify the location in the Tomcat context XML file, although that is not explicitly specified in the Spring documentation:
<Context>
<Parameter name="spring.config.location" value="file:/path/to/config/folder" />
</Context>
Modify the .war file and place the file in the WEB-INF/classes/config or WEB-INF/classes folder.
If you can specify the working directory for the running Tomcat, place the file there or in a config sub-folder.
If you can specify a JVM option for the running Tomcat, add -Dspring.config.location=file:/path/to/config/folder, and place the file there.

Gradle root directory of modules in Java

This is the structure for my Gradle project:
- Game [parent]
|- Server [module]
-- Cache
|- Client [module]
The problem occurs when I try to access the cache from my server java project using:
new File("/cache/cacheItem.dat");
Java will now look in the parent folder (Game) for the cache instead of the server folder and I don't know how to change the root for the server project using Gradle. I would like to know how to change the root directory of the server to it's own folder instead of the parent.
You should not rely on your project structure for accessing and especially writing files. If you package your application for deployment into JARs, any of these paths will no longer be available to write data to and may change even during development, as you have noticed. Instead, you should make your cache path configurable, e.g. from a configuration file and/or command line parameter. This allows the cache path to be configured to an external location upon deployment and to some temporary path during development.
If you only need read access to files at a known location place them under src/main/resources (or src/test/resources for tests). Gradle by convention packages the content of this directory together with your code and everything therein will be available at runtime at root level, during development as well as after deployment. To access such resources on the classpath in Java use getClass().getResource("path/to/resource").

Override NewRelic config file used by Cloud Foundry java build pack

I have a Spring Boot application deployed in Cloud Foundry, using the standard Java build pack. Since the app is bound to NewRelic, the build pack injects a NewRelic agent into the environment.
NewRelic documentation talks about adding a newrelic.yml file next to the agent jar location to customize instrumentation behavior. In the CF scenario, can it be located in the classpath of the deployed Spring Boot jar? How do I enable this behavior?
In the CF scenario, can it be located in the classpath of the deployed Spring Boot jar? How do I enable this behavior?
I don't know about that, but two things you can do:
Specify additional configuration options for the NewRelic agent through your bound user provided service.
https://github.com/cloudfoundry/java-buildpack/blob/master/docs/framework-new_relic_agent.md#user-provided-service-optional
(Optional) Any additional entries will be applied as a system property appended to -Dnewrelic.config. to allow full configuration of the agent.
If you were to fork the Java build pack, you could overlay your own configuration file.
https://github.com/cloudfoundry/java-buildpack/blob/master/docs/framework-new_relic_agent.md#additional-resources
Your custom file would go here: https://github.com/cloudfoundry/java-buildpack/blob/master/resources/new_relic_agent/newrelic.yml
You probably don't want to fork the build pack though as that creates a whole mess of other problems.
I guess a third option might be to use a .profile.d script, but that's a pain with Java apps because you have to put the files at the root of your JAR file. It could work though.
https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#profile
Hope that helps!

How to exclude default application.properties add custom properties file using profiles in maven for spring boot project?

I have developed a web application using spring boot. I have three resource folders in src/main/resources staging,qa,production which consists of application properties and logging configuration for the specific environment. Along with these folders I have application.properties and logging configuration in resources folder which I use it for dev environment. I want to package war file according to the environment using spring-boot-maven plugin. I am new to maven any help would be appreciated?
Convetion is application-{profileName}.properties
Point 10 and 11:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
e.g application-test.properties
it overrides the application.properties
Make profile in pom.xml
Introduction to profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
Rather than using Maven to add different folders to your classpath for each environment, you can use Spring profiles.
First of all create different application.properties for each environment, such as:
application-staging.properties
application-qa.properties
application-production.properties
For the logging you can use the logging.config property. So, in application-staging.properties you could use:
logging.config=classpath:logback-staging.xml
In the other properties files you can use different logging.config properties.
Now just run your application with the spring.profiles.active property.
However, an easier solution would be to use externalized configuration. Rather than having to rebuild each time you want to change configuration for a specific profile, you can externalize it by putting an application.properties file next to your JAR/WAR in the correct environment, rather than on your classpath. Spring boot will pick this up automatically.
Now you can also externalize your logging config by placing a logback.xml (or log4j2.xml, ...) file next to your JAR/WAR and just configure your (externalized) application properties with:
logging.config=file:logback.xml
This allows you to edit your configuration and logging without having to change your JAR/WAR.

Categories

Resources