I am building my java project with bazel. I want to use environmental variable at build/run time. According to the documentation --action_env=APP_ENV=Development should work.
So i do bazel run myproject:app --action_env=APP_ENV=Development
But System.getenv("APP_ENV"); is null
In my intellij IDE I have the following. Neither --action_env=APP_ENV=Development nor --action_env APP_ENV=Development work
Did i get something wrong here?
You can use --run_under to add a prefix to the Bazel run command. This can be used in the IntelliJ Run/Debug configuration to set environment variables.
--run_under='export VAR1=FOO VAR2=BAR && '
Launch Intellij from a terminal so it has the environment variables that it needs during bazel sync (build) and bazel run.
On MacOS, use the open command to launch it.
$ export POSTGRES_USER=dev_user
$ open -a "IntelliJ IDEA CE"
$
Bazel will only pass variables to the build processes if you provide them on the command line:
$ bazel build --action_env=POSTGRES_USER //...
Or specify them in .bazelrc:
build --action_env=POSTGRES_USER
The current version of the Intellij bazel plugin allows you to specify a bazel binary. You can take advantage of this to add specific environment variables per run configuration.
Let's say you want to add the following to the runtime environment:
VAR1=VAL1
VAR2=VAL2
Then create a file called say bazel_custom with the following contents (replace /path/to/bazel with the correct path to bazel):
#!/usr/bin/env bash
export VAR1=VAL1
export VAR2=VAL2
/path/to/bazel "$#"
Save the file and mark it as executable:
chmod +x bazel_custom
Then edit your Intellij bazel run configuration and set the path to the task-specific bazel binary to /path/to/bazel_custom.
Related
I am working on a project on a virtual machine that is running on windows 10, and where I don't have the user access to system variables in GUI. Since, I need to use Maven I am wondering how can I set the java and maven variables with bash terminal?
I have tried to set the variable with this command:
export M2_HOME="/c/Program Files/apache-maven-3.6.3"
export M2="$M2_HOME/bin"
export MAVEN_OPTS="-Xms256m -Xmx512m"
And then if ran mvn --version in the terminal, I got:
bash: mvn: command not found
If I ran printenv I see in the list that the variables were set:
M2_HOME=/c/Program Files/apache-maven-3.6.3
M2=/c/Program Files/apache-maven-3.6.3/bin
MAVEN_OPTS=-Xms256m -Xmx512m
But, if I close the terminal and open it again, then I don't see these variables again.
What is the right way to set up Java and Maven env variables through bash on Windows?
You need to escape the string by surrounding it with quotes:
export M2_HOME="/c/Program Files/apache-maven-3.6.3"
EDIT:
To answer the question in the comments, note that you still need to add mvn's path to the $PATH variable in order to use it:
export PATH=${PATH}:${M2}
I would like to execute a git pre-push hook from the IntelliJ IDEA.
Git pre-push hook can able to execute via a Terminal but not from the IntelliJ IDEA.
I have posted the Version Control console:
And Its saying JAVA_HOME is not set.
Used Versions
Java : 1.8.0_211
IntelliJ IDEA : IntelliJ IDEA 2019.2 EAP (Ultimate Edition), Build #IU-192.4787.16
Ubuntu : 18.04
Git : 2.17.1
Git pre-push hook script
source ~/.bash_profile
remote="$1"
url="$2"
echo “Running Tests”
pwd
./gradlew test
exit $?
I am not getting the where is a problem exactly and How can I solve this issue ?
The issue is that there is no JAVA_HOME variable defined in the environment the hook is run in. The environment is inherited from the parent process, so depending on how and when IntelliJ is started, its environemnt
The easiest way to fix the issue is to define JAVA_HOME in the hook itself (or maybe in .bash_profile), e.g. by adding export JAVA_HOME=<PATH to your Java Home> in it. This way variable will be set during hook execution and gradlew will not fail.
I'm trying to create a maven project with VS Code but when I run the command it says :
'mvn' is not recognized as an internal or external command,operable program or batch file.
but mvn -version is running on command prompt
Environment varaible for User :
MAVEN_JOME : C:\apache-maven-3.6.1,
M2_HOME : C:\apache-maven-3.6.1,
JAVA_HOME : C:\Program Files\Java\jdk1.8.0_212\jre and
System Variables
path is set to C:\apache-maven-3.6.1\bin, C:\Program Files\Java\jdk1.8.0_212\bin for maven and java respectively.
The command I'm running to create the project is:
mvn archetype:generate -DgroupId=com.cs.test-project -DartifactId=test-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false The Reference to Command
And the VS Code Reference to Command
The same command is running in Command prompt and it created the project successfully on Desktop.
I fixed this problem by simply restarting VSCode as I had set my PATH variable while my VSCode was running so the changes were not reflected to the VSCode integrated terminal until I restarted it.
It's kindy counterintuitive as path has to be pointed to the actual mvn command, instead to a M2_PATH folder. Also once properly pointed, a new error will be shown if your environment is missing proper jdk within JAVA_HOME. The error is trown by maven. Maven plugin is kinda stupid as it will ignore your default JDK configured within settings.json (java.home or/and java.configuration.runtimes properties) and will happly NOT set java for maven. You have to configure it specificly for maven plugin as this:
"maven.executable.path": "c:\\apache-maven-3.8.1\\bin\\mvn",
"maven.terminal.customEnv": [{
"environmentVariable": "JAVA_HOME",
"value": "c:\\openjdk-1.8.0_232-redhat",
}]
Of course, both paths should be pointed to proper folders/files in your environment.
The way I fixed was by changing the exec path in Vs Code settings from CMD to Powershell.
Open
settings -> features -> terminal
Change the windows exec path from cmd to where you have PowerShell.
For example: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Windows exec setting
On Tuesday Oct. 25, 2022 while trying to solve the same problem, I went through all of these previous solutions for Windows and finally had to resort to the official installation docs:
Installing Apache Maven
Unzipped the download and located the bin directory and manually added that directory location to my: System Properties>Advanced>Environment Variables>'Path'
For good measure, restart the computer.
I am unable to Install ANT, but i get install GIT, so i make a Folder in C Drive and paste the ANT unzipped files.
I have following location of installations,
1) JAVA: <D:\jdk1.7\bin>
2) GIT : <C:\Program Files\Git\bin>
AND
3) i created a Folder in C Drive for ANT : <C:\ant\bin>
Using Command prompt ,
i can execute the the command git , successfully . But when i put command ant, gives the following error
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre7\lib\tools.jar
Buildfile: build.xml does not exist!
Build failed
My environment variable set to the following path,
ANT_HOME = <c:\ant\bin>
JAVA_HOME = <d:\jdk1.7\bin>
Path = <%PATH%;%ANT_HOME%\bin;>
That's all , Thanks.
According to the information you provided, PATH variable should look like following:
%JAVA_HOME% ; %ANT_HOME% ; <other entries>
It seems that you're using Windows, thus you should check both your user-scope and system-scope environment variables (Computer - Properties - Advanced System Settings - Environment Variables).
Currently your build is failing due to JRE-related entry in PATH variable. Please remove it and retry.
Easiest way to check that the issue is resolved is to execute this from your command line:
javac -version
Normally this will give output like following:
javac 1.7.0_40
Install java and add it to class path by either creating java_home or directly to path variable in windows. Do the same thing for ant too. ie) place the ant location[upto ant\bin] in path variable and open the command prompt type ant . you will get the output as build failed which expects build.xml. Same will be applicable for git and other utilities...
hope it helps
i got the solution, Just download the project given in the link below ,
http://www.java2s.com/Open-Source/Java_Free_Code/Image/Download_Volley_demo_Free_Java_Code.htm
And import using Eclipse, and RUN. That's all . no need to make git reference. ( if you getting error continuously )
volley.jar you can use in your custom projects also.
Trying to setup jenkins but my builds fails with:
$ ant test
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
Build step 'Invoke Ant' marked build as failure
If I manually run "ant test" on the machine it works just fine and the JAVA_HOME is set to the exact same value. Any idea why it fails when jenkins try to run it ? Are there any more environment variabled involved ( I could not see any though ) ?
For others, I had to add the PATH /usr/bin/ to my PATH variable within Jenkins. (Find your correct path using which java).
Jenkins > Manage Jenkins > Configure System
Add an Environment Variable >>
e.g:
Name: PATH
Value: /usr/local/bin/:/usr/bin/
Screenshot
The problem was this, I had forgot to check the box "Restrict where this project can be run" in the project configuration. Thus the testing tried to execute on "master" where JAVA_HOME was not the same as expected on the build executor I intended to run it on. Thus where I tested and where it actually ran was different machines.
Set JAVA_HOME in your Jenkins system config in Jenkins 2.107.1.
open your jenkins, e.g. http://192.168.1.12:9090, then add /configure to the url,
that is http://192.168.1.12:9090/configure, then you can find like next:
For me the options above did not help, solved by creating a link to what's asked:
sudo ln -s ${actual_java_location} /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
actual_java_location can be read by this:
readlink -f $(which java)