How to set environment variable using Gradle? - gradle

I know I can't set environment variable directly from Gradle, but is there some other solution?. I need to do this:
do some stuff..
set 4 environment variables (their values depend on some settings)
run some ant scripts (that depend on environment variables)
I thought of creating a file (.setenvironment) with all the settings I need, and then source it from Gradle (source .setenvironment), but I fear that I wouldn't be able to reset the variables if something goes wrong (and I need to set "JAVA_HOME", for instance, which is also important for the build scripts themself).

You can't set environment variables from java and other JVM languages. The only way of doing it is via ProcessBuilder.
You can also set the variables before gradle is run.

Related

Gradle Properties as gitlab environment variable

Is there a way to pass all Gradle properties kept in the GitLab Enviroment variable,
let's say I will create a Variable ALL_IN_FOR_GRADLE with the following key=value pairs
A=V1
B=V2
using these key- values in
./gradlew e2e <PROPERTIES>
You can set the GRADLE_OPTS environment variable which is the standard way to configure gradle using environment variables. You can also expand variables within variables, so you could compose GRADLE_OPTS from other variables in your GitLab .gitlab-ci.yml configuration:
variables:
GRADLE_OPTS: $ALL_IN_FOR_GRADLE
# OR
GRADLE_OPTS: "org.gradle.foo=bacon org.gradle.bar=eggs"
Alternatively, you can create a file type variable for the gradle.properties file... however this would likely conflict with repos which commit their own gradle.properties so I would not recommend that.

Environment Variables required to setup maven?

What are the environment variables required by a developer to ensure maven is setup correctly on a machine? are M2_HOME, MAVEN_HOME, MVN_HOME and MVN_OPTS all the same?
All you need to set is JAVA_HOME pointing to a JDK installation. The first three do not exist anymore in master. The latter is options passing args to the JVM.

How to set Java home path during building Android gradle file via command line?

I want specify the Java home path during building my Android gradle via command line; for example,
gradle build -d path of jdk
Is it possible?
According to gradle documentation:
The following properties can be used to configure the Gradle build
environment:
...
org.gradle.java.home Specifies the Java home for the Gradle build
process. The value can be set to either a jdk or jre location,
however, depending on what your build does, jdk is safer. A reasonable
default is used if the setting is unspecified.
org.gradle.jvmargs Specifies the jvmargs used for the daemon process.
The setting is particularly useful for tweaking memory settings. At
the moment the default settings are pretty generous with regards to
memory.
In other words, you can do it simply by running
gradle build -Dorg.gradle.java.home=<java home path>
Depending on what you want to accomplish, one of the following should work.
As Amnon Shochot suggested, set the -Dorg.gradle.java.home flag. This is probably preferable in most cases.
If you want to have use a particular JDK throughout, set the JAVA_HOME variable appropriately before executing gradle.
$ export JAVA_HOME=/usr/local/specialJava/
$ gradle build
If you don't want to change the environment, try adding the below to your build.gradle script. It should affect only the compiler used to compile Java code, nothing else. So Gradle doesn't run inside this particular JDK, but it will use it for compiling.
tasks.withType(JavaCompile) {
options.fork = true
options.forkOptions.executable = "/usr/local/specialJava/bin/javac"
}
(Last option stolen from here)

how to use env variables from properties file in jenkins

I want that my jenkins job will use env variables from a properties file. is that possible, and how?
Thanks, Ohad.
EDIT: I want environment variables to be resolved when checkout from svn, so that few projects will have the same revision. I am trying to use a solution suggested here.
There are a couple of plugins used for this type of thing. You might want to take a look at the following two. I have feeling the one your looking for is the second one.
Setenv
Envfile
If you want to get all manual about it you can also do this.
Manage Jenkins -> Configure System -> Global Options -> Environment variables
Goodluck!

Setting environment variables for application Maven

My question is i have a pom file which calls a bat file which sets environment variables for the application. Then i call the ant script which uses these environment variables to compile and execute. These environment variables cant be recognized by ant script and it fails. I believe its because both run in different context. Can you guys let me know how to link this together.
org.codehaus.mojo
exec-maven-plugin
1.1
Pre_Clean
pre-clean
exec
env.bat
maven-antrun-plugin
Compile
compile
run
Yes. When the batch file ends the environment changes go out of scope. So, you need to call ant from within your .bat file OR set up the environment before starting maven OR set them up in the POM.

Resources