Gradle: Load property file dependent on profile - gradle

I want to write a gradle build script, which works in different environments (development, live).
In each environment I have to load different property sets (target directories, databases, etc.).
Is there a gradle way to read a property file dependent on the environment or a profile?

You may want to check out the Gradle Properties Plugin.
Include plugin:
plugins {
id 'net.saliman.properties' version '1.4.2'
}
Create property files:
gradle-dev.properties
or
gradle-prod.properties
Call gradle:
gradle myTask -PenvironmentName=dev
gradle myTask -PenvironmentName=prod

Also have a look at gradle's equivalent of maven's profiles.

I had the same problem, but I use spring and configured it to read properties from classpath:application.properties
In this case you can add this to gradle.build with java plugin
if (project.hasProperty('env')) {
println "Target environment: $env"
sourceSets.main.resources.srcDir "src/main/environment/$env"
}
this for different folders based on environment
So for adding resources based on environment 'dev' you should have 'src/main/environment/dev' folder (with properties file), and invoke gradle: gradle myTask -Penv=dev

Related

Setting up Artifactory in gradle global

currently I have a project which is deploying artifacts in our artifactory. For that Project everything is setted up perfect and it works.
I just wonderd if there is a possibility to set up the whole artifactory configuration global in gradle, so that I don't have to write the artifactory {...} stuff for each project.
You can maintain a file lets say build_dependency.gradle and define the task for all project
allprojects
{
//task common for all the project
}
subprojects
{
//task for subprojects
}
or specify the type of project e.g ext.warProject = 1 in dependency file and refer it in build_dependency.gradle as
if(project.hasProperty('warProject '))
{
//task here
}
and use this file in build.gradle like apply from: "$rootDir/path_to_file/build_dependency.gradle"
"$rootDir/path_to_dependenccy_file"`
You could simply write your own Gradle plugin that would be responsible for:
applying the artifactory plugin and other related plugin(s) like maven-publish
provide default values for the artifactory extension properties , like contextUrl, repoKey, credentials, etc...
Then your different projects will just have to apply your custom plugin, and provide only the project-specific configuration (configuration of the artefact to be published, for example, in publishing extension)
EDIT there are other ways to implement that, but it depends on what you mean by "global in gradle":
global to your own computer? then you could create a User InitScript that would contain the artifactory plugin configuration part
global to your team/company ? then you could need to implement a custom plugin, and maybe include this plugin into a custom gradle wrapper distribution (see example here
EDIT2 If you just want to set the artifactory plugin configuration of different sub-project of a same multi-project build, then the simpliest solution would be to define this configuration in the subprojects block of the root project build script:
subprojects {
apply plugin: "com.jfrog.artifactory"
artifactory {
publish {
contextUrl = '<repo url>'
repository {
repoKey = "<repo name>"
username = "user"
password = "pass"
}
}
}
}

How can I use the gradle idea task to set Intellij's default settings?

How can I use the gradle idea task to set values in Intellij's default.xml file?
Background: I'm creating a tool to reconfigure Intellij, and some users will be using the IDE's global configuration, and other will be using the project's configuration, so I need to change both of them. The global configuration is stored in the default.xml file, and the project settings are stored in the projects ipr file.
I can make the necessary changes to the ipr file, but not the default.xml file.
There is a plugin which generates IDEA project configuration and allows you to modify it Gradle IDEA Plugin.
Set JDK and language level:
apply plugin: 'java'
apply plugin: 'idea'
idea {
project {
jdkName = '1.6'
languageLevel = '1.5'
}
}
The project files will be generated, when you run the idea task: gradle idea.
The only one catch: the plugin does not support the IDEA directory based project configuration layout, only the file based.

How to get Intellij to recognize properties in application.yml

I am trying to get Intellij to recognize my properties using gradle. I have followed the steps here. So this means I have a #ConfigurationProperties annotated class with some properties.
I added the spring dependency to process them:
dependencies {
optional "org.springframework.boot:spring-boot-configuration-processor"
}
compileJava.dependsOn(processResources)
I added the plugin (I've tried not using the plugin and just making it a compile dependency, no change)
buildscript {
repositories {
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies { classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE' }
}
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
When I run the build, I see a build/classes/java/main/META-INF/spring-configuration-metadata.json file is created based off of my properties.
When I try to use the property in either application.yml or application.properties, Intellij says it cannot resolve it.
The docs does say it should be called additional-spring-configuration-metadata.json and may expect it to be called that to process it, but I do not see a way to make the build name it that way nor configure Intellij to expect otherwise.
Has anyone got this working with gradle? Or is this a bug.
Edit I created a repo with a pair of projects to demonstrate this. One for gradle and one for maven. I created the projects from start.spring.io and basically just added the properties configuration. I also used a straight compile dependency in both cases instead of optional / compileOnly.
I had not confirmed this before, but the code assist does work for maven, but not gradle. Both create a spring-configuration-metadata.json in the META-INF in their respective build folders. I am not exactly sure who is not picking it up.
Misc relevant versions
Intellij: 2017.3.4
Springboot: 1.5.9
Gradle: 4.4.1
Java: 8.161
Turn the annotation processing on
Do not delegate IDE build/run actions to Gradle
Rebuild your project in IDE: Build -> Rebuild Project
As far as I can tell, IntelliJ (at the time of this writing, 2018.1.2) wants the spring-configuration-metadata.json file to either be in a main source root (src/main/resources/META-INF/ or src/main/java/META-INF/) or in its default output directory for it to pick it up for autocompletion of properties in your source tree. To expand on phospodka's comment, you can add something like this to your build.gradle to satisfy IntelliJ.
task copyConfigurationMetadata(type: Copy) {
from(compileJava) {
include 'META-INF/spring-configuration-metadata.json'
}
into "out/production/classes"
}
compileJava {
dependsOn processResources
finalizedBy copyConfigurationMetadata
}
This answer is a combination of the (at this time) other two answers with a minor twist. In my case this is what "fixed" the issue: (in 2019.03.01-Ultimate)
Turn on the default annotation processing (File>Settings>Build, Execution, Deployment>Annotation Processors>Enable Annotation processing
Select Obtain processors from project classpath
Select Store generated sources relative to module output directory
keep other defaults
click OK
Add the code in #thyme's answer to your build.gradle
EXCEPT instead of into "out/production/classes"
use: into "build/generated/sources/annotationProcessor"
Now you should be able to run gradle clean/build and Intellij should be able to find your "additional metadata" definitions.
Notice that even though the build.gradle code doesn't explicitly mention 'additional-spring-configuration-metadata.json', it is exactly that "additional metadata" that ends up in the annotationProcessor folder as "spring-configuration-metatdata.json" where Intellij finds it.
EDIT: Also note, you need to clean / rebuild after adding any new "additional metadata" items before Intellij will see the new entries (in the freshly regenerated file).

How to provide correct settings for gradle?

Using Jenkins for CI, I need to use hidden credentials for Gradle or maven publishing. My credentials are applied in Jenkins rather than in source code. Maven settings are in a settings.xml, but I would like to define properties in Jenkins. What would I use in a command-line to do this? I imagine
gradle -Dsomething.username=blah -Dsomething.password=secret
There may be a better way but this is how we do it: we define target maven repository in build.gradle using custom project properties and then provide those properties via command line:
uploadArchives {
repositories {
mavenDeployer {
repository(url: "https://your-repo-server.company.com/your-built-artifacts") {
authentication(userName: project.getProperties()['nexusUploadUsername'], password: project.getProperties()['nexusUploadPassword'])
}
}
}
}
gradle -PnexusUploadUsername=blah -PnexusUploadPassword=secret
You can also take one step further and configure them as Jenkins build parameters or environment variables in order to prevent people from seeing them in logs (do not forget to set that 'Mask passwords' checkbox): gradle -PnexusUploadUsername=$JENKINS_NEXUS_USER -PnexusUploadPassword=$JENKINS_NEXUS_PASSWORD.

Is it possible to override sonar.modules in the sonarqube gradle plugin?

I'm trying to set up sonar analysis across a non-standard multi-project directory structure with a root project to aggregate all the sonar data.
Using sonarqube runner, I've tried to set the property sonar.modules with all the sub-projects (which are not in child directories), but the plugin seems to override the property with erroneous default values that cause my build to fail.
The overwrite seems to happen in this java class : https://github.com/SonarSource/sonar-gradle/blob/master/src/main/java/org/sonarqube/gradle/SonarQubePlugin.java on line 190.
Is there a way around this problem?
I have not tried, but looking at how other properties(sonar.projectKey, sonar.projectName, sonar.projectVersion) within the same addGradleDefaults method are begin set, Did you try the same pattern followed for configuring the other properties per SonarQube plugin documentation ?
FYR, Here is the link from SonarQube on How to use the plugin and or override the default values within the gradle build script. http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle
Here is the code snippet
apply plugin: 'org.sonarqube'
sonarqube {
properties {
property "sonar.projectName", "My Project Name"
property "sonar.projectKey", "org.sonarqube:java-gradle-simple"
}
}

Resources