I have two properties files in src/main/resources application-dev.properties and application-prod.properties.
When I run my application with java -jar -Dspring.profiles.active=dev it picks the correct properties file and my application is running.
However when I use gradle -Dspring.profiles.active=dev test, the properties are not set from the specified profile.
I also created an application.properties file containing spring.profiles.active=dev.
When I compile and run with java -jar test.jar it works as expected but when I use gradle test the properties are ignored.
There are two JVMs involved, one running Gradle and one running your test. When you run gradle -Dspring.profiles.active=dev test you are setting a property on the JVM running Gradle, not the JVM running your test. Gradle doesn't automatically copy system properties from its JVM to the JVM it spawns. This topic at the Gradle forums explains how to set properties on the JVM running your test, but in short you'll need something like this in your build.gradle:
tasks.withType(Test) {
systemProperty 'spring.profiles.active', 'dev test'
}
If you've set the active profile as a system property you can use this:
tasks.withType(Test) {
systemProperty 'spring.profiles.active', System.getProperty('spring.profiles.active')
}
To copy all system properties:
tasks.withType(Test) {
systemProperties = System.getProperties()
}
Related
I use a custom logback.xml to configure logging. Usually I pass it in via the logging.config property in application.properties to the spring application, however I now need to specify a path to it so it can be picked up by a non-spring mvn module.
This command works fine in my intelliJ run configuration:
verify "-Dlogback.configurationFile=${ALLUSERSPROFILE}/myPath/logback.xml" -f pom.xml
I thought that transferring this to my jenkinsFile would be trivial:
withMaven(mavenSettingsConfig: 'nexus-maven-settings') {
withNPM(npmrcConfig: 'nexus-npm-ro-settings') {
powershell 'mvn verify "-Dlogback.configurationFile=${ALLUSERSPROFILE}/myPath/logback.xml" -f webApp/com.pilz.ie.s30.automation/pom.xml'
}
}
but my logbackfile isn't working. I suspect it is a powershell issue with the single quote, double quotes or the Env var, but for some reason the logback file just isn't being picked up.
Any help appreciated!
I have a question. In my app, I do have several configurations:
application-prod.properties
application-test.properties
application-dev.properties
and the main file:
application.properties
Containing one line:
spring.profiles.active=test
To build and run the app I am using ./gradlew buildNeeded
Can I somehow pass the properties suffix: test, prod, dev so it is used in the build process, so I can make different bash scripts to run the installation process on test and prod servers?
I am looking for something like ./gradlew buildNeeded --spring.profiles.active=test or anything that will work...
You can achieve that using spring-gradle-plugin.
From plugin documentation Passing arguments to your application
Like all JavaExec tasks, arguments can be passed into bootRun from the
command line using --args='' when using Gradle 4.9 or
later. For example, to run your application with a profile named dev
active the following command can be used:
./gradlew bootRun --args='--spring.profiles.active=dev'
You can use project properties:
task buildNeeded {
doLast {
def springProfile = project.properties.getOrDefault('spring.profiles.active', 'test')
println springProfile
}
}
Then you can run the project with
./gradlew buildNeeded -Pspring.profiles.active=test
Is there a way to externalize the run config for per environment configurations with Grails 3 applications?
I am aware that there are run config arguments that allow one to run their application under a dev, prod, test, etc environment, but I was wondering if there is a variable, for example "grails.env" that I can change in a properties, YAML, config, or other file that can be read at run time that will do this for me.
This is just to avoid confusion between the application being tested and run in one setting/environment(physical environment like my local machine), then being given to another individual or team to run elsewhere.
The goal is to configure or set this variable so the run team/individual wont need to know or change the run commands.
Im aware of how to configure the environment with the command line, and within intelliJ and Eclipse. I'm wondering if there is a way to default this in perhaps a file that is read instead.
Thanks
I'm not to sure what is confusing you here but let me try and see if I can point you in the right direction
From the documentation here: http://docs.grails.org/latest/guide/conf.html#environments your application can be delivered as a package war file to the target user to have tested, and the war file can be created with any of of the environment configurations
grails test war
Since the command is can be run in the console, you could run with system properties like this:
grails $ENVIRONMENT war
Hope this helps
Update 2
By default the grails run-app runs with the dev environment, but you can force an environment by doing
grails $env run-app
If you want some default configurations to be run when you use the run-app command, then you should make sure that configuration is not in the environment block in either application.yml or application.groovy
Example of configuration in the environment block:
-----------------------------------------------------
environment{
development{
appConfig{
ishybrid = true
}
}
}
Example of configuration not in any environment block:
-------------------------------------------------------
appConfig{
ishybrid = true
}
environment{
development{
//other configs
}
}
So basically depending on what you exclude from the environment block configuration is what will be configured as the default configuration for your application to run.
Hope this helps better?
My Spring run configuration is just the default with the following the VM options:
-Dspring.profiles.active=local
My Maven run configuration is the defaults with the following in the command line:
spring-boot:run
and the following in VM options:
-Dspring.profiles.active=local
When I run the maven one it doesn't pick up on a profile and instead uses default:
No active profile set, falling back to default profiles: default
With the Spring configuration I have no issues, the problem is with how I need to deploy it, it uses a maven command, so I can't have this failing and I don't really understand why it's happening. There really isn't anything fancy is this projects. It's your basic micro service.
Just in case it's needed. The root 'Application' file only has the following:
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Link to the Maven run config. It is all defaults outside of what is pictured. https://imgur.com/a/GGiwimQ
Adding the following to the command line arguments seemed to fix it:
-Drun.jvmArguments=-Dspring.profiles.active=local
Although I understand why it worked it doesn't explain why this happened in the first place. I will update this answer if I ever find out the true reason.
create
application.properties
file in src/main/resources if it doesnt exist and add
spring.profiles.active=local
I need to analyse code via Sonar using Gradle and I have some strange problem which I cannot handle. I have the following project structure
-java
|-src
|-main
|-test
|-integration-test
And Sonar analyse only test directory by default and integration-test directory if I change sonar.test property to /src/integration-test, but I want to analyze this two directories together and I actually dont know how to do it. Below we have my sonarqube task properties from gradle.build file.
sonarqube {
properties {
property "sonar.projectName", "projectName"
property "sonar.projectKey", "org.sonarqube:projectName"
property "sonar.host.url", "http://sonar.doesntmetter"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.junit.reportsPath", "build/test-results/test"
property "sonar.jacoco.reportPath", "build/jacoco/test.exec"
property "sonar.jacoco.itReportPath", "build/jacoco/integrationTest.exec"
property "sonar.login", "admin"
property "sonar.password", "admin"
}
}
What I noticed is that when I run gradle build there would be integrationTest.exec and test.exec in /build/jacoco/ directory, but in case I run gradle sonarqube with default sonar.test property there would be only test.exec, and from other hand when I run gradle sonarqube with sonar.test=/src/integration-test there would be both test.exec and integrationTest.exec.
Dont you know how to analyze unit and integration test in one run?
I think you can add this to your Gradle:
sonarqube {
properties {
properties["sonar.sources"] += sourceSets.custom.allSource.srcDirs
properties["sonar.tests"] += sourceSets.integTest.allSource.srcDirs
}
}
Just use += to add more tests/sources.
More information and examples are here.
EDIT:
Add report paths too! Properties are named:
sonar.surefire.reportsPath - test.testResultsDir (if the directory exists)
sonar.junit.reportsPath - test.testResultsDir (if the directory exists)
EDIT:
I rewrite your code and set sources for testing in one line, check it with your structure
sonarqube {
properties {
property "sonar.projectName", "projectName"
property "sonar.projectKey", "projectKey"
property "sonar.host.url", "http://itsprettyprivate"
property "sonar.java.coveragePlugin", "jacoco"
// Or add it via += to properties, it is up to you ;)
property "sonar.sources", "src/java,src/test,src/integration-test"
//Tells SonarQube where the unit tests execution reports are
property "sonar.junit.reportsPath", "build/test-results/test"
//Tells SonarQube where the unit tests code coverage report is
property "sonar.jacoco.reportPath", "build/jacoco/test.exec"
//Tells SonarQube where the integration tests code coverage report is
property "sonar.jacoco.itReportPath", "build/jacoco/integrationTest.exec"
property "sonar.login", "admin"
property "sonar.password", "password"
}
}