Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active March 9, 2023 14:42
Show Gist options
  • Save adojos/2aaf3abeee9422e1840235636dea9c06 to your computer and use it in GitHub Desktop.
Save adojos/2aaf3abeee9422e1840235636dea9c06 to your computer and use it in GitHub Desktop.
Maven: Create Maven Project Using Command Line #maven

Create Maven Project Using Command Line


Maven project can be easily created using built-in plugins from the popular IDEs such as Eclipse and IntelliJ IDEA. However, in scenarios where you don't or cannot use IDE, we can also create a maven project from the Maven native command line without using any IDE. In this article we would create a simple Java project using Maven command line with and without archetype.

👉 Pre-Requisite:

1. You should have Java (JDK) installed on your system in order to run Maven.
2. You should have 'JAVA_HOME', 'M2_HOME' Environment Variables set on your system.
3. You should have 'Java bin' and 'Maven bin' folders specified in you PATH Environment Variables.

Maven Archetype Plugin

Maven archetypes is a maven project templating toolkit which can be used to generate a skeleton maven projects of different types, such as JAR, web application, maven site, etc. Once you create your project using one of the available archetype, you can easily tweak the generated project structure or artefacts including auto-generated pom.xml as per your project requirement.

Maven archetypes are available simply as 'Maven Plugin' called 'maven-archetype-plugin' that we can use to extend the functionality of Maven. It is one of the default plugin that is supported directly by Maven. The simplest way to use the archetype is to simply call "archetype:generate".


Option 1: Simple Project using 'quickstart' Archetype

If you want to start with a simple Maven project and add/modify the project configuration along the way, you can start with the 'maven-archetype-quickstart' archetype. Using this archetype would provide you with a simple greenfield project configuration.

  1. Create a directory where you want to create your java maven project and start a shell in that directory.

  2. On your command line, execute the following Maven goal command:

mvn archetype:generate -DgroupId=com.adojos.app -DartifactId=my-java-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Output:

The above command should produce the following output

$ mvn archetype:generate -DgroupId=com.adojos.app -DartifactId=my-java-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.adojos.app
[INFO] Parameter: artifactId, Value: my-java-app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.adojos.app
[INFO] Parameter: packageInPathFormat, Value: com/adojos/app
[INFO] Parameter: package, Value: com.adojos.app
[INFO] Parameter: groupId, Value: com.adojos.app
[INFO] Parameter: artifactId, Value: my-java-app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Project created from Archetype in dir: D:\DEV-WORKSPACE\Workspace-Eclipse\my-java-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.814 s
[INFO] Finished at: 2022-03-18T11:46:39Z
[INFO] ------------------------------------------------------------------------

Resulting Directory Structure

my-java-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- adojos
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- adojos
                    `-- app
                        `-- AppTest.java

Option 2: Create Project using Interactive Mode

If you want to create maven project using an 'archetype', but are not sure which archetype to use then you can run Maven's 'generate' goal without any parameters or command switches. This would make Maven's 'generate' goal to run in 'Interactive Mode'.

In this mode, Maven command line would automatically provide you with suitable options to choose from, at each step via prompts. For example, if you are not sure about the precise name of the 'archetype' you want to use, then Maven's 'generate' goal would print a list of all available 'archetypes' (archetype catalog) and give you option to choose one of them. Next, it would prompt you to input 'groupID', followed by 'artefactID' and so on.

  1. Create a directory where you want to create your java maven project and start a shell in that directory.

  2. For running 'generate' goal in interactive mode simply trigger the 'generate' goal without any parameters and follow the subsequent command line prompts:

mvn archetype:generate

Note: Currently there are over 3000+ archetypes available in archetype catalog, so its better to know your archetype before generating the project rather then choosing from a long list of archetypes.

Output and Resulting Directory Structure

The output and resulting directory structure in this case would depend on the archetype and other inputs provided by you on command prompt via interactive mode.


Compilation Error

When compiling your newly created Maven project, it is very common to encounter the following compilation error. If this happens, please refer to the article about How To Configure Maven Java Compiler. Also mentioned in Reference Links below.

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[INFO] 2 errors
[INFO] -------------------------------------------------------------


Understanding the Command Switches

  1. archetype:generate : The archetype:generate tells maven to generate a maven project using the information provided in the subsequent command switches or supplied interactively.

  2. -DgroupId=com.adojos.app : Every maven project is identified by 'Project Identifiers' also called 'Maven Coordinates' which consist of groupId, artifactId, packaging, version, and name. The -DgroupId=com.adojos.app here is one of such identifier.

  3. -DartifactId=my-java-app : Once again this command switch is setting one of the Maven project identifier called 'artefactID' which is usually your project or app name.

  4. -DarchetypeArtifactId=maven-archetype-quickstart : This switch specifies the template (archetype) to use for the project. The maven-archetype-quickstart artifact generates a simple JAR based maven project.

  5. -DarchetypeVersion=1.4 : This switch specifies the archetype model version.

  6. -DinteractiveMode=false : Setting this parameter to 'true' would make Maven to prompt you to confirm all the required parameters of the project one by one which is useful, if you don't know which parameters to provide for generating maven project.


About '-DinteractiveMode=' Parameter

If you don't specify the 'archetype' for your project while generating the project using Maven 'generate' goal then Maven shall do one of the following based on the value of '-DinteractiveMode=':

  1. If '-DinteractiveMode=' is not mentioned in your command, then Maven by default would run interactively and prompt you for archetype name on the command line.

  2. If '-DinteractiveMode=' is set to true, then again Maven would run interactively and prompt you for archetype name on the command line.

  3. If '-DinteractiveMode=' is set to false, then Maven would by default automatically choose 'maven-archetype-quickstart' archetype for your project and generate project without prompting.

In the below example we have not specified any 'archetype' for our project and also set the'-DinteractiveMode=false' so in this case Maven would automatically choose the choose 'maven-archetype-quickstart' archetype for us and generate project accordingly.

mvn archetype:generate -DgroupId=com.adojos.app -DartifactId=my-java-app -DarchetypeVersion=1.4 -DinteractiveMode=false

Reference Links

How To Configure Maven Java Compiler


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment