Pass Dynamic Port from Gradle boot:run of an application - spring-boot

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.

Related

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

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.

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.

Specifying application config file by name when Spring Boot starts up

Currently I can build my Gradle-based Spring Boot app like so:
./gradlew build && java -Dspring.config=. -jar build/libs/myapp.jar
And this works fine provided I have an application.yml in the root of my project directory.
However, I would now like to have both an application-local.yml as well as an application-dev.yml, and to specify which one to use when I build + run myapp.jar.
How can I specify either file at startup?
You can use Spring boot's capability of using Profile Specific property file.
You can specify the application yml inline with your profile name
application-[profile].yml. In your case, it would be
application-dev.yml
application-local.yml
Specify the profile you would want to use as a command line argument
-Dspring.profiles.active=dev

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

Spring boot running a fully executable JAR and specify -D properties

The Spring Boot Maven and Gradle plugins can now generate full executable archives for Linux/Unix operating systems.Running a fully executable JAR is as easy as typing:
$ ./myapp.jar
My question is in this case how to set -D properties, e.g.
-Dspring.profiles.active=test
In addition, if server does not install jdk , could this fully executable jar still run?
There are two ways to configure properties like that:
1:
By specifying them in a separate configuration file. Spring Boot will look for a file named like JARfilename.conf which should be stored in the same folder like the JAR file. There you can add the environment variable JAVA_OPTS:
JAVA_OPTS="-Dpropertykey=propvalue"
2:
Or you can just specify the value for the environment variable in the shell before you execute the application:
JAVA_OPTS="-Dpropertykey=propvalue" ./myapp.jar
Have a look at the documentation for the complete list of available variables: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#deployment-service
Regarding your second question: To execute a JAR, you don't need a JDK, a JRE is sufficient (but you need at least that, if you don't have any java installed on the server, the application won't run).
By default SpringApplication will convert any command line option arguments (starting with ‘--’, e.g. --server.port=9000) to a property and add it to the Spring Environment. As mentioned above, command line properties always take precedence over other property sources.
e.g.
$ java -jar myapp.jar --spring.application.json='{"foo":"bar"}'
please see http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

Resources