Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Maven Archetype
3.
Creating an Archetype
4.
Building an Archetype
5.
Famous Used Artifact
6.
Advantages of the Maven Archetype
7.
Frequently Asked Questions
7.1.
Why do we need to use Maven?
7.2.
What is the need for a pom file?
7.3.
What is GroupID in Maven?
7.4.
What is dependency mediation in Maven?
7.5.
Which locations are used to store Maven dependencies?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

What is a Maven Archetype

Author Sagar Mishra
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

You must be familiar with the term Java if your background is in information technology. Yes, we can build projects and much more using the Java language. But are you familiar with the term Maven? Do you know what a Maven Archetype is?

Maven Archetype

In this blog, we will discuss the topic of What is a Maven Archetype? Let's start now.

Maven Archetype

A Maven project templating toolkit is called Archetype. An archetype is a unique pattern or model that serves as the basis for all similar things. The name is perfect, given that we are working to develop a system that offers a reliable way to produce Maven projects.

For instance, if you created a working project using the quick start archetype, you can quickly create a site for that project by using the site archetype inside that current project. You can do everything like this with archetypes.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Creating an Archetype

The first step is to call the goal to help us create a new project based on an Archetype. Follow the syntax given below:

mvn archetype:generate
mvn archetype:generate output

This command will list all the archetypes present in the Maven. You can further add the artifactid of the archetype whose template you will use in your project. Follow the blog How to Create Archetypes for more details.

A basic Maven project that has the following extra content is an archetype:

  • We can copy the resources from the template that is located at the src/main/resources/archetype-resources to our newly created project.
     
  • The descriptor used to describe the metadata for archetypes is src/main/resources/META-INF/maven/archetype-metadata.xml.
     

There are two ways to create the Maven Archetype that are:

  • Manually: A user can add the resources given above after creating a new Maven project.
     
  • By using the archetype-maven-plugin: A user can use the archetype-maven-plugin to generate it and then change the contents of the archetype-metadata.xml file and archetype-resources directory.
     

After creating the Maven Archetype, you will get the given below structure.

archetype-root/
├── pom.xml
└── src
    └── main
        ├── java
        └── resources
            ├── archetype-resources
            │    ├── pom.xml
            │    └── src
            └── META-INF
                 └── maven
                     └── archetype-metadata.xml

Building an Archetype

Building an Archetype

You can customize your archetype as per your requirements. Follow the steps below to build your archetype.

Step 1: Start with the Archetype Packaging. Here you have to add a few extensions as per your need. You will get this under the maven-archetype directory.

<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.archetype</groupId>
            <artifactId>archetype-packaging</artifactId>
            <version>3.0.1</version>
        </extension>
    </extensions>
    <!--....-->
</build>


Step 2: Now add the dependencies in the pom.xml as per the need of the project.

<project ...>

    <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>        
    </dependencies>

</project>
adding dependencies ss

Step 3: Add your java code under the archetype-resources/src/main/java, as these are the required resources for the Maven Archetype.

package ${package};
// import
@ApplicationPath("${app-path}")
public class AppConfig extends Application {
}


We can add the class for a ping resource, also.

@Path("ping")
public class PingResource{
    //….
}


Step 4: Configure your data after performing all the steps.

<archetype-descriptor
  name="maven-archetype">
    
    <!--...-->
    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/config/liberty</directory>
            <includes>
                <include>server.xml</include>
            </includes>
        </fileSet>
    </fileSets>

</archetype-descriptor>


Step 5: Finally, install the Archetype by running the command.

mvn install
mvn install output

Famous Used Artifact

We will understand the most used Artifact of Archetype in this section. The list is given below.

  • maven-archetype-archetype artifactid: Using maven, a sample archetype project is made using this artifact id.
     
  • maven-archetype-j2ee-simple: An archetype that creates a sample J2EE application that is simplified.
     
  • maven-archetype-simple: A template for creating a basic Maven project.
     
  • maven-archetype-site-simple artifactid: Maven is used to generate a sample maven site using this artifact id.
     
  • maven-archetype-webapp artifactid: In order to create a sample webappmaven project, this artifact id is used.
     
  • maven-archetype-plugin artifactid: In order to develop a sample Maven plugin, this artifact id is utilized.
     
  • maven-archetype-mojo artifactid: In order to develop a sample Maven plugin, this artefact id is used.

Advantages of the Maven Archetype

Advantages

Let's now discuss some of the advantages of the Maven Archetype.

  • The maven Archetype is very helpful while creating project structures.
     
  • While using Maven or Maven Archetype, there is no need to use or add jar files in any project.
     
  • Maven Archetype can be the right and perfect directory for their users.
     
  • Users can build and deploy the project.

Frequently Asked Questions

Why do we need to use Maven?

Maven is a complete build management system. Doing a project and adding JARs and other dependencies are made easier. While updating the primary repository of JARs and other dependencies, a project can benefit from this. Without using scripts, we can create output files.

What is the need for a pom file?

The XML files are called POM (Project Object Model) files. All Maven projects' pom.xml files can be found in the Project Files node of the Projects window. It includes a full list of the parent project's properties and modules. This is why we need pom files.

What is GroupID in Maven?

All Projects that begin with a reversed domain have a GroupID that makes them all uniquely visible. Since the pattern is broken in various projects, Maven does not enforce this convention.

What is dependency mediation in Maven?

Maven decides which dependency version should be used when multiple versions of an artifact exist. If two versions of a dependency are at the same depth in the dependency tree, the earliest declared dependency will be used. The term "dependency mediation" is used to describe this.

Which locations are used to store Maven dependencies?

The JARs, dependency files, and other items that Maven downloads are all saved in the Maven local repository. The Maven local repository is a folder on the local machine that houses all of the artifacts locally.

Conclusion

In this article, we discussed the topic of the Maven Archetype. In detail, we have seen the definition of the Maven Archetype, how to create an archetype, building an archetype, famously used artifacts, and the advantages of the Maven Archetype.

We hope this blog has helped you enhance your knowledge of the Maven Archetype. If you want to learn more, then check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass