spring profile not activated on STS tc Server - spring

I have a project with Spring profile
In my web.xml, i have
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
to set the default spring profile.
I build the maven project with
clean install -Dspring.profiles.active="prod"
Then, I choose the option Run As -> Run on Server to deploy the maven project to tc Server.
However, the active profile is still dev.
What is the correct way to activate a spring profile on tc Server

If you are running your app from with STS and the tc Server that comes with it, you can put the system property definition into the launch configuration of tc Server. Once you started up tc Server once, you can modify the lauch config via "Run Configurations...", select the one for Pivotal tc Server, go to the VM arguments and add the -Dspring.profiles.active=prod setting.
Since this is a runtime option, it doesn't have any effect while building the app via Maven (the clean install way you tried).

Configuring Tomcat
defining context param in web.xml – that breaks “one package for all environments” statement. I don’t recommend that
defining system property -Dspring.profiles.active=your-active-profile
I believe that defining system property is much better approach. So how to define system property for Tomcat? Over the internet i could find a lot of advices like “modify catalina.sh” because you will not find any configuration file for doing stuff like that. Modifying catalina.sh is a dirty unmaintable solution. There is a better way to do that.
Just create file setenv.sh in Tomcat’s bin directory with content:
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active="prod"
and it will be loaded automatically during running catalina.sh start or run.

I don't know why the JAVA_OPTS method didn't work for me.
What does is adding spring.profiles.active=dev in /conf/catalina.properties

Related

cannot access to /q/dev on quarkus from an uber-jar despite being in dev

I recently start deploying my quarkus project utilising a jar build by uber-jar
now each time I try to acces to /q/dev or anything like that, I have this error :
RESTEASY003210: Could not find resource for full path: http://participeo-monchoixdevie-api-dev.emotic.fr/q/dev/
Any idea?
I'm running in profil dev.
Running in dev-mode and running from an uber-jar are mutually exclusive. If you're running an uber-jar, you are not in dev mode, and so the dev mode facilities are unavailable. Perhaps you mean that you compiled the uber-jar with quarkus.profile=dev, but that is a bad idea to do (it will probably only affect the set of used configuration values, but won't activate dev mode). Dev mode is what you get by executing mvn quarkus:dev on the project (or an equivalent if you're using Gradle).

In Spring Boot, how do you build a JAR file for a different profile even if proper environment variables have not been set?

I'm new to Spring Boot, so bear with me. Currently, I'm working on a small app just for the purposes of learning Spring Boot. My goal is to deploy it using AWS (elastic beanstalk).
So far, I've created three application properties files:
application.properties: Properties which apply to all profiles.
application-dev.properties: Properties only for development. This includes localhost connection to DB, path to self signed key store, etc.
application-prod.properties: Properties used only for prod. This includes the prod DB details, etc.
Everything works fine when running the app locally using the dev profile since everything has been hard coded in the application-dev.properties.
However, the application-prod.properties file contains references which will be resolved through OS environment variables, such as:
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
I currently do not have the variables DB_USERNAME and DB_PASSWORD set up in my local OS, and I do not wish to do so. But when I run the following command, it obviously fails:
mvn package spring-boot:repackage -Dspring.profiles.active=prod
It fails because it's unable to find the above environment variables.
Is there any way to delay this check until I actually execute the JAR? My plan is to build the JAR locally and then copy it over to my prod server, and run it there. The prod server will definitely have these environment variables.
The workaround I found is this:
mvn package spring-boot:repackage -Dspring.profiles.active=dev
java -jar -Dspring.profiles.active=prod [jar-file-name].jar
However, this feels like a hack. And it may cause issues in the future that I can't think of right now.
You can use any value you want in those properties for prod profile. If the env varieble exists, Spring will take the value from there instead of the properties.
As explained here:
Spring Boot uses a very particular PropertySource order that is
designed to allow sensible overriding of values. Properties are
considered in the following order:
[...]
OS environment variables.
[...]
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
So your application-prod.properties can look like this:
spring.datasource.username=willBeOverridenByEnvValue
spring.datasource.password=willBeOverridenByEnvValue
You have to make sure though that you set both SPRING_DATASOURCE_USERNAME and SPRING_DATASOURCE_PASSWORD env values in your prod server

Build Spring project for run on another system

I Successfully create a spring boot project on my own local system. I want to build a jar file so I can install it on remote server. so I had to configure server address and mySql address of remote server but I can not Build and it have many errors, and they all right cause my system can not see the remote server address and database.
this is my .properties file:
spring.datasource.url=jdbc:mysql://localhost:8081/aths
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
server.address=192.168.24.250
server.port=8080
how can handle it for running on another configurations? ( another IP, datasource, and ...)
Am I doing it right or not? thanks
You can use spring profiles here :
Create different property files for different profiles using application-{profile}.properties format, e.g. application-dev.properties for dev, and application-prod.properties for production put your profile specific configurations in them. Then when you're running the spring boot application, activate your intended profile using the SPRING_PROFILES_ACTIVE environment variable or spring.profiles.active system property.
and at the end, you will run your jar file with command
java -jar -Dspring.profiles.active=prod application.jar
You can have different application.properties within your resources folder and use spring profiles for example application-{profile}.properties and run the application with the specified profile. However this still limits the configuration items to what has been hard coded within the properties files. When running the application, if it was to be distributed to other people, where non of the profiles are supported you can provide a properties file at start up.
So in the same directory for example as the .jar file create a file named application.properties with empty place holders for all the variables required for the application so the admin can enter the details correct for them. Then they will be required to start the application with the following command
java -jar 'applicaitonname.jar -Dspring.config.name="file:/path/to/application.properties"
Or springboot will load properties from application.properties files in the following locations:
A /config subdirectory of the current directory.
The current directory
Failing that the default application.properties from the resources folder will be loaded.

How do I activate a Spring Boot profile when running from IntelliJ?

I have 5 environments:
- local (my development machine)
- dev
- qc
- uat
- live
- staging
I want different application properties to be used for each environment, so I have the following properties files each which have a different URL for the datasource:
- application.properties (containing common properties)
- application-local.properties
- application-dev.properties
- application-qc.properties
- application-uat.properties
- application-live.properties
I am using IntelliJ and running my app using bootRun in the Gradle plugin on my local machine. I will be using deploying the same application war file on all other environments which run Tomcat.
I have tried adding:
--spring.profiles.active=local
to the run configuration under script parameters.
I have tried adding
-Dspring.profiles.active=local
to the run configuration under VM options.
Neither work. I keep seeing the INFO message on startup say: No active profile set, falling back to default profiles: default
If I run my app from the windows command line using
gradle bootRun
but I first set the environment variable
set SPRING_PROFILES_ACTIVE=local
Then everything works.
So my question is, how do I activate my local spring boot profile when running bootRun from IntelliJ ?
I added -Dspring.profiles.active=test to VM Options and then re-ran that configuration. It worked perfectly.
This can be set by
Choosing Run | Edit Configurations...
Go to the Configuration tab
Expand the Environment section to reveal VM options
If you actually make use of spring boot run configurations (currently only supported in the Ultimate Edition) it's easy to pre-configure the profiles in "Active Profiles" setting.
Spring Boot seems had changed the way of reading the VM options as it evolves. Here's some way to try when you launch an application in Intellij and want to active some profile:
1. Change VM options
Open "Edit configuration" in "Run", and in "VM options", add: -Dspring.profiles.active=local
It actually works with one project of mine with Spring Boot v2.0.3.RELEASE and Spring v5.0.7.RELEASE, but not with another project with Spring Boot v2.1.1.RELEASE and Spring v5.1.3.RELEASE.
Also, when running with Maven or JAR, people mentioned this:
mvn spring-boot:run -Drun.profiles=dev
or
java -jar -Dspring.profiles.active=dev XXX.jar
(See here: how to use Spring Boot profiles)
2. Passing JVM args
It is mentioned somewhere, that Spring changes the way of launching the process of applications if you specify some JVM options; it forks another process and will not pass the arg it received so this does not work. The only way to pass args to it, is:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="..."
Again, this is for Maven.
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html
3. Setting (application) env var
What works for me for the second project, was setting the environment variable, as mentioned in some answer above: "Edit configuration" - "Environment variable", and:
SPRING_PROFILES_ACTIVE=local
Tested with IntelliJ Community edition 2021.x
You can create Multiple configurations, one each for a specific profile, In my case below, I have created a dev config with dev profile environment variable.
Goto Run > Edit Configuration
Choose the configuration you want to edit, in the left under Application.
On the right side > Under Environment Variable, update spring.profiles.active=<your profile name> example
spring.profiles.active=dev
(observer:- the variable should be without -D flag)
Save the changes and Run the Spring boot app with the same configuration.
Note:- You can also create a new configuration or copy existing in step 2 above, using the option available in the same panel.
Try add this command in your build.gradle
So for running configure that shape:
For Spring Boot 2.1.0 and later you can use
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
I ended up adding the following to my build.gradle:
bootRun {
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "local"
}
test {
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "test"
}
So now when running bootRun from IntelliJ, it defaults to the "local" profile.
On our other environments, we will simply set the 'SPRING_PROFILES_ACTIVE' environment variable in Tomcat.
I got this from a comment found here: https://github.com/spring-projects/spring-boot/pull/592
A probable cause could be that you do not pass the command line parameters into the applications main method. I made the same mistake some weeks ago.
public static final void main(String... args) {
SpringApplication.run(Application.class, args);
}
I use the Intellij Community Edition.
Go to the "Run/Debug Configurations" > Runner tab > Environment variables > click button "...". Add:
SPRING_PROFILES_ACTIVE = local
spring.profiles.active
In my case I used below configuration at VM options in IntelliJ , it was not picking the local configurations but after a restart of IntelliJ it picked configuration details from IntelliJ and service started running.
-Dspring.profiles.active=local
So for resuming...
If you have the IntelliJ Ultimate the correct answer is the one provided by Daniel Bubenheim
But if you don't, create in Run->Edit Configurations and in Configuration tab add the next Environment variable:
SPRING_PROFILES_ACTIVE=profilename
And to execute the jar do:
java -jar -Dspring.profiles.active=profilename XXX.jar
Try this. Edit your build.gradle file as followed.
ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }
You can try the above way to activate a profile
Here are 2 ways
Using gradle project property
In build.gradle, add
bootRun{
//https://github.com/spring-projects/spring-boot/pull/592#issuecomment-880263914
if (project.hasProperty('profiles')) {
environment SPRING_PROFILES_ACTIVE: profiles
} else {
def profiles = 'dev'
environment SPRING_PROFILES_ACTIVE: profiles
}
}
In intellij gradle configuration, change the value "test" in "-Pprofiles" as appropriate to environment you want to run
Using environment property
Follow answer by #Hubert https://stackoverflow.com/a/39749545/3333878
And configure the run configuration as
Create files properties like these
application.properties
application-dev.properties
application-prod.properties
then run
VM option is hidden by default.
Here is the right way to do it
Run->Edit Configurations->Select the application on the left menu->Add VM Options
and then add
-Dspring.profiles.active=<profile_name>
Replace the <profile_name> with the profile, say local
Click Apply & OK.
Set -Dspring.profiles.active=local under program arguments.

how to set java class loader PARENT_LAST

i have a spring mvc web application that I need to change the class loader on. I need to change the class loader to be equal to PARENT_LAST. I am using WAS 6.1 and already have a jacl script from a previous web application I can copy to do the job.
In the last application Apache ant was used and what they did was to make the deploy dependent on running the jacl script.
In my new web application I am using maven install to create a war file and am deploying that war file to my application server.
How can I set the class loader to be PARENT_LAST using maven? I know how to do it in the console but if there was a way to do it using scripting that would be nice.
Also will this setting be placed somewhere in the war file so that on deploy of the application the setting will be picked up. This question comes from my lack of understanding of how jacl scripts work?
thanks
If you are only deploying the WAR file itself you can't control this, but if you have your WAR file in an EAR file you can use the deployment.xml solution. The deployment.xml file would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<appdeployment:Deployment xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:appdeployment="http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi" xmi:id="Deployment_1347529484613">
<deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_1347544766353" startingWeight="99" warClassLoaderPolicy="SINGLE">
<modules xmi:type="appdeployment:WebModuleDeployment" xmi:id="WebModuleDeployment_1347543866613" startingWeight="1" uri="YourWebApp.war" classloaderMode="PARENT_LAST"/>
<classloader xmi:id="Classloader_1347543866613" mode="PARENT_LAST"/>
</deployedObject>
</appdeployment:Deployment>
Once you are done all you need to do is to add the file in the correct location of your EAR project build assuming you are using src/main/application that would be src/main/application/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml and build the EAR using Maven as normal.
During server deployment this will be picked up by WAS.
AFAIK there is no way to preconfigure WAR for PARENT_LAST during assembly. Classloading policy is specified during deployment, thus the way of setting it depends on how application is deployed.
Changing the policy using the script is straightforward. Scripts are run using wsadmin tool. The Jython snippet below does the job. It can easily be converted to Jacl.
dep = AdminConfig.getid('/Deployment:app_name/')
depObject = AdminConfig.showAttribute(dep, 'deployedObject')
classldr = AdminConfig.showAttribute(depObject, 'classloader')
AdminConfig.modify(classldr, [['mode', 'PARENT_LAST']])
AdminConfig.save()
Websphere uses deployment.xml file to govern deployment setting of each module in an ear file. You can change the classloader setting in deployment.xml at the following path:
/MyTestEAR/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml
I do not know how you can configure that in Maven.

Resources