spring boot application properties based on spring profiles - spring

Hi I want my spring boot web project to be deployed both on development and production environment and it should be run on specific profile based setting.
I googled on how to do that, and first of all that I have searched is defining application-{profile name}.properties properly in the src/main/resources classpath.
Now the problem is how to set profiles.
Since I am working on tomcat 8 in linux, there should be some configuration but I don't know how to do that.
and I am also curious that when my project is packaged as war file, java -jar {filename} -Dspring.active.profile=blahblah will not be work, but I think there is an alternative way.
plus, is there an way to set profile on tomcat 8 in Windows 10 ?
Thanks you

First:
I will recommend get rid of dedicated tomcat server and use embedded tomcat, jetty etc. Build your web apps as jar files and just run them. (of course if you don't have any limitations)
Second: You can do this either system property or env variable.
If you go with system property (order is important)
java -Dspring.profiles.active=blahblah -jar {filename}
If you go with env variable you need specify
SPRING_PROFILES_ACTIVE=blahblah

Related

What is the difference between setting profile in quarkus with smallrye.config.profile or quarkus.profile

In quarkus the config is stored inside an application.properties file.
You can have multiple application-{profile}.properties files. {profile} is the name of the profile you want it to be.
When started with java -jar <pathToJar> -Dquarkus.profile=PROFILE_ONE the file application-PROFILEONE.properties is used. During startup of the app you can read that quarkus is using the PROFILE_ONE profile.
When started with java -jar <pathToJar> -Dsmallrye.config.profile=PROFILE_ONE the file application-PROFILEONE.properties is used. During startup of the app you can read that quarkus is using the PROD profile.
What exactly is the difference between both? Is it better to use smallrye.config.profile so that quarkus is still using the PROD profile? Is the PROD profile faster?
Thanks!
That is actually a bug. Internally, both use the same profile, but the log is reporting a different one when you use smallrye.config.profile, because it is only checking for quarkus.profile and then it defaults to prod (later in the code the actual profile is checked and used the correct one).
The message needs to be fixed. I'll look into it.

Multiple web applications in one tomcat instance start with a properties file from another applications

We have multiple web applications in one tomcat instance on 1 server, all running a spring-boot application inside.
Whenever we start tomcat and it starts to boot up all the spring-boot applications we mostly see that each application might use property files/settings from another application.
What especially happens is that we see it sometimes use the database information from other applications being used, resulting in a database which holds tables from other applications. This is scary since we might start a database migration or something.
We also see that the logs are written to the wrong project log file.
We define these settings using a application.properties like (or sometimes application-test.properties or application-secret.properties):
spring.datasource.username
spring.datasource.password
logging.file.name
Anyone have an idea why this is happening?
We found 2 possible causes for this behavior:
if Tomcat is started in a directory where application property files are present, or where application property files are placed in a /config subdirectory, like a WEB-INF/classes directory, these application property files are used by every Spring Boot application deployed in the tomcat instance. To fix this, make sure the Tomcat start script changes the working directory to a directory not containing application property files.
if the 'startStopThreads' attribute of the Tomcat Engine element in server.xml is set to values higher than 1, Spring Boot applications seem to occasionally and randomly use application property files of other Spring Boot applications deployed in the Tomcat instance. When 'startStopThreads' is set to 1, we don't see this behavior.

How do i pass constant values from tomcat to war file(i.e. based upon spring-boot )?

After lot of online search & tried lot of experiments. finally i
didn't get any of the link which can full-fill my requirement, so
finally I choose this platform.
Note : I am using Spring-Boot Maven Project & Tomcat 7.0.62 version & JDK 7.
First of all I was using Embedded Tomcat & produce .Jar and i was passing extra dynamic parameter to .jar using command line argument.
Now, scenario has been changed. My .Jar file will be converts to .war file also we have excluded Embedded Tomcat i.e. not embedded Tomcat.
Now i want to pass same list of command-line argument to my Spring-Boot project's .war file from outside. something like from tomcat.
any help appreciate.
You have a few options in a servlet container/application server:
Use system properties
Use init parameters
Use JNDI
They'll all be available via Spring's Environment so will work pretty much as it you'd passed them in via the command line.

How to run web applications using jersey in an easy way?

I created a web application using Jersey through this maven code:
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.example -DartifactId=simple-service-webapp -Dpackage=com.example \
-DarchetypeVersion=2.4.1
And I am using Tomcat v7 as my Java server. When I finish writing some code, I use mvn's package command to generate a .war file, copy this file to the /webapps folder and then start tomcat to run my application and test it on browser. But I think I waste lots of time doing these things. So I want to ask if there is an easier way test my code on browsers. How do you guys run your web applications, especially Jersey app, on your server?
And I am using Intellij Idea, does it have some features that help me build and run Jersey apps, or other J2EE apps? how to use them?
In IntelliJ IDEA you can create a Tomcat Run/Debug configuration. In that configuration you can specify "before launch" tasks/options, including running a maven goal. So by running the Tomcat configuration, IDEA will run the maven goal, deploy your code to Tomcat, start the tomcat server, and (if desired) open you web browser to a specified page.
JetBrains has a Getting Started with Spring MVC, Hibernate and JSON tutorial. What you want to do is very similar. The main difference is you will need to remove the default "make" option in the "Before Launch" section at the bottom of the run/debug configuration and instead have it run your maven task.
There is also the Creating a simple Web application and deploying it to Tomcat tutorial. It's a little older and some of the options on the run/debug dialog have changed. But at the core, its still valid. Combined with the above, you should be in good shape.
Finally take a look at the Run/Debug Configuration: Tomcat page in the help guide (also available in the online webhelp).

How to set the NetBeans Server in Maven?

I have a Web Application that is environment independent so it's a single war that can be deployed to multiple environments.
I have created multiple Tomcat Servers (i.e. Devel, QA, Production) and defined them in in the NetBeans server configuration.
I'm able to run the war against each of the environments fine by manually changing the project properties but I would like to be able to set what server to connect to from within Maven and either add custom Debug/Run actions or by using maven profiles.
I've tried setting:
<netbeans.deployment.server.id>tomcat70:home=C:\Apache\Tomcat\7.0.34,base=C:\Apache\Tomcat\Devel</netbeans.deployment.server.id>
but that didn't work for me.
Does anyone know if this can be done?
Thanks,
Rob
The proper way to do this is to setup separate profiles in your pom and then have the property set in each of those profiles.

Resources