No Relation To Blog
Subscribe to the personal musing of Emmanuel Bernard
Setting global environment variables in IntelliJ IDEA and other test config goodies
When you run a test from IntelliJ idea, you can customize some of
the settings by selecting the list of tests and click Edit Configurations.
From there you can change things like:
- under which module classpath the test is run
- the working directory the test should run from
- virtual machine parameters
- specific environment variables to use
In Hibernate OGM, we use the same test site for all NoSQL solutions. Changing the module classpath is useful in this situation. We also let you refine via an environment variable which hostname runs say MongoDB.
Unfortunately in Mac OS X, graphic applications do not inherit your .bash_profile config. I know, stupid,
but what can you do against the Empire? Workaround!
You can set environment variables from each test but it gets tedious quickly. Alternatively, you can set
an environment variable globally in IntelliJ - what they call parent environment variables.
Go to Preferences and search for Path Variables.
Set your global environment variables here and you are good to go. Note that you can ask a test not to
inherit these global variables if you want to - in the test configuration.
Update:
It turns out I was wrong. Paths Variables is not where global environment variables can be set. If you
know where, please let me know.
Update 2:
To set an environment variable visible by applications in Mac OS X, use
launchctl setenv MYPATH myvar
then restart your IDE (you might need to log out and back in as well).
Thanks @aagahi for the info. This is not great as it's remote from my IDE but at least that works.
BTW, launchctl is supposed to set the data in /etc/lanuchd.conf or $HOME/.launchd.conf. Not on my
system. If anyone knows where the data is put, I'm interested.
Notes to JetBrains:
- your environment variable UI is plain counter productive: either give us a text free area or make tabs and co working
- an option to inherit the
.bash_profileconfiguration would be awesome :)
Mac OS X and IntelliJ crashes
It seems it was cause by a couple of things.
Job scheduler crashThread 55 Crashed: Java: JobScheduler pool 1/2
-XX:-ReduceInitialCardMarks
<key>VMOptions</key> <string>-XX:-ReduceInitialCardMarks -Xms128m -Xmx512m -XX:MaxPermSize=250m -ea -Xbootclasspath/a:../lib/boot.jar</string>Font corrupted
Removing fonts from ~/Library/Fonts solved the issue. What you can do is move them to a different directory and slowly re-inject them to find the culprit.
I am quite happy all this is behind me. I chose:
- A Java VM
- On Mac OS X
- with IntelliJ IDEA
to precisely avoid this kind of bad experience and time waste :) Back to normal.
IntelliJ's Live Template
If you remember, I like to write my getters this way (long story here)
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private String name;
I have been doing it manually for a while but it turns out IntelliJ has a super nice feature for that: Live Templating which allows you to use parameterized and contextual templates to generate code.
Go to your Settings->Live Templating and create a new one.
Add this template:
public $TYPE$ get$UpperName$() { return $lowername$; }
public void set$UpperName$($TYPE$ $lowername$) { this.$lowername$ = $lowername$; }
private $TYPE$ $lowername$;
Then edit the variables:
- TYPE: the expression should be classNameComplete() to refer to the class name context
- lowername: the expression should be decapitalize(UpperName). Also tick skip if defined to not have to validate the computed value (this saves you one key stroke).
I've named my Live Template "get". When I type "get" and TAB, I am asked to type the property type (which is suggested). The second variable asked is the capitalized property name. Then the Live Template infers the rest (decapitalize my property name to fill up lowername).
Pretty nice feature!