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 maven configuration:
From the main menu, select Run | Edit Configurations or choose Edit Configurations from the run/debug configurations selector on the toolbar. In the Run/Debug Configurations dialog that opens, select a configuration where you want to pass the arguments. Type the arguments in the Program arguments field.
You could run: mvn exec:exec -Dexec. args="arg1". This will pass the argument arg1 to your program. By using the -f parameter, you can also run it from other directories.
From the main menu, select Run | Edit Configurations to open the run/debug configuration for your project. In the list that opens, select Run Maven Goal. In the Select Maven Goal dialog, specify a project and a goal that you want to execute before launching the project. Click OK.
Maven will use the value (2.5) passed as an argument to replace the COMMON_VERSION_CMD property set in our pom. xml. This is not limited to the package command — we can pass arguments together with any Maven command, such as install, test, or build.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With