Archetype catalogues

How does the plugin find all the archetypes available in the system? When you just type mvn archetype:generate, a list of archetypes is displayed by the plugin for user selection. The complete list is around 1100, and only the first 10 are shown here:

1: remote -> br.com.ingenieux:elasticbeanstalk-service-webapp-archetype (A Maven Archetype Encompassing RestAssured, Jetty, Jackson, Guice and Jersey for Publishing JAX-RS-based Services on AWS' Elastic Beanstalk Service)
2: remote -> br.com.ingenieux:elasticbeanstalk-wrapper-webapp-archetype (A Maven Archetype Wrapping Existing war files on AWS' Elastic Beanstalk Service)
3: remote -> br.com.otavio.vraptor.archetypes:vraptor-archetype-blank (A simple project to start with VRaptor 4)
4: remote -> br.gov.frameworkdemoiselle.archetypes:demoiselle-html-rest (Archetype for web applications (HTML + REST) using Demoiselle Framework)
5: remote -> br.gov.frameworkdemoiselle.archetypes:demoiselle-jsf-jpa (Archetype for web applications (JSF + JPA) using Demoiselle Framework)
6: remote -> br.gov.frameworkdemoiselle.archetypes:demoiselle-minimal (Basic archetype for generic applications using Demoiselle Framework)
7: remote -> br.gov.frameworkdemoiselle.archetypes:demoiselle-vaadin-jpa (Archetype for Vaadin web applications)
8: remote -> ch.sbb.maven.archetypes:iib9-maven-projects (IBM Integration Bus 9 Maven Project Structure)
9: remote -> ch.sbb.maven.archetypes:wmb7-maven-projects (WebSphere Message Broker 7 Maven Project Structure)
10: remote -> co.ntier:spring-mvc-archetype (An extremely simple Spring MVC archetype, configured with NO XML.)

Going back to the original question, how does the plugin find these details about different archetypes?

The archetype plugin maintains the details about different archetypes in an internal catalogue, which comes with the plugin itself. The archetype catalogue is simply an XML file. The following configuration shows the internal catalogue of the archetype plugin:

<archetype-catalog>

<!-- Internal archetype catalog listing archetypes from the Apache Maven project. -->

  <archetypes>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-archetype</artifactId>
      <version>1.0</version>
      <description>An archetype that contains a sample archetype.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-j2ee-simple</artifactId>
      <version>1.0</version>
      <description>An archetype that contains a simplified sample J2EE application.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-plugin</artifactId>
      <version>1.2</version>
      <description>An archetype that contains a sample Maven plugin.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-plugin-site</artifactId>
      <version>1.1</version>
      <description>An archetype that contains a sample Maven plugin site. This archetype can be layered upon an existing Maven plugin project.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-portlet</artifactId>
      <version>1.0.1</version>
      <description>An archetype that contains a sample JSR-268 Portlet.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-profiles</artifactId>
      <version>1.0-alpha-4</version>
      <description></description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-quickstart</artifactId>
      <version>1.1</version>
      <description>An archetype that contains a sample Maven project.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-site</artifactId>
      <version>1.1</version>
      <description>An archetype that contains a sample Maven site, which demonstrates some of the supported document types like APT, XDoc, and FML and demonstrates how to i18n your site. This archetype can be layered upon an existing Maven project.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-site-simple</artifactId>
      <version>1.1</version>
      <description>An archetype that contains a sample Maven site.</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-webapp</artifactId>
      <version>1.0</version>
      <description>An archetype that contains a sample Maven Webapp project.</description>
    </archetype>
  </archetypes>
</archetype-catalog>

In addition to the internal catalogue, you can also maintain a local archetype catalogue. This is available at USER_HOME/.m2/archetype-catalog.xml, and by default, it's an empty file.

There is also a remote catalogue, which is available at http://repo1.maven.org/maven2/archetype-catalog.xml. By default, the archetype plugin will load all the available archetypes from the local and remote catalogues. If we go back to the archetype list displayed by the plugin, when you type mvn archetype:generate, by looking at the each entry, we can determine whether a given archetype is loaded from the internal, local, or remote catalogue.

For example, the following archetype is loaded from the remote catalogue:

1: remote -> br.com.ingenieux:elasticbeanstalk-service-webapp-archetype (A Maven Archetype Encompassing RestAssured, Jetty, Jackson, Guice and Jersey for Publishing JAX-RS-based Services on AWS' Elastic Beanstalk Service)

If you want to enable the archetype plugin to list all the archetypes from the internal catalogue, you need to use the following command:

$ mvn archetype:generate -DarchetypeCatalog=internal

To list all the archetypes from the local catalogue, you need to use the following command:

$ mvn archetype:generate -DarchetypeCatalog=local

To list all the archetypes from the internal, local, and remote catalogues, you need to use the following command:

$ mvn archetype:generate -DarchetypeCatalog=internal,local,remote

Building an archetype catalogue

In addition to the internal, local, and remote catalogues, you can also build your own catalogue. Say you have developed your own set of Maven archetypes and need to build a catalogue out of them, so it can be shared with others by publicly hosting it. Once you have built the archetypes, they will be available in your local Maven repository. The following command will crawl through the local Maven repository and build an archetype catalogue from all the archetypes available there. Here, we use the crawl goal of the archetype plugin:

$ mvn archetype:crawl -DcatalogFile=my-catalog.xml

Public archetype catalogues

People who develop archetypes for their projects will list them in publicly hosted archetype catalogues. The following points list some of the publicly available Maven archetype catalogues:

Let's take Apache Synapse as an example. Synapse is an open source Apache project that builds an Enterprise Service Bus (ESB). The following command will use the Apache Synapse archetype to generate a Maven project:

$ mvn archetype:generate
                   -DgroupId=com.packt.samples
                   -DartifactId=com.packt.samples.synapse
                   -Dversion=1.0.0
                   -Dpackage=com.packt.samples.synapse.application
                   -DarchetypeCatalog=http://synapse.apache.org
                   -DarchetypeGroupId=org.apache.synapse
                   -DarchetypeArtifactId=synapse-package-archetype
                   -DarchetypeVersion=2.0.0
                   -DinteractiveMode=false

The previous command will produce the following directory structure. If you look at the pom.xml file, you will notice that it contains all necessary instructions along with the required dependencies to build the Synapse project:

com.packt.samples.synapse
                       |-pom.xml
                       |-src/main/assembly/bin.xml
                       |-conf/log4j.properties
                       |-repository/conf
                                      |-axis2.xml
                                      |-synapse.xml

Let's have look at the previous Maven command we used to build the project with the Synapse archetype. The most important argument is archetypeCatalog. The value of the archetypeCatalog argument can point directly to an archetype-catalog.xml file or to a directory, which contains an archetype-catalog.xml file. The following configuration shows the archetype-catalog.xml file corresponding to the Synapse archetype. It has only a single archetype but with two different versions:

<archetype-catalog>
  <archetypes>
    <archetype>
      <groupId>org.apache.synapse</groupId>
      <artifactId>synapse-package-archetype</artifactId>
      <version>1.3</version>
      <repository>http://repo1.maven.org/maven2</repository>
      <description>Create a Synapse 1.3 custom package</description>
    </archetype>
    <archetype>
      <groupId>org.apache.synapse</groupId>
      <artifactId>synapse-package-archetype</artifactId>
      <version>2.0.0</version>
      <repository>
        http://people.apache.org/repo/m2-snapshot-repository
      </repository>
      <description>Create a Synapse 2.0.0 custom package</description>
    </archetype>
  </archetypes>
</archetype-catalog>

Note

The value of the archetypeCatalog parameter can be a comma-separated list, where each item points to an archetype-catalog.xml file or to a directory, which contains archetype-catalog.xml. The default value is remote,local, where the archetypes are loaded from the local repository and the remote repository. If you want to load an archetype-catalog.xml file from the local filesystem, then you need to prefix the absolute path to the file with file://. The value local is just a shortcut for file://~/.m2/archetype-catalog.xml.

In the previous Maven command, we used the archetype plugin in the non-interactive mode, so we have to be very specific with the archetype we need to generate the Maven project. This is done with the following three arguments. The value of these three arguments must match the corresponding elements defined in the associated archetype-catalog.xml file:

-DarchetypeGroupId=org.apache.synapse
-DarchetypeArtifactId=synapse-package-archetype
-DarchetypeVersion=2.0.0

The anatomy of archetype-catalog.xml

We already went through couple of sample archetypes-catalog.xml files and their usage. The XML schema of the archetypes-cat alog.xml file is available at http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd. The following configuration shows an archetypes-catalog.xml file skeleton with all the key elements:

<archetype-catalog>
  <archetypes>
    <archetype>
      <groupId></groupId>
      <artifactId></artifactId>
      <version></version>
      <repository></repository>
      <description></description>
    </archetype>
    ...
  </archetypes>
</archetype-catalog>

The archetypes parent element can hold one or more archetype child elements. Each archetype element should uniquely identify the Maven artifact corresponding to the archetype. This is done using groupId, artifactId, and version of the artifact. These three elements carry exactly the same meaning that we discussed under Maven coordinates. The description element can be used to describe the archetype. The value of the description element will appear against the archetype, when it is listed by the archetype plugin. For example, the following output is generated according to the pattern, groupId:artifactId (description) from the archetypes-catalog.xml file, when you type mvn archetype:generate:

Choose archetype:
1: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype that contains a sample Maven project.)

Each archetype child element can carry a value for the repository element. This instructs the archetype plugin on where to find the corresponding artifact. When no value is specified, the artifact is loaded from the repository where the catalogue comes from.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.176