-
f you don't have maven installed (type mvn on terminal to check whether it exists), install maven. On Mac use
brew install maven
-
First time you run any maven command it will download lots of dependencies and print lots of download statements on console. Second time onwards, it will be faster and won't fill up your screen.
-
Create a new Java project with maven
mvn archetype:generate
-
Select default options for for first two questions. Just hit enter twice.
-
Enter artifact-id (com.shital) and group id (tutorial).
-
Select default value for version and package.
-
Confirm all properties.
|-pom.xml
|-src
| |-test
| | |-java
| | | |-com
| | | | |-shital
| | | | | |-AppTest.java
| |-main
| | |-java
| | | |-com
| | | | |-shital
| | | | | |-App.java
-
Using compile and exec plugins of maven. You need to specify the main class in the argument as follows:
mvn compile exec:java -Dexec.mainClass="com.shital.App"
-
Put main Class in pom.xml so that you don't have to specify it at command line. Insert following inside tag in pom.xml
<plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.shital.App</mainClass> </configuration> </plugin> </plugins>
-
Now you can simply type following to run your program. No need to specify main class
mvn compile exec:java
-
Maven creates all classes inside target/classes dir. To simply run using java commmand, do following in the base dir
cd target/classes java com.shital.App
-
Now run all uni tests. Check dummy test written in src/test/java/com/shital/AppTest.java
mvn test
-
You should see following on your console. This means all tests passed and there are no test failures.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0