How to create a project using maven-archetype-plugin? What is artefactId etc?

40,518

Solution 1

mvn archetype:generate command is used to create a project from an existing template. There are several archetype's defined by many developers and project groups. When you run the command, maven does following things:

  1. Downloads maven-archetype-plugin's latest version.
  2. Lists all archetype's that can be used to create a project from. If you defined an archetype while calling the command, maven jumps to step 4.
  3. By default, maven chooses maven-archetype-quickstart archetype which basically creates a maven Hello World project with source and test classes. If you want to create a simple project, you can just press enter to continue. If you want to create a specific type of application, you should find the archetype matching your needs and enter the number of that archetype, then press enter. E.g. If you want to create a webapp project, you can enter 153 (this is the current number for this archetype, it can change in time.)
  4. Since archetypes are templates and they intend to reflect current best practices, they can evolve in time, thus they have their own versions. Maven will ask you which version of the archetype you want to use. By default, maven chooses latest version for you. so if you agree to use the latest version of an archetype, just press Enter at this step;
  5. Every maven project (and module) has its groupId, artifactId and version. Maven will then ask these to you in three steps. groupId: This is generally unique amongst an organization or a project. artifactId: The artifactId is generally the name that the project is known by. version: This is the last piece of the naming puzzle.(read more)
  6. Finally, maven will ask you the package structure for your code. A best practice is to create your folder structure that reflects the groupId, thus Maven sets this as default but you are free to change this.

After entering these information, Maven will show you all the information you entered and ask you to verify project creation. If you press Y and then enter, voila your project is created with the artifact and settings you chose.

You can also read maven-archetype-plugin's usage site.

Solution 2

It is asking you which archetype you want to use to seed your project. If you press "enter" at that prompt, it'll give you a list of available choices. You can use maven-archetype-quickstart to just create a simple project (it may prompt you to pick a repository after this, in that case, just enter the number that corresponds to the first repository listed after you enter this).

To answer your other question: yes, using an archetype is a common way for setting up a new project. Mainly because there are plenty of archetypes out there for all kinds of projects/modules. Once you know which archetype you want, using it to bootstrap a project is the simplest way to get started.

Solution 3

A quick look at the tutorial reveales, that you omitted some parameters to the archetype:generate command. That's why it doesn't know which archetype to choose to generate your tutorial project from and presents all available archetypes (149) to you.

First question: By pressing return then you accepted the suggestion of the archetype plugin and choose # 149. This archetype exits in different versions and normally, as stated by Chris it's ok to choose the latest - here 1.1.

Second question: Since an archetype is kind of a template which can save you lots of work: Yes that's common. But not the only way - you can always start with an empty project.

Share:
40,518

Related videos on Youtube

Mellon
Author by

Mellon

Software Engineer

Updated on July 09, 2022

Comments

  • Mellon
    Mellon almost 2 years

    I am new to Maven and am using the maven.apache.org tutorial here as an introduction.

    In the "How do I make my first Maven project?" section of the tutorial, it teaches us to generate a Maven archetype project by executing the following command:

    mvn archetype:generate
    

    After Maven downloaded many artifacts, it suddenly stopped and asked the following question on the command line:

    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 149:
    

    The Apache tutorial does not describe this prompt.

    I have two questions:

    1. What is the question above asking for? How should it be answered such that the mvn archetype:generate process continues?

    2. Conventionally, do people use mvn archetype:generate to create a Maven project?

    --------------Update---------------------

    With respect to my 1st question, I pressed "enter" without inputting any value and got the following output:

    Choose version: 
    1: 1.0-alpha-1
    2: 1.0-alpha-2
    3: 1.0-alpha-3
    4: 1.0-alpha-4
    5: 1.0
    6: 1.1
    Choose a number: 6: 
    

    What is that?

    I input "1" in above case, then I got the following things:

    Define value for property 'package':  : :
    Define value for property 'groupId': : 
    Define value for property 'artifactId': :
    ...
    

    How can I define them?

  • Mellon
    Mellon over 12 years
    @ Chris , please check my update in my post, I press enter, but got strange output. what are they?
  • Chris
    Chris over 12 years
    those are the versions of the archetype available in your repository. Unless you have a compelling reason otherwise, you should just use the latest (so in this case, version 1.1)
  • Sri Sankaran
    Sri Sankaran over 12 years
    You can turn off all prompting by using the ineractiveMode=false switch. Try mvn archetype:generate -DgroupId=com.mycompany -DartifactId=someProject -DinteractiveMode=false. Hope that helps.