Set Environment Variable At Eclipse workspace level for all launchers? - eclipse

Can I set a workspace wide setting to add an environment variable to all future launchers created in the workspace?
Use Case
Our unit tests require an environment variable to guide the test to certain resources.
the variable varies with each version of our product
Options
- modify each junit launcher with the environment variable
- create start up script that sets variable and launches eclipse
- set globally
Ideally, I'd like to provide a way for users to set it once per workspace.
Does eclipse have a place to set an environment variable for all launchers?
Thanks
Peter

Only option which I can think of is similar to yours where you can create multiple shell scripts(linux) or batch files(windows) and set it up there. for ex -
I am giving examples for windows environment and same can be done for Linux as well -
for windows-
eclipse.exe -DvariableName1=value1 -DvariableName2=value2
As you want to pass different variables to different workspaces, you can also pass location of your workspace as part of arguments.
eclipse.exe -data <your_workspace_location> -DvariableName1=value1 -DvariableName2=value2
You can create multiple shortcuts of eclipse.exe in windows and place them on Desktop (for quick access) if needed. Each shortcut may point to a similar variant of above command with different workspace and different variables.
Hope this helps. Happy coding :)

I have a different approach for doing this on windows. This solution allows you to store a large number of environment variables to one place and apply them globally inside eclipse.
I have gitBash installed already, and associated in windows to execute .bash files
I set up a /c/Temp/.env file with lots of environment variables in the format
export VARIABLE=my value
I create an eclipse_startup.bash script that looks like this (and pin it to my start menu).
echo setting up env
. /c/Temp/.env
echo starting eclipse
/c/Users/me/eclipse-oxygen/eclipse/eclipse.exe
It has the disadvantage that I end up with a bash window open with eclipse, and I have to restart after changing the environment. On the plus side, my complex application has lengthy environment files already, so I can simply load them in and start.

Related

How do I run a project in eclipse with different jboss-ejb-client.properties

I have EJBs deployed on several different servers, for different environments. I have many projects that use these EJBs. I usually just run my projects against the DEV server EJBs, but sometimes I need to run against the TEST or PROD environment EJBs. This necessitates having to comment out all of the DEV nodes in my jboss-client-ejb.properties file and uncomment all of the TEST nodes. But then if I forget to change them back, I may mess up some data if I run it later. What I would like to do is create a different runtime configuration for each environment, and have each runtime config use a different version of the jboss-client-ejb.properties. Is there a way to do this? If so how? I have looked at all of properties of a run configuration, and don't see anything helpful.
In eclipse preferences search for string variable substitution. Here create variables that point to multiple config files for each of your environments. Then create multiple run configurations and for each one (like dev or prod) add a program argument that points to your string variable defined in your preferences like this -DmyconfigFile={$MyDevPropertiesFilePath}, or you could hard code the config path and have multiple runtime configurations that use different config files. Key point here is create multiple runtime launch configurations for each environment and add the properties for each environment that point to the config file respective to each environment. This way you can easily select the launch menu and decide to run "dev" "prod" or whatever you name your multiple configurations. Trying to do this with one runtime configuration will cause pain as you say, because it is easy to forget to revert or change the config file you want to to use. Hope that helps. Also if you create a new workspace you can export your runtime configurations using the export wizard which is also helpful for passing on to other developers or putting in source control.
P.S Looking more at your question you wan to pass in the config file path as a program argument, you are correct there are no specific options for setting this file path. Using program arguments with multiple launch configurations.

Add existing make environment to eclipse

I have a question about eclipse and make. I have created a make environment which acts as follow:
I have a batch file (Start.bat) which SET´s variables for further usage (e.g. SET TOOL_PATH = blabla).
After calling this I use my make.bat <option>, which calls the make.ext -f Makefile.mak <option> with the given option e.g. make.bat all -sj4. The environment variables which are set in Start.bat are very important for the running of the overall make environment.
This works very fine and now I would like to import this in eclipse but I am not familiar with eclipse. I need from eclipse that when I would like to run a make, first the Start.bat is called and then the make.bat <option> is called.
What I need to do?
You can import your project as 'existing make file project'
In the project settings you should be able to define pre-biuld steps and call the batch there or define a specific target (e.g. start) that calls the start batch and is executed before your 'make'.
I dont know exactly at the moment how to do this the best way, but I think you are fast into it.

Best way to set environment variables in NetBeans Platform App

I would like to set some environment variables for a NetBeans Platform app. Is there a clean way of doing this so the environment variables are set when I start the app without having to write a custom startup script? I'm doing this on a linux environment.
https://netbeans.org/features/platform/
If you want to pass arguments/commandline parameters to the NetBeans Platform app during startup, you can pass those parameters either directly to the launcher or via the etc/<brandingname>.conf file which is located in the distribution of the application. There you can determine any options via the default_options attribute. Example-
default_userdir="${HOME}/.${APPNAME}/user"
default_cachedir="${HOME}/.${APPNAME}/cache"
default_options="-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-ea --branding mybrandingcluster"
jdkhome="jre"
Furthermore, you can separately assign the path of the Java
Platform, the user directory, and additional clusters in this file.
If you are looking forward to save environment variables specific to your application you can use the NbPreferences API. Detailed example is listed in Geertjan´s blog

PyCharm - automatically set environment variables

I'm using virtualenv, virtualenvwrapper and PyCharm.
I have a postactivate script that runs an "export" command to apply the environment variables needed for each project, so when I run "workon X", the variables are ready for me.
However, when working with PyCharm I can't seem to get it to use those variables by running the postactivate file (in the "before launch" setting). I have to manually enter each environment variable in the Run/Debug configuration window.
Is there any way to automatically set environment variables within PyCharm? Or do I have to do this manually for every new project and variable change?
I was looking for a way to do this today and stumbled across another variation of the same question (linked below) and left my solution there although it seems to be useful for this question as well. They're handling loading the environment variables in the code itself.
Given that this is mainly a problem while in development, I prefer this approach:
Open a terminal
Assuming virtualenvwrapper is being used, activate the virtualenv of the project which will cause the hooks to run and set the environment variables (assuming you're setting them in, say, the postactivate hook)
Launch PyCharm from this command line.
Pycharm will then have access to the environment variables. Likely because of something having to do with the PyCharm process being a child of the shell.
https://stackoverflow.com/a/30374246/4924748
I have same problem.
Trying to maintain environment variables through UI is a tedious job.
It seems pycharm only load env variables through bash_profile once when it startup.
After that, any export or trying to run a before job to change bash_profile is useless
wondering when will pycharm team improve this
In my case, my workaround for remote interpreter works better than local,
since I can modify /etc/environment and reboot the vm
for local interpreter, the best solution I can do are these:
1. Create a template Run/Debug config template and clone it
If your env variables are stable, this is a simple solution for creating diff config with same env variables without re-typing them.
create the template config, enter the env variables you need.
clone them
see picture
2. Change your script
Maybe add some code by using os.environ[] = value at your main script
but I don't want to do this, it change my product code and might be accidentally committed
Hope someone could give better answer, I've been spent too much time on this issue...
Another hack solution, but a straightforward one that, for my purposes, suffices. Note that while this is particular to Ubuntu (and presumably Mint) linux, there might be something of use for Mac as well.
What I do is add a line to the launch script (pycharm.sh) that sources the needed environment variables (in my case I was running into problems w/ cx_Oracle in Pycharm that weren't otherwise affecting scripts run at command line). If you keep environment variables in a file called, for example, .env_local that's in your home directory, you can add the following line to pycharm.sh:
. $HOME/.env_local
Two important things to note here with respect to why I specifically use '.' (rather than 'source') and why I use '$HOME' rather than '~', which in bash are effectively interchangeable. 1) I noticed that pycharm.sh uses the #!/bin/sh, and I realized that in Ubuntu, sh now points to dash (rather than bash). 2) dash, as it turns out, doesn't have the source "builtin", nor will ~ resolve to your home dir.
I also realize that every time I upgrade PyCharm, I'll have to modify the pycharm.sh file, so this isn't ideal. Still beats having to manage the run configurations! Hope it helps.
OK, I found better workaround!
1.install fabric in your virtualenv
go to terminal and
1. workon your virtualenv name
2. pip install fabric
2. add fabric.py
add a python file and named it "fabric.py" under your project root, past the code below,and change the path variables to your own
from fabric.api import *
import os
path_to_your_export_script = '/Users/freddyTan/workspace/test.sh'
# here is where you put your virtualenvwrapper environment export script
# could be .bash_profile or .bashrc depend on how you setup your vertualenvwrapper
path_to_your_bash_file = '/Users/freddyTan/.bash_profile'
def run_python(py_path, virtualenv_path):
# get virtualenv folder, parent of bin
virtualenv_path = os.path.dirname(virtualenv_path)
# get virtualenv name
virtualenv_name = os.path.basename(virtualenv_path)
with hide('running'), settings(warn_only=True):
with prefix('source %s' % path_to_your_export_script):
with prefix('source %s' % path_to_your_bash_file):
with prefix('workon %s' % virtualenv_name):
local('python %s' % py_path)
3. add a external tool
go to
preference-> External tools -> click add button
and fill in following info
Name: whatever
Group: whatever
Program: "path to your virtualenv, should be under '$HOME/.virtualenvs' by default"/bin/fab
Parameter: run_python:py_path=$FilePath$,virtualenv_path=$PyInterpreterDirectory$
Working directory: $ProjectFileDir$
screenshot
wolla, run it
go to your main.py, right click, find the external name (ex. "whatever"), and click it
you could also add shortcut for this external tool
screenshot
drawbacks
this only work on python 2.x, because fabric don't support python 3

Modify Eclipse RCP startup args in protected directory

I am enabling internationalization for my RCP application. The preferences tab allows the user to select between languages. I understand that Eclipse (3.7) has to load the language at start-up and can not dynamically change languages.
I know of three approaches to accomplish this:
1) Modify the OS level shortcut to pass in -nl XX
2) Change the app.ini file to have -nl XX (on separate lines)
3) Change the config.ini to have osgi.nl = XX
The issue with these approaches is that they all require write permission to the application directory. When running under Vista / Windows 7 and Linux implementations that do not provide write access to programs, a normal user does not have permissions to modify these files.
Is there another approach to pass in arguments that change the VM language? Is there a workaround for the file protection provided by the OS?
In Windows the application directory is write-protected for a good reason. Fortunately one can set Eclipse Runtime Options to configure where the RCP application should store
configuration data
workspace data
This can be accomplished in two ways:
setting command line arguments (-configuration , -data)
defining system properties (osgi.configuration.area to , osgi.instance.area to ) for example in config.ini
For further information see Runtime Options in official Eclipse Help.
In Windows such data should be stored in the user directory.
By the way you should be able to locate these settings in the Installation Details pane of the standard About dialog.
However setting these properties is a bit tricky. In my case the application installer evaluates the location of the user directory at installation time and modifies the config.ini file accordingly.