Build And Run Java Project With Maven

This article will tell you how to use the maven command to build and run a java project. You will learn the below tasks in this article with examples.

  1. How to build and run maven project.
  2. How to add your java file into the maven project.
  3. Add TestNG test framework jar files in this maven project.

1. Build & Run Maven Project.

    1. Go to the project folder, in my environment it is C:\WorkSpace\MvnExampleProject\dev2qaExample
    2. Open a command prompt and run the command mvn clean package. The argument clean means remove all existing java class files and recompile all the java source files. The argument package means to put all the class files into a jar file.
      >C:\WorkSpace\Tool\apache-maven-3.8.1-bin\apache-maven-3.8.1\bin\mvn clean package
      [INFO] Scanning for projects...
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD FAILURE
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time:  0.630 s
      [INFO] Finished at: 2021-04-20T08:56:10+08:00
      [INFO] ------------------------------------------------------------------------
      [ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\Users\zhaosong). Please verify you invoked Maven from the correct directory. -> [Help 1]
      [ERROR]
      [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
      [ERROR] Re-run Maven using the -X switch to enable full debug logging.
      [ERROR]
      [ERROR] For more information about the errors and possible solutions, please read the following articles:
      [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
      
    3. The above command execute failed, it is because the current working directory does not has maven java project files.
    4. When the above command finished successfully, you will see there has a target folder under the project.
    5. Open the target folder, you will find the maven java project target jar file has been generated. In this example, the jar file name is dev2qaExample-1.0-SNAPSHOT.jar. The project build jar file name format is project-name-version-SNAPSHOT.jar.
    6. Open a terminal and run the command java -cp ./target/dev2qaExample-1.0-SNAPSHOT.jar com.dev2qa.example.App under the project folder. The argument -cp is the abbreviation of classpath which means find the java class in the after jar file. You can see the java application output text Hello World in the terminal.

2. Add Other Java File To Maven Project.

  1. Create AppUtil.java file as below. This is a util class which has one method that prints out the welcome message.
    package com.dev2qa.util;
    
    public class AppUtil 
    {
        public void welcomeToMavenWorld( String userName )
        {
    	StringBuffer strBuf = new StringBuffer();
    	strBuf.append("Hello ");
    	strBuf.append(userName);
    	strBuf.append(", welcome to maven world.");
            System.out.println( strBuf.toString() );
        }
    }
  2. Because it is in package com.dev2qa.util which is different from com.dev2qa.example, so we should save it in the folder C:\WorkSpace\MvnExampleProject\dev2qaExample\src\main\java\com\dev2qa\util.
  3. Now you can see there are two folders under the dev2qa folder, one is the original example folder, the other is the created util folder.
  4. We also need to edit the App.java file which exists in src/main/java/com/dev2qa/example folder to use the AppUtil class we just created.
    package com.dev2qa.example;
    
    import com.dev2qa.util.*;
    
    public class App 
    {
        public static void main( String[] args )
        {
            AppUtil appUtil = new AppUtil();
    	appUtil.welcomeToMavenWorld("Jerry");
        }
    }
  5. Run the command mvn -clean package again to build and package the maven project jar file.
  6. Open a terminal and run the command java -cp ./target/dev2qaExample-1.0-SNAPSHOT.jar com.dev2qa.example.App under project folder again. You will see the different outputs by AppUtil class as below.
    java -cp ./target/dev2qaExample-1.0-SNAPSHOT.jar com.dev2qa.example.App
    Hello Jerry, welcome to maven world.

3. Add TestNG jar File To Maven Java Project.

  1. The default test framework for this maven project is JUnit. And we want to use testNG to replace it.
  2. Go to http://testng.org/doc/download.html. You will see there are remote repository and dependency XML segments in the below maven section.
    <repositories>
      <repository>
        <id>jcenter</id>
        <name>bintray</name>
        <url>https://jcenter.bintray.com</url>
      </repository>
    </repositories>
     
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.10</version>
      <scope>test</scope>
    </dependency>
    
  3. Copy the above XML into your local maven java project pom.xml. The testNG <repositories> XML segments should be placed under the pom.xml <project> XML element, the testNG <dependency> XML segments should be placed under the pom.xml <dependencies> XML element.
  4. Run the command mvn clean package in the terminal again. You will find maven will download testNG jar files from its center repository and save those jars into the local repository.

Reference

  1. How To Use Maven Command To Create Java Project.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x