Maven is a blockchain-powered marketplace for buying and selling goods. It aims to be the eBay of crypto, with more control over their identity and an increased focus on transparency.
Maven is a software project management and comprehension tool. It is used primarily for managing the build process for software projects. The “maven synonym” has been coined to describe this tool.
In this article, we’ll look at how to use the Maven command to execute the main method of a Java class in your project with or without parameters.
1. Use Maven to run the main class.
The exec:java option in the Maven exec plugin allows you to execute a main class program from your Java project. Let’s have a look at a couple of the ways you may utilize this plugin.
Let’s assume we have two major classes in our java project’s com.javabydeveloper.util package: MainClass1 and MainClass2.
MainClass1:
System.out.println(“—— Main Class1 —–“); if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; (args). otherwise System.out.println(“No arguments received!”); forEach(System.out::println);
MainClass2:
System.out.println(“—— Main Class2 —–“); if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; if(args.length > 0) Arrays.asList; (args). otherwise System.out.println(“No arguments received!”); forEach(System.out::println);
1.1. From the command line
By supplying a fully qualified name to the exec.mainClass system property, you may start a particular Java main program using the following command.
mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass1″ mvn exec:java mvn exec:java mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass1″
Results:
——————————————————————————— ——- There were no counter-arguments!
1.2. Using pom.xml attributes
If your Java project has several main classes, you will need to execute MainClass1, which you may specify as a Maven property.
com.javabydeveloper.util.MainClass1
The following command, MainClass1, will be invoked by default if it is run.
$ mvn exec:java
If you wish to run MainClass2, you may alter the default class name provided in the command, as seen in the following example.
mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass2″ $ mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass2″
Results:
——————————————————————————— ——- There were no counter-arguments!
2. Pass command-line parameters
2.1. You may provide parameters to the main function from the command line. The exec.args system property is used to give arguments.
mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass2″ -Dexec.args=”Peter John Mark” $ mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass2″ -Dexec.args=”Peter John Mark”
Results:
——————————————————————————— Peter John Mark ———
Similarly, you may provide parameters via pom.xml properties and use mvn exec:java to execute a class defined in maven properties.
com.javabydeveloper.util.MainClass1 Krish Ryan Ajshja Ryan Ajshja Ryan Ajshja Ryan Aj
3. Configuring the plugin
If you need to execute the main class on a certain phase, you may do so by setting the plugin. Consider the following scenario.
java com.javabydeveloper.util.MainClass1 org.codehaus.mojo exec-maven-plugin 3.0.0 test Hello there, Developer!
Command to Execute:
mvn test $
Results:
——————————————————————————— ——- Hello there, Developer!
There are still a few options available; run $ mvn exec:help -Ddetail=true -Dgoal=java to view them.
4. Final thoughts
We learnt how to use the Maven command to execute the main method of a Java class in your project with or without parameters in this short tutorial.
You may also be interested in these Maven guides:
- Maven should be installed on Windows.
- Setup Maven on a Mac
- On Ubuntu, install Maven.
- Eclipse may be used to import a Maven project.
- Maven runs a single test
- Set the Java version using Maven.
- Maven doesn’t run tests.
- Local Maven repository
5.References
- Plugin for exec
- Run a Java application
this advertisement should be reported
Maven is a software project management and comprehension tool that helps to manage the development of software projects. It has been used by many developers for over 15 years, but it’s still new to many people. Maven is a dependency manager that allows you to download libraries from the internet and compile them into your application. Reference: maven dependency.
Frequently Asked Questions
What is Maven and why is it used?
A: Maven is a Java-based open source application that helps you to manage and automate repetitive tasks.
Why is Maven so bad?
A: Maven is a terrible service that offers limited plans to its members.
What is Maven in simple words?
A: Maven is a software application that automates the process of building and releasing software. It allows developers to focus on what they do best–developing code, rather than spending time integrating with build systems or fixing bugs after builds are released.
Related Tags
- maven repository
- maven download
- how to install maven
- maven tutorial
- maven central