netbeans-gradle-project
netbeans-gradle-project copied to clipboard
Make gradle plugin respect Netbeans Configurations
It seems the Gradle plugin ignores different Netbeans Configuration. Usually Netbeans runs the Default Profile, but one can create a new configuration (for example, "Headless" to run tests in headless mode) and set a system property in that configuration (like headless=true).
Even though the output shows the JVM is started with that property correctly set, it seems the plugin is not configured to pass that configuration to gradle, nor the application arguments for that matter.
The current workaround is setting that in the build file :
tasks.withType(Test) {
systemProperties System.getProperties()
args System.getProperty("exec.args").split()
}
It is my expectation that the plugin will honor the Netbeans Configuration settings out of the box when running the project (same behavior that Netbeans has with running maven or ant projects).
Thanks!
Can you describe what exactly you are doing (what you write into where) and your expectation (where do you expect to see what precisely)? Because I'm unaware any bug where configurations of other profiles are not honored.
Anyway, if you set a JVM argument, that is a JVM argument for the build script. That won't be passed to tasks which happen to spawn another Java process (like test). Also, I would recommend you to use project properties instead of using JVM arguments for performace reason.
What I'm doing exactly is:
-
My project has a dependency to openjfx-monocle to be able to run tests in headless mode (useful for running tests in Jenkins).
-
I go to the dropbox in Netbeans' toolbar that sets the project configuration (right of redo and undo menu buttons) and select "Customize..."
-
Click on add profile and name it "headless". Select that profile and go to Built-In Tasks, select "Test" in dropbox menu, unclick Inherit, in the text area for JVM Arguments I type: "headless=true" and finally click the "OK" button to save the changes.
-
I configure my tests to be aware of the expected headless system property with a @BeforeClass method:
@BeforeClass
public static void setupHeadlessMode() {
if (! Boolean.getBoolean("headless")) {
throw new RuntimeException("It should be headless!");
}
}
My expectation is that when I select "headless" in the dropbox menu in the Netbeans toolbar and run the tests, I should not receive the RuntimeException.
This won't work for two reasons:
- To set a system property, you have to add the JVM argument:
-Dheadless=true - This will only set the system property for the build script itself not for the started task. You have to forward the system property to your test tasks. Though I would recommend project properties instead of system properties (as that would be more efficient). To set one, you have to add "-Pheadless=true" in the arguments (not the JVM arguments).
Thanks for getting back to me. I made the mistake when I wrote this issue to forget the "-D" before the property (which is what I did use in my test). I tried to use the properties section as you suggested, but it does not work. Is there anything else I need to do? Thanks!
If you are using -Pheadless, you will have to forward it to the test task manually. For example like this:
if (project.hasProperty('headless') {
test.systemProperty 'headless', project.headless
}
Thanks for the answer. I don't know what I am doing wrong, but your suggestion does not work for me. I added a println to confirm the project has the headless property, but Boolean.getBoolean("headless") in my code always returns false.
This is the way this has to be done in all Gradle projects whether in NetBeans or not vim with Gradle. See:
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html
You have to configure the system properties for the test jvm which is not the one your Gradle build script is running in. A simple syntax I use in a lot of cases is:
systemProperties = System.properties
Others can be found here: http://stackoverflow.com/questions/21406265/how-to-give-system-property-to-my-test-via-gradle-and-d
Hope it helps,
Wade
On Jan 23, 2017 7:12 PM, "MarceloRuiz" [email protected] wrote:
Thanks for the answer. I don't know what I am doing wrong, but your suggestion does not work for me. I added a println to confirm the project has the headless property, but Boolean.getBoolean("headless") in my code always returns false.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kelemen/netbeans-gradle-project/issues/313#issuecomment-274660178, or mute the thread https://github.com/notifications/unsubscribe-auth/AAvjuhfVgzunaJjRZp3gEI48h4YAMbI8ks5rVUH6gaJpZM4LrPKG .
@wadechandler Thanks for your answer. That is exactly what I was doing and it was working. I was just trying to follow kelemen's suggestion to translate the project property (which is set correctly) to the system property just for the test. Do you happen to know why the other approach was not working? Thanks!
Ah, so, you would need to use -Pheadless=true versus -Dheadless, and then be sure and set systemProperty "headless" project.getProperty ("headless") to specifically pull the value from the project, and put it into the system property, and an if statement to make sure the value is set before doing it would make sure you don't set the property unless it is actually set. So, not sure why it wasn't working for you exactly, but probably a missing -P or something. Sorry I missed your system property was working.
Wade
On Jan 23, 2017 8:28 PM, "MarceloRuiz" [email protected] wrote:
@wadechandler https://github.com/wadechandler Thanks for your answer. That is exactly what I was doing and it was working. I was just trying to follow kelemen's suggestion to translate the project property (which is set correctly) to the system property just for the test. Do you happen to know why the other approach was not working? Thanks!
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kelemen/netbeans-gradle-project/issues/313#issuecomment-274673601, or mute the thread https://github.com/notifications/unsubscribe-auth/AAvjuoE7Vhw0lh7HHzVH6lR-F2pgxOuPks5rVVPEgaJpZM4LrPKG .
@MarceloRuiz Can you create a sample project and share it on Github (commit NB generated files as well except files within .nb-gradle/private/)? That way I can be sure what you are doing.
@kelemen
Hi Attila, Sorry I couldn't answer this before. Here is a sample project as you requested (not in github): sample project here Thanks, Marcelo.
You have set "-Pheadless=true" for the default profile (I assume this was not the case in your real project). Anyway, you only set "-Pheadless" for the "Build" action. You have to add this for every test related built-in task (test, test single, debug test single method, ...). Sadly, there is no way at the moment to define arguments passed for all built-in tasks (or possibly even custom tasks). Though, it wouldn't be too hard to add.
Thanks for getting back to me. In my real project I had a new profile for headless operation with the property set to each of those tasks you mentioned. Maybe I was too tried when I created the sample project. What I ended up doing is tweaking the gradle build file for test tasks and setting the system properties needed to run headless tests there.
The project you have linked works for me as expected and if I configure "-Pheadless=true" for the test command, it will print "It is headless!".
By the way, you are writting this:
tasks.withType(Test) {
testLogging {
showStandardStreams = true
}
if (project.hasProperty('headless')) {
test.systemProperty 'headless', project.headless
}
}
If you are doing it this way, you should remove the test. from your to refer to the test task to be configured.
Thanks Attila!