How To Use Maven Command To Create Java Project

Maven is a popular java project build and management tool. It is open-source and totally free. The key concept of maven is POM (Project Object Model). In every maven project, there is a pom.xml file that contains project information, source directory, target build directory, test scripts directory, etc.

The most important data in pom.xml is that it defines all the project used libraries ( jars ) and their versions. So when some other developers want to initiate the project, he just needs to run one maven command then all the project needed jars will be downloaded and used in his local project. This can make java libraries’ dependency management easy.

Before a detailed introduction, we will show you how to set up maven and how to create a java project using the maven command line.

1. Steps To Install Maven On Windows.

  1. Install Jdk1.7 or higher. If you have installed it, you can run the command java -version in a command prompt window to ensure it.
  2. Setup JAVA_HOME system environment variable.
  3. You can read How To Install Jdk Correctly if you do not know how to do the above steps.
  4. Go to http://maven.apache.org/download.cgi and choose the latest stable maven build to download.
  5. Extract the zip file to a local folder. Such as C:\WorkSpace\dev2qa.com\Tool\apache-mvn-3.5.0
  6. Setup M2_HOME, MAVEN_HOME, M2, MAVEN_OPTS system environment variable. M2_HOME‘s value is C:\WorkSpace\dev2qa.com\Tool\apache-mvn-3.5.0. MAVEN_HOME‘s value is the same as M2_HOME. M2’s value is %M2_HOME%\bin. MAVEN_OPTS‘s value is -Xms256m -Xmx512m.
  7. Append maven bin directory to system environment variable PATH then you can run maven command directly in your command prompt. Because M2‘s value is %M2_HOME%\bin so we append ;%M2% to the end of the PATH variable.
  8. Run the command mvn -version in dos prompt, when you see the below output, it means the installation success.
    Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
    Maven home: C:\WorkSpace\Tool\apache-maven-3.8.1-bin\apache-maven-3.8.1\bin\..
    Java version: 1.8.0_131, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_131\jre
    Default locale: en_US, platform encoding: Cp1252
    OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

2. Maven Repository Introduction.

Maven Repository is a place such as a directory. You store all your project-related source files, libraries, and all other resources in it. There is three kinds of maven repository.

  1. Local maven repository.
  2. Central maven repository.
  3. Remote maven repository.

2.1 Local Maven Repository.

The local maven repository is a directory in your local machine. It stored all your project-related jars, plugins, source files. When you initially execute the maven command, it is created. When you build your java project, all the dependency jars will be downloaded from the central maven repository and saved into the local maven repository. The subsequent build will use the jars stored in the local maven repository only. This can reduce build cost time when you build your project very often. You can also change it by update the %M2_HOME%\conf\settings.xml file like below.

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
   -->
  <localRepository>C:\WorkSpace\MvnRepository</localRepository>

As you can see in the above code, the default local maven repository is ${user.home}/.m2/repository, you can change it to your local maven repository directory such as C:\WorkSpace\MvnRepository.

2.2 Central Maven Repository.

The central maven repository is Apache community managed. There are almost all java libraries that exist in it. You can browse it at https://repo1.maven.org/maven2/. When building a project, if it can not find any dependent jars in the local maven repository it will search in the central maven repository and download those jars if found. You can also search for an artifact in http://search.maven.org if you want to download the artifact’s pom or jars.

2.3 Remote Maven Repository.

The remote maven repository is used when the dependent jars can not be found in both local and central maven repositories. You can store your java project-specific required jars in it. Below is an example of the remote maven repository.

   <repositories>
      <repository>
         <id>dev2qa.lib1</id>
         <url>http://download.dev2qa.com/mav_repo/lib1</url>
      </repository>
   </repositories>

2.4 Dependence Jars Search Order In Maven Repository.

  1. Search local maven repository.
  2. If not found in step1 then search the central maven repository if found then download to local.
  3. If not found in step2 then search remote maven repository if specified, if found then download to local.
  4. If not found in all the above steps, an error will be thrown.

3. Steps To Create Your First Maven Java Project.

There is a maven-archetype-quickstart archetype that we can use to create a java project. It is just a java project template.

  1. Open a command prompt and create folder C:\WorkSpace\MvnExampleProject.
  2. Run the below command in the opened window.
    cd C:\WorkSpace\MvnExampleProject
    mvn archetype:generate -DgroupId=www.dev2qa.com -DartifactId=dev2qaExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  3. Then you can see the maven java project build process output in the dos window. When you see a green BUILD SUCCESS message, it means the java project has been created and initialized successfully.
  4. After that, you will find there add a lot of artifact jar folders under your local repository folder.
  5. The maven java project folder structure has also been changed. You will find all the java source files are saved in src/main/java and all unit testing java source files are saved in src/test/java.
    dev2qaExample
       |-src
       |---main
       |-----java
       |-------com
       |---------dev2qa
       |-----------example
       |-------------App.java
       |---test
       |-----java
       |-------com
       |---------dev2qa
       |-----------example
       |-------------AppTest.java
       |-pom.xml
  6. The pom.xml content is something like below. You can find related parameter values when you run the mvn command to create this project such as groupId, artifactId, name, etc. You can also add dependency if you need new jars in your java project. Because the example uses JUnit as a test framework, so it adds JUnit artifact in pom.xml.
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>Dev2QaExample</groupId>
      <artifactId>Dev2QaExample</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>apache-poi</name>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
          <resource>
            <directory>src</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
          <resource>
            <directory>resources</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
      
      <dependencies>
        
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>
         
           
      </dependencies>
      
    </project>
  7. There is also App.java and AppTest.java created in the example. You just need to add source code in App.java and test code in AppTest.java.
  8. If you remove the content in the C:\WorkSpace\MvnExampleProject folder and run the maven create command again, there will not any artifact download log output in the console, because all artifacts that this project needed has been downloaded in the local maven repository. So do not need to download it again.
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