Search code examples
javamavenintellij-ideaintellij-14

how to add program arguments for maven command line project in intellij?


I am new in intellij. I would like to add program arguments for my maven command line application so that they can be used in main(String[] args) method, such as: -pg 541, which specified some method to be triggered in my main function.

I was tried to do it using Maven configuration (Run/Debug Configurations) by adding arguments directly in Command line section but it didn't succeed.

I tried also JUnit which runs Maven project but the program arguments section was disabled.

Here is a snapshot of what I tried.

using junit configuration:

using junit configuration

using maven configuration:

using maven configuration


Solution

  • JUnit tests are executed via the test runner framework, your main method is not called at all by JUnit, therefore you can't supply program arguments this way.

    To pass the arguments to the application via its main method you have to use Application Run/Debug configuration type in IDEA.

    If you want to pass parameters to the unit tests, consider using VM Options field instead, like -Dparam=value and in the test method you can read it with String value = System.getProperty("param");

    Maven supports it as well using argLine in some unit test plugins.