How to specify a global system property for java applications?
Asked Answered
E

2

6

I'm trying to integrate Cobertura into our test environment to be able to capture code coverage information. The tests are run from ant scripts using ANT junit tasks, and there are more than 50 ant script files (build.xml) with one or more junit task definitions.

As per the Cobertura doc, the junit tests have to be given the path of the cobertura coverage data file through the sysproperty nested element of the junit task. What it means to me is that I will need to update all those 50+ files to specify a sysproperty element for the junit tasks, which I plan to do as a last resort.

I'm running my tests in Linux (CentOS) and I would like to know if there is a way to specify a global system property (-Dxxx=yyy) so that the system property is made available to all java applications running in the system.

-EDIT-
The junit tests are run in a forked JVM. <junit fork="yes" ...

Entreaty answered 16/11, 2011 at 20:43 Comment(2)
Depending on which ide you use, it should be pretty straightforward to refactor them quickly. Also you can try the ant input task, if that will help.Buber
@Buber As I'd mentioned, that's the way I'm going to take if I don't find another simple solution which doesn't require modifying the scripts.Entreaty
L
8

You can try to set ANT_OPTS environment variable.


ANT_OPTS - command-line arguments that should be passed to the JVM. For example, you can define system properties or set the maximum Java heap size here.


-EDIT-

You have to set _JAVA_OPTIONS environment variable. Here and here there is some information about it.

Larimer answered 16/11, 2011 at 21:8 Comment(2)
+1 Thanks. It works if I run the tests within the same VM, but I need to run my tests in a forked VM. I have updated the question.Entreaty
Just updated my answer. You have to set _JAVA_OPTIONS environment variable to define default JVM properties.Larimer
D
0

alternatively set environment variable and in your ant script use :

<property environment="env"/>
<echo>${env.yourEnvVariable}</echo>
...

The advantage over using ANT_OPTS (which is primarily meant for ant internal settings, like f.e. VM parameters..) is the possibility for userspecific settings if using user environment variables whereas ANT_ARGS is "static" for all ant scripts.

Detain answered 17/11, 2011 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.