Generating New Projects Using Archetypes

Creating Maven projects from scratch can be a lot of work. What dependencies does my Liferay portlet project need? What does a Liferay Maven Service Builder project look like? How do I create a Liferay Maven-based context contributor? These questions can be answered with three words: Liferay Maven Archetypes.

Liferay provides a slew of Maven archetypes to easily create Liferay projects. In this tutorial, you’ll learn how to use Liferay’s Maven archetypes to generate a project.

At the time of this writing, Liferay provides approximately 60 Maven archetypes for you to use; expect this number to continue growing! These archetypes are generated from the Central Repository, unless you’ve configured for them to be generated from another remote repository (e.g., Liferay Repository. You can view the Liferay-provided Maven archetypes by running the following command:

mvn archetype:generate -Dfilter=liferay

The generated archetypes are not all intended for the latest Liferay DXP release. Some are intended for earlier versions of Liferay Portal (e.g., 7.0, 6.2). For example, archetypes with the com.liferay.maven.archetypes prefix are legacy archetypes targeted for Liferay Portal 6.2. Those prefixed with com.liferay.project.templates.[TYPE] or com.liferay.faces.archetype:[TYPE] are compatible with Liferay DXP 7.1.

Here’s a brief list of some popular Maven archetypes provided by Liferay:

For documentation on the archetypes (project templates) compatible with Liferay DXP 7.1, see the Project Templates reference section. Visit Maven’s Archetype Generation documentation for further details on how to modify the Maven archetype generation process.

Here’s an example that creates a Liferay MVC portlet using its Liferay Maven archetype.

  1. On the command line, navigate to where you want your Maven project. Run the Maven archetype generation command filtered for Liferay archetypes only:

    mvn archetype:generate -Dfilter=liferay
    
  2. Select the com.liferay.project.templates.mvc.portlet archetype by choosing its corresponding number (e.g., 11).

    In most cases, you should choose the latest archetype version. The archetype versions provided are compatible with all 7.x versions of Liferay DXP.

  3. Depending on the Maven archetype you selected, you’re given a set of archetype options to fill out for your Maven project. For the MVC portlet archetype, you could use these properties:

    • groupId: com.liferay
    • artifactId: com.liferay.project.templates.mvc.portlet
    • version: 1.0.6
    • package: com.liferay.docs
    • className: SampleMVC

    Once you’ve filled out the required property values, you’re given a summary of the properties configuration you defined. Enter Y to confirm your project’s configuration.

Your Maven project is generated and available from the folder you ran the archetype generation command from. If you have an existing parent pom.xml file in that folder, your module project is automatically accounted for there:

<modules>
    ...
    <module>com.liferay.project.templates.mvc.portlet</module>
</modules>

The Liferay Maven archetypes generate deployable Liferay projects, but they’re bare bones and likely require further customizations.

If you want to generate a quick foundation for a Liferay project built with Maven, using Liferay Maven archetypes is your best option.

« Installing Liferay Maven ArtifactsCreating a Module JAR Using Maven »
Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0