Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Maven Archetype?
3.
Maven Archetypes Descriptor
4.
How to Create Archetypes?
5.
Frequently Asked Questions
5.1.
Which locations are used to store Maven dependencies?
5.2.
How does Maven produce Java documentation?
5.3.
How does one create a new project from a hard drive?
5.4.
Describe the surefire plugin
5.5.
What is dependency mediation?
6.
Conclusion 
Last Updated: Mar 27, 2024
Medium

How to Create Archetypes?

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

Introduction

Let me assume that if you are reading this article, you already know what Maven is. But what comes next?

Next, an Archetype is a project model from which other things of the same kind are made. This blog will discuss how to create archetypes in Maven in detail. Let's start going!

how to create archetype

What is Maven Archetype?

Maven Archetype is a Maven plugin that enables you to build a project structure based on a template. When you start a new project, Maven generates these archetypes, essentially project templates. In a nutshell, Archetype is a toolkit for Maven project templating.

The main advantage of using archetypes is standardizing project development and making it simple for developers to comply with best practices while launching projects more quickly.

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

Maven Archetypes Descriptor

Maven Archetypes Descriptor

The maven archetype descriptor is the center of the archetype project. It is an XML file with the name archetype-metadata.xml that is contained in the jar's META-INF/maven directory.

It is used to explain the metadata of Maven Archetypes.

<archetype-descriptor
  ...
  name="custom-archetype">
    <requiredProperties>
        <requiredProperty key="foo">
            <defaultValue>bar</defaultValue>
        </requiredProperty>
    </requiredProperties>
    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
    </fileSets>
    <modules>
        <module name="sub-module"></module>
    </modules>
</archetype-descriptor>


Different Tags used in the Maven Archetype are:-

1. requiredProperties: When a project is generated, it is used to provide properties. We will therefore be given the option to accept the default value or be prompted for values to be entered for them.

2. fileSets: They are used to set up which resources will be copied into the generated project.

3. modules:  It can configure every module in the generated project.


Also check out - Phases of Compiler

How to Create Archetypes?

The following steps are used to create Archetypes in Maven:-

1. Visit the site code.quarkus.io .

2. Fill the groupIdArtifactId, and Build Tool as shown in the figure and generate the project.

Generating archetype file

3. Download the zip file and open it in the text editor of your choice.

4.  Open the pom.xml file. The following is an example of a pom.xml for an archetype artifact:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.codingninjas</groupId>
  <artifactId>ninjaarchetype</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  
  <properties>
    <compiler-plugin.version>3.10.1</compiler-plugin.version>
   
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
    <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
    <quarkus.platform.version>2.14.2.Final</quarkus.platform.version>
    <skipITs>true</skipITs>
    <surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
  </properties>
  
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>${quarkus.platform.group-id}</groupId>
        <artifactId>${quarkus.platform.artifact-id}</artifactId>
        <version>${quarkus.platform.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
  <dependencies>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
    </dependency>
    
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-openapi</artifactId>
    </dependency>
    
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive</artifactId>
    </dependency>
    
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-hibernate-orm-panache</artifactId>
    </dependency>
    
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-arc</artifactId>
    </dependency>
    
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>${quarkus.platform.group-id}</groupId>
        <artifactId>quarkus-maven-plugin</artifactId>
        <version>${quarkus.platform.version}</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <goals>
              <goal>build</goal>
              <goal>generate-code</goal>
              <goal>generate-code-tests</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${compiler-plugin.version}</version>
        <configuration>
          <compilerArgs>
            <arg>-parameters</arg>
          </compilerArgs>
        </configuration>
      </plugin>
      
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire-plugin.version}</version>
        <configuration>
          <systemPropertyVariables>
            <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
            <maven.home>${maven.home}</maven.home>
          </systemPropertyVariables>
        </configuration>
      </plugin>
      
      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>${surefire-plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
            <configuration>
              <systemPropertyVariables>
                <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
                <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                <maven.home>${maven.home}</maven.home>
              </systemPropertyVariables>
            </configuration>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>3.9.0.2155</version>
      </plugin>
      
      <plugin>
        <groupId>org.pitest</groupId>
        <artifactId>pitest-maven</artifactId>
        <version>1.1.0</version>
      </plugin>
    </plugins>
  </build>
  
  <profiles>
    <profile>
      <id>native</id>
      <activation>
        <property>
          <name>native</name>
        </property>
      </activation>
      <properties>
        <skipITs>false</skipITs>
        <quarkus.package.type>native</quarkus.package.type>
      </properties>
    </profile>
  </profiles>
</project>

 

5. Run the following command to install all the maven dependencies in the project.

mvn install
mvn install

6. Run the following command to install the archetype from the maven project.

mvn archetype:create-from-project
mvn archetype:create-from-project

7.  The structure obtained from creating Archetypes is:-

archetype location

Frequently Asked Questions

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.

How does Maven produce Java documentation?

Maven creates Javadoc for projects using the maven-javadoc plugin. This plugin internally uses the JDKbinjavadoce.exe command to produce Javadoc. The project's javadocs are generated using the mvn install command when it is deployed.

How does one create a new project from a hard drive?

A new project is launched using the -mvn Archetype: create.

The Archetype is created after reading the source and resource files, along with the parameters' values and other properties.

Describe the surefire plugin

Unit tests for applications are run using the Surefire Plugin during the build lifecycle's test phase. It can produce reports in either plain text or XML files, two different file types.

What is dependency mediation?

Maven decides which dependency version should be used when multiple versions of an artifact exist. If two dependency versions 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.

Conclusion 

Congratulations on finishing the blog! We have discussed how to create archetypes in Maven. We have further discussed archetype descriptors and commands to create archetypes.

We hope this blog has helped you enhance your knowledge of how to create archetypes in maven. Do not stop learning! We recommend you read some of our maven articles: 

1. Maven Interview Questions

2. DropWizard- Setting up using Maven

3. How to install Maven on Windows

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, 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. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

We wish you Good Luck! 

Happy Learning!

Live masterclass