Currently we deploy our Java based azure-function and then set the configuration:
mvn azure-function:deploy
az functionapp config appsettings set --name function-app --resource-group XXXX --settings "EndPoint=https://XXXX" "Key2=YYYY" "Key3=ZZZZ"
This is not perfect. Is there a maven task to set the configuration?
Even better, if it could deploy and set the configuration together?
The configurations would need to be provided as command line arguments?
I have created Profiles in Java class like this,
#Profile(value = "cache")
public class MapCache {
....
}
These profiles are not getting activated when spring.profiles.active used, but if i use spring.profiles.include profiles are working fine.
I would like activate profiles through properties which are added in application.properties
Note: application is running in independent jetty instance.
Any tip would be great on this.
To activate a profile via annotations you need #ActiveProfile("profileName"). The #Profile annotation is used to label an object as being a member of the profile.
you can also try to run the application and pass them as command line args
java -jar -Dspring.profiles.active={your_profile_name} application.jar
or if you run the app via maven:
mvn spring-boot:run -Dspring-boot.run.profiles={your_profile_name}
Here is an nice example I found on the internet with all the ways you can set the spring profile, it should help : https://www.baeldung.com/spring-profiles
I encountered a similar issue where SpringBoot wasn't activating the profiles set in the spring.profiles.active application property.
The issue was the result of the code base using a non standard name and location for the application property file (not my doing). Once I specified the location via the command line arg- --spring.config.location=/non/standard/location/acme.properties- the profile was activated.
I am using a javaagent for my spring boot app and currently I am running it via
java -javaagent:agent.jar -jar app.jar
My project is a gradle project and I want to embed the agent.jar inside the app.jar so that I can run it as
java -javaagent:app.jar -jar app.jar
It can be done via boot maven plugin as mentioned here - https://jeroendruwe.be/spring-boot-and-new-relic/ but there is no alternative for boot gradle plugin. The closest I find is this - https://jdpgrailsdev.github.io/blog/2014/04/08/spring_boot_gradle_newrelic.html, but it does not embed the jar as intended.
Is there anyway it can be done via gradle?
Can be add as dependency jar from local directory. 'libs' is directory inside your project.
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
gradle jar :
jar {
manifest{
attributes "Agent-Class" : "com.......agent.AgentTest"
//attributes "Premain-Class" : "com.......agent.AgentTest"
}
}
Althought, I am using to do like this :
public static void main(String[] args) throws Exception {
String pid = "6236";
String agentPath = "...";
VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid);
virtualmachine.loadAgent("/home/aaa/Code/agent-1.0-SNAPSHOT.jar");
virtualMachine.detach();
}
Did you try
java -Xbootclasspath=('your path'/app.jar or 'your path to libs'/agent.jar) -javaagent:'your path'/app.jar -jar 'your path'/app.jar
?
Another approach: use jar as dependency and move (or create another one) your premain class in Spring Boot application jar.
Is there any way to input arguments when launching spring-boot application (mvn spring-boot:run) from commandline and then get them in main()?
Looking at the source code of the spring-boot-maven-plugin I found that you need to do:
mvn spring-boot:run -Drun.arguments="arg1,arg2"
Another way to get more information about what options the run goal of the spring-boot plugin supports is to execute the following command:
mvn help:describe -Dcmd=spring-boot:run -Ddetail
For Spring Boot 2.x, the source is here and you now need to use -Dspring-boot.run.arguments="args1,args2"
(edit from april 2021)
For Spring Boot 2.2+, you now need to use -Dspring-boot.run.arguments="args1 args2"
If you are using Gradle and you want to be able to pass command line arguments to the Gradle bootRun task, you first need to configure, for example like so:
bootRun {
if ( project.hasProperty('args') ) {
args project.args.split('\\s+')
}
}
and run the task using gradle bootRun -Pargs="arg1 arg2"
When passing multiple arguments using -Drun.arguments, if the argument in turn has 'comma-separated' values, then only the first value of each argument is used. To avoid this repeat the argument as many times as the number of values.
This is more of a workaround. Not sure if there is an alternative unless the delimiter is different - like '|'.
E.g Issue:
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,dev"
Picks only 'test' profile for the above command.
Workaround:
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,--spring.profiles.active=dev"
Picks both 'dev' & 'test' profiles for the above command.
Be aware : The way of passing arguments depends on the spring-boot major.minor version.
TLDR
For Spring Boot 1:
mvn spring-boot:run -Drun.arguments="argOne,argTwo"
For Spring Boot 2.0 and 2.1:
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
(edit from april 2021)
For Spring boot 2.2 and later:
mvn spring-boot:run -Dspring-boot.run.arguments="argOne argTwo"
spring-boot-maven-plugin version and the the Spring Boot version you use has to be aligned.
According to the Spring Boot major version used (1 or 2), the spring-boot-maven-plugin in the 1 or the 2 version should indeed be used.
If your pom.xml inherits from the spring-boot-starter-parent :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>ONE_OR_TWO_VERSION</version>
</parent>
In your pom, the version of the plugin used should not even be specified as this plugin dependency is inherited :
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
...
</configuration>
</plugin>
</plugins>
In case of your pom.xml not inheriting from spring-boot-starter-parent, don't forget to align the version of spring-boot-maven-plugin with the exact version of spring boot you want to use.
Passing arguments in command line with spring-boot-maven-plugin:1.X.X
For one argument :
mvn spring-boot:run -Drun.arguments="argOne"
for multiple :
mvn spring-boot:run -Drun.arguments="argOne,argTwo"
The maven plugin page documents it :
Name Type Since Description
arguments | String[] | 1.0 | Arguments that should be passed
to the application. On command line use
commas to separate multiple arguments.
User property is: run.arguments.
Passing arguments in command line with spring-boot-maven-plugin:2.X.X
For one argument :
mvn spring-boot:run -Dspring-boot.run.arguments="argOne"
for multiple :
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
I didn't find the plugin documentation for the 2.X.X version that refers to that.
But the org.springframework.boot.maven.AbstractRunMojo class of the spring-boot-maven-plugin:2.0.0.M3 plugin refers to this user property:
public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo
...
#Parameter(property="spring-boot.run.arguments")
private String[] arguments;
...
protected RunArguments resolveApplicationArguments(){
RunArguments runArguments = new RunArguments(this.arguments);
addActiveProfileArgument(runArguments);
return runArguments;
}
...
}
Hint : as you pass more than one argument, whitespaces between commas are considered.
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
will be interpreted as ["argOne", "argTwo"]
But this :
mvn spring-boot:run -Dspring-boot.run.arguments="argOne, argTwo"
will be interpreted as ["argOne", " argTwo"]
(edit from march 2021)
Whitespaces are now used as separator for multiple-arguments commands, see the relevant issue.
As I checked today, the correct usage for Spring Boot 2.2.5 is:
mvn spring-boot:run -Dspring-boot.run.arguments="--arg1=value --arg2=value"
Because help says:
commandlineArguments
User property: spring-boot.run.arguments
Arguments from the command line that should be passed to the application.
Use spaces to separate multiple arguments and make sure to wrap multiple
values between quotes. When specified, takes precedence over arguments.
Spring Boot 1 as 2 provide a way to pass multiple profiles as argument and avoid the issue related to the comma used both as separator between the args and the values passed as active profile.
So instead of writing :
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=test,--spring.profiles.active=dev
use the Spring Boot Maven profiles property that is a convenience shortcut of spring.profiles.active such as the followings :
The Maven user property is different according to the Spring Boot version.
For Spring Boot 1.4+, that is run.profiles :
mvn spring-boot:run -Drun.profiles=dev,test
For Spring Boot 2, that is spring-boot.run.profiles :
mvn spring-boot:run -Dspring-boot.run.profiles=dev,test
From the plugin documentation :
profiles:
The spring profiles to activate. Convenience shortcut of specifying
the 'spring.profiles.active' argument. On command line use commas to
separate multiple profiles.
Type: java.lang.String[]
Since: 1.3
Required: No
User Property: spring-boot.run.profiles
I'm using spring.boot 2.4.2 and i separated the arguments with withe-space and put the values between double quotes.
mvn spring-boot:run -Dspring-boot.run.arguments="--param1=value2 --param2=value2"
And if you're using Eclipse...
| Parameter Name | Value |
| run.arguments | "--name=Adam" |
This is what worked for me (spring-boot v1.4.3.RELEASE),.
mvn spring-boot:run -Dspring.profiles.active=test,local -Dlogback-debug=true
For the latest version of spring use -Dspring-boot.run.arguments= as shown in the example below
spring-boot:run -Djasypt.encryptor.password=temp -Dspring-boot.run.arguments="OU,Grade"
Use the following command for a Spring Boot application.
mvn spring-boot:run -Dspring-boot.run.arguments="--java.net.preferIPv4Stack=true --config.password=PASSWORD --config.token=s.TOKEN --spring.application.name=ENV --config.server.ip=IP_ADDRESS --spring.profiles.active=ENV --spring.profiles.active.custom=ENV_custom"
Java Maven Spring Junit with webapplication
I am using following code to load property file into spring context placer holder.
<context:property-placeholder location="file:${RESOURCE_PATH}/jdbc.properties" />
in eclipse Junit run time configuration i have defined "RESOURCE_PATH" so it runs fine when i execute my junit tests from GUI but when i run from maven they fail.
Can we define variable and pass in pom file at run time ?
You should either supply the property RESOURCE_PATH using -D switch when running maven or put it into pom.xml into section <properties>; something like this:
<properties>
<RESOURCE_PATH>put your path here</RESOURCE_PATH>
</properties>