How can I run multiple profiles with Gradle + Spring boot? [duplicate] - spring-boot

This question already has answers here:
How to run bootRun with spring profile via gradle task
(15 answers)
Closed 3 years ago.
I am trying to learn using gradle with springboot from maven so I wanted to know how I can run my project in command line to pick up a different configuration file.
The scenario here is I have my properties eg.
application-qa.properties and application.properties
however If run gradle with something like ./gradlew -PspringProfile=qa bootRun
It loads only the default properties in application.properties and not the qa properties.
How do I accomplish that with gradle as with maven before?
This is what I have tried.
I created gradle run config in IntelliJ but this has not also worked,
Is there a commad-line option?

First of all, as another user said in the comments to your question, you are not using the correct name of the parameter.
But furthermore, you can't set the active profile with a project property through -P (at least not out of the box). You have a few other options.
A
Use --spring.profiles.active=qa as an argument like this:
gradlew bootRun --args='--spring.profiles.active=qa'
I don't think you can put that type of argument into the "arguments" list in the IntelliJ run configuration though, but you can put it directly into the "tasks" value instead if you like. But that's a bit weird.
B
Default to a particular environment in the Gradle build file when run though Gradle:
bootRun {
environment('spring.profiles.active', 'qa')
}
This makes sense if you are always running against the same environment when using the bootRun task from Gradle.
C
Set an environment variable spring.profiles.active=qa, either on your local computer or in the IntelliJ run configuration.

Related

Pass Dynamic Port from Gradle boot:run of an application

I am having an application, which is running on some port(ex-8080) now when I start this application using gradlew I want to pass dynamic port to start the application?
./gradlew :testApplication:bootRun
is there anyway to pass the dynamic port here??
Add the following to build.gradle so that we can pass parameters to gradlew along to the underlying java command:
bootRun {
if (project.hasProperty('args')) {
args project.args.split(',')
}
}
Pass the arguments you would normally send to a java command (in this case, overriding the server.port) as -Pargs to gradlew:
/gradlew :testApplication:bootRun -Pargs="--server.port=8081"
What is here:
When you run java with arguments --server.port=8081, Spring Boot will override default property (e.g. Spring Boot will ignore your port in properties file, it will use value from command line
-Pargs is the way to ask bootRun to command line arguments. See details here.
See also the same question for maven.
I couldn't pass the port directly.
But if you want a workaround, do the following:
Build the application with gradle build.
Navigate in your project and open the directory build/libs
Now you have to see the jar of your project and then run this command java -jar yourJarProject.jar --server.port=8081.

How do I use Gradle bootRun with --args in Intellij

Gradle 4.9 introduced the --args parameter to bootRun, which I can use easily from the command-line, but how do I use this from a Run/Debug configuration in Intellij 2018?
With a Gradle build set to run the bootRun task on my project in Intellij, I've tried the following arguments in the Run/Debug Configurations screen without any success:
--args 'foo'
--args='foo'
--args=foo
Output from Intellij:
9:18:56 AM: Executing task 'bootRun --args='foo''...
Unknown command-line option '--args'.
9:18:57 AM: Task execution finished 'bootRun --args='foo''.
A similar question documents the older syntax for doing this.
Maybe you can add your args to Tasks as bootRun --args='foo' in IDEA's Run/Debug Configurations.
My task is run --args='-h' and it works for me
Unfortunately, #Linsama's workaround will not work with multiple arguments. For example, run --args='--arg1 --arg2' will not work.
For multiple arguments, you have to move the entire thing in the Arguments field and leave the Tasks field blank.
This will work:
Tasks:
Arguments: run --args='--arg1 --arg2'
As a workaround you can use gradle properties.
In intellij Arguments field add -Pargs=--myArg=value
Then in your build.gradle add
bootRun {
if (project.hasProperty('args')) {
args project.args.split(',')
}
}
They should be now accesible using ApplicationArguments inside your application.

Is there a way to tell gradle which profiles should be used for the tests?

I use the yml configuration files pattern application-{default,dev,production}.yml.
To define which configuration application will use, I fix the environment SPRING_PROFILES_ACTIVE=dev so when the spring app run, it choose the correct configuration.
I have now theses two issues:
The ./gradlew build command also run the test command, test need to have the correct configuration as the application does when it start.
My jenkins does not have access to the database and the units tests keep failing.
Which make make ask:
Does the build command tries all the datasource in order ? Is there a way to specify the spring boot active profile ?
Is there another different approach for deploying spring-boots app in production from jenkins ?
Does anyone has a workaround except
./gradlew -x test build
This is not what I want.
Neither adding #ActiveProfile("dev") to my tests because this require source code modification.
Simply Create multiple property files.For Example:
application.properties
application-test.properties
application-production.properties
Provide different properties based on profile and
Below you can specify
which profile to load in you gradle.build file
def profile = "test"
bootRun {
args = ["--spring.profiles.active="+profile]
}
Put below code in the end of gradle file

In IntelliJ, how do I pass command line variables when building with Maven (TestNG run configuration)?

I'm trying to do the following, but in IntelliJ with a TestNG run configuration:
mvn clean install -Dfoo=bar
So, the value for the foo system property should be bar:
System.out.println(System.getProperty("foo"));
======
bar
All potential answers I've googled either tell me to hardcode variables into my pom.xml (which I can't do) or say to "just set it in Edit Configurations..." without showing what a properly set variable looks like. I dug through the IntelliJ manual too.
I tried all the logical names for "foo" in both Run/Debug Configurations/Parameters tab and Run/Debug Configurations/Environment Variables, such as Dfoo and -Dfoo.
Use a Maven configuration instead of a TestNG configuration. You can still run your TestNG tests from the Maven configuration:

How to set system properties in IntelliJ IDEA 13 gradle task?

I have a Spring Boot project with gradle build tool. The JDBC url, username and password are kept in a property file which is not part of application it's a external property file, the path of the property file is taken from system properties as follows.
export _JAVA_OPTIONS=-DdatabaseConfiguration=db.properties
It is working if I run the application from terminal using gradle bootRun, but when I try to run from Intellij IDEA 13 gradle tasks its not working, the property value is null.
I tried the VM options in Run/Debug Configuration as in the below screen shoot its not working either
How can the JAVA_OPTIONS can be set in Intellij IDEA 13 gradle tasks.
This is because every time you use the Gradle tool window to kick off tasks in IntelliJ, it creates/overwrites the launch configuration for that task.
Basically, I've had to run from the Gradle tool window just once. Then I go into the failed Launch Config (shown in question) and enter the system property in the VM options. From there on out, I need to use that Launch Config to execute the task instead of the Gradle tool window.
Update: Even better solution:
Preferences->Build, Execution, Deployment->Build Tools->Gradle->Gradle VM options
Add your system properties there (i.e. -Dappengine.sdk.root=/opt/google/google-cloud-sdk/platform/appengine-java-sdk)
Doing this will keep them from getting overwritten/lost in the Launch configs that the Gradle tool window generates.
Another thing to note is that using the Gradle tool window causes the commands to be run without access to Environment Variables. This can cause a lot of problems with builds that depend on these env vars.
I ran into this today with the appengine-gradle-plugin and had to put
-Dappengine.sdk.root=/opt/google/google-cloud-sdk/platform/appengine-java-sdk
in the VM options because it was not seeing the env vars. From the command line, it picks up the env vars and works fine. This worked for my appengineRun task.
But it does not work for appengineUpdate since that gives another error caused by lack of env vars: Toolkit not found: apple.awt.CToolkit

Resources