IntelliJ Variable injectied into Gradle
Hello, I am trying to dynamically resolve dependencies in Gradle for local development versus actual nonprod/prod pipeline builds. To do this, we have created a block in build.gradle like this:
if (project.ext.isLocal) {
implementation 'com.dcsg.fit:common:' + project.ext.fitCommonVersion + '-SNAPSHOT'
} else {
implementation 'com.dcsg.fit:common:' + project.ext.fitCommonVersion + project.ext.config.build.SNAPSHOT
}The idea is that for local development, we want IntelliJ to properly use the SNAPSHOT build of our own common dependency. IntelliJ seems to have issues dynamically analzing variables inside of a Gradle script for dependency resolution with its own internal real time code analysis. If the dependency versions aren't defined in a literal manor, it won't find them and highlight any code that is using that dependency as an error. We have the following variable defined in build.grade for use above:
ext {
config = [
build: [
SNAPSHOT: project.hasProperty('SNAPSHOT') ? '-SNAPSHOT' : '',
]
]
isLocal = true
fitCommonVersion = '1.8.0'
}How can we provide a variable (perhaps environment variable) for the isLocal variable so that IntelliJ's real time code analysis can pick that up dynamically? The idea is that we provide some variable there that doesn't require us to manually change “isLocal = true”, then ensure that we don't check that change into git (should always be isLocal = false in the repo).
Please sign in to leave a comment.
Hello, John!
Could you please clarify: does
isLocalvariable supposed to represent the type of the current Git branch (local/remote) and you want it to update dynamically as you change branches?In regards to the dynamic dependencies: I've tested the code snippets you've shared and, while IDEA highlights such dependencies' declarations as “unrecognized notation”, the actual dependency resolution does seem to be successful: the correct version of the dependency is displayed under External Libraries and code analysis/highlighting does seem to recognize it as well.
Would you be able to share a sample reproducer Project so we can take at the issue as it occurs on your end?
Also, which version of IDEA and Gradle you are using?
isLocal is simply a variable that we manually change in the build.gradle that will alter the conditional logic and for the appending of “-SNAPSHOT” so that we are always using a snapshot build of the dependency during local development. If isLocal is false (which it always is when checked into the Git repo), we have environmental logic where the SNAPSHOT is used for dev, but the release dependency is used for prod release. This works fine, but it requires us to 1) manually change the isLocal=true when we are developing locally, and 2) implement a Git check on isLocal so that we don't accidentally commit isLocal=true into the repo.
John Glista Thank you for sharing the detail!
So,
isLocalis meant to be tied to the Git state, correct?You may want to look into Git Hooks, so you can change the
isLocalvalue when certain Git actions are triggered (IDEA by default picks up third-party changes to build scripts automatically).There is also grgit Plugin for Gradle, which allows for Gradle-Git integration and might allow for the functionality you are looking for.