How to create a Java Maven project using Command Line in IntelliJ

HOME

In the previous tutorial, I have explained How to create Maven Java project in Intellij without Command Line. In this tutorial, I will explain about creation of Java Maven Project using Command Line in IntelliJ.

Step 1 – Open IntelliJ IDEA IDE. Click on Terminal Button as shown in the below image.

Step 2 – Go to the path where you want to create the project. This can be done by typing command

cd path of folder

Step 3 – This tells Maven to generate a Java project from a Maven template.

mvn archetype:generate

Step 4 – We need to mention the number as displayed on your screen in Command Prompt to proceed further. Like here, Choose a number or apply filter has 1771, so I have also mentioned 1771 in command prompt.

Step 5 – We need to provide again input in command prompt. This time program wants to know which version we want to use. I prefer to use the latest version. Here, it is 8, so I have selected version 8.

Step 6 – We need to provide 2 input here

A) Value of groupId – This serves as the group identifier of your Maven project, it should be in a form similar to Java packages, such as com.example
B) Value of artifactId – This serves as the group-local identifier of my Maven project like MavenIntelliJFromCMD
C) Value of Version – The initial version of our project. The default is 1.0-SNAPSHOT
D) Value of package – The name of our root package. The default is groupId we have created earlier.
We will notice the INFO message about the properties.

If the displayed settings are correct, then just enter Y in :: prompt.

Step 7 – Successful Build – Below screenshot shows that the Maven IntelliJ Project built successfully.

Step 8 – Contents of Project Folder – Open folder MavenIntelliJFromCMD to see the contents of the folder. It should have POM file and src.

This project can be imported in IntelliJ and can be used further. To know how to import Java Maven project in Intellij, click here.

That’s it! We have created a Maven Project in IntelliJ using Command Line.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

How to import Java Maven project in IntelliJ

Home

In the previous tutorial, I have explained How to create a Maven Project in IntelliJ. In this tutorail, I will explain how we can import a Java Maven project in IntelliJ IDE.

Step 1 – Open IntelliJ IDEA and Welcome Screen appears. Click Open button present on Welcome Screen.

Step 2 – Navigate to your Maven project and select the top-level folder. Select the project you want to Import. As you can see, I have selected – MavenProjectFromCMD option. Select OK button to proceed to next screen.

Step 3 – A screen appears to Open or Import project. It will have all the possible configurations for the project. As this is a Maven project, select Maven project and click OK Button.

Step 4 – A warning message box will appears. Select Trust Project Button and move forward.

Step 5 -This screens shows that the project is imported.

Step 6 – Run the test present in the project. Here, I have run AppTest. Right click on AppTest ->Run ‘AppTest’. Below screen shows that the project is imported successfully.

That’s it! So Simple!!!

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to create Maven project in IntelliJ

HOME

In the previous tutorial, I have explained how to create a Simple Java project in IntelliJ IDEA IDE. In this tutorial, I will explain about creating a Maven project in IntelliJ IDE.

Step 1 – Open the IntelliJ. It will look as shown below. To create a New Project, click on New Project Icon.

Step 2 – Click on File Option, hover on New Option and click on Project Option as shown below.

Step 3 – Select New Project as Maven.
Project SDK should be current Java version available.
Select option for Create from archetype. This will enable the options to select archetype.
Select archetype : maven-archetype-quickstart.
Click on the Next Button.

Step 4 – Below screen will appear. Mention the Name, Group Id, Artifact Id and Version . Click the Finish button

Name : MavenIntelliJDemo
Group Id : com.example
Artifact Id : MavenIntelliJDemo
Version : 1.0-SNAPSHOT

Step 5 – Verify the information in the below screen. Click on Finish Button.

Step 6 – This dialog box will appear on the screen. This provides you the option to open the project in current window or will open a new window with this project. I’m selecting the option – New Window.

Step 7 – This screen shows the structure of Maven project as well as POM.xml file.

Step 8 – Project Folder Creation – We can see a folder with the name of project – MavenIntelliJDemo in our Eclipse Workspace.

This is how we can create the Maven project – MavenIntelliJDemo in IntelliJ.

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Maven Tutorials

 HOME

 

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is a part of the Apache Software Foundation.

Maven Installation

 Chapter 1 Maven – How to install Maven on Window

Eclipse IDE

Chapter 1 Maven – How to create a Java project using Command Line
Chapter 2 Maven – How to create Maven project in Selenium
Chapter 3 Maven – How to import Maven project in Eclipse
Chapter 4 Maven – How to add M2_REPO classpath variable to Eclipse

IntelliJ IDE

Chapter 1 How to create Maven project in IntelliJ
Chapter 2 How to import Java Maven project in IntelliJ
Chapter 3 How to create a Java Maven project using Command Line in IntelliJ

Maven – How to add M2_REPO classpath variable to Eclipse

New version of Eclipse like Photon or Eclipse IDE 2019-06 has M2_REPO classpath variable after integrating maven plugins (m2eclipse) with Eclipse IDE, M2_REPO classpath variable gets added –> pointing to default locations (for example c:\Users\\.m2\repository) and this variable is non-modifiable. However, if we are using older version of Eclipse, then we need to add M2_REPO manually to Eclipse.

Steps to add M2_REPO

  1. Open Eclipse IDE, select Window ->Preferences ->Java ->Classpath Variables

2. Click on New Button. Add below mentioned information:-

Name – M2_REPO

Path – Path where M2 file places in your system

Eg – C:\Users\SingVi04\.m2\repository

Note:- I have already added M2_REPO, so we can see an error message – Variable name already exists.

3. Verify that M2_REPO add – You can check new Classpath variable M2_REPO is added under BuildPath ->Classpath Variables

Maven – How to import Maven Project into Eclipse

 
 

In the previous tutorial, we have seen how we can create a Java project from Command Line. In this tutorial, will see how we can import this project in Eclipse.

  1. We need to make sure if Eclipse Maven Integration(m2e) is present . Latest Eclipse IDE has bundled the m2e plugin.

2. If you are using older version of Eclipse. Then below are the steps to follow

2.1 Open Eclipse IDE and select Help ->Install New Software

2.2 Click on Add button to add a new Repository

2.3 Fill the below mentioned information in the dialog box:-

Name – M2Eclipse

Location – http://download.eclipse.org/technology/m2e/releases

2.4. After the Pending finish, select all the Plugins and press Next.

2.5. Accept the terms of the license agreement and click Finish

2.6. At the end of the installation, you will be ask to restart your Eclipse. Click Yes to perform the restart.

2.7 To check if the installation is successful, go to Help ->About Eclipse

3. In Eclipse IDE, select File ->Import ->Maven ->Existing Maven Project. 

4. The m2e plugin will analyze the pom.xml and will configure the project and generate the Eclipse files automatically.

5. Below is the code of App.java. Run this code

package com.Selenium;
 
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

6. Below is the code of AppTest.java. Run this code

package com.Selenium;
 
import static org.junit.Assert.assertTrue;
import org.junit.Test;
 
/**
 * Unit test for simple App.
 */
public class AppTest 
{
    /**
     * Rigorous Test 🙂
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}

Note:- Apache Maven Eclipse Plugins like eclipse:eclipse, eclipse:clean, etc are retired. To know more about it, please refer the link

7. Structure of POM.xml

<?xml version="1.0"encoding="UTF-8"?>
 
<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>com.Selenium</groupId>
  <artifactId>MavenProjectFromCMD</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>MavenProjectFromCMD</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Mavendefaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>

        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>

        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>

        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>

        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>

        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>

        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>

        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>

        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

How to install Maven on Windows

 
What is Maven?

Apache Maven is a software project management and comprehension tool. It uses the concept of a project object model (POM), Maven can manage a project’s build, and reporting and documentation from a central piece of information. MAVEN helps us in creating the project structure, managing and downloading the dependencies. We need to define the required dependencies in pom.xml.

Installation Steps

1) To install Apache Maven on window, we need to download Maven’s zip folder from Official Maven Site. Download apache-maven-3.6.1-bin.zip.

2) Unzip the downloaded folder and then it will have below mentioned files. We do not need to install anything, just unzip the folder

3) We need to configure MAVEN_HOME environment variable. Type – “View Adva” in the search option and we will see the option – View Advanced system setting.

4) In System Properties dialog, select Advanced tab and click on the Environment Variables button.

5) In “Environment variables” dialog, System variables, Clicks on the New button and add a MAVEN_HOME variable 

6) A dialog box will appear, mention Variable Name – MAVEN_HOME and Variable value – mention the path where Apache folder is placed.

7) Add %MAVEN_HOME%\bin (full path till bin where maven is placed on your machine) to Path. Click New Button present in System Variable and add MAVEN_HOME\bin

8) Once the Path is updated with %MAVEN_HOME%\bin. This is how it will look like.

9) We have to make sure that JDK is install and JAVA_HOME environment variable is configure. If JAVA_HOME variable is not configure, then add JAVA_HOME just like MAVEN_HOME in Environment variable.

How to verify if Maven is install properly on your machine

Open command prompt and type mvn -version, then the screen should look something like below screen

This confirms that Maven is install successfully and configured on your machine.