Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 611
kal Avatar asked Feb 16 '17 12:02

kal


People also ask

How do I add command line arguments in IntelliJ?

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.

How do I run a Maven project using command line arguments?

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.

How do I use Maven command in IntelliJ?

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.

What are Maven arguments?

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.


1 Answers

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.

like image 189
CrazyCoder Avatar answered Oct 16 '22 18:10

CrazyCoder