Ir al contenido principal

Creating an AEM application using Maven and Adobe's archetype

In this tutorial, I show how to use Maven and Adobe's archetype to create an application from scratch and then build it and deploy it into AEM, also, the last part of the video shows how to do the same process using a Maven container using Docker, above is the vid and below you will find some useful notes.
  1. 1.

    Pre-reqs

  2. 2.

    What is Maven

    • Maven is among many things a build automation tool which uses xml files called Project Object Model or POM files to describe different things about a project like what dependencies the project has, how to build and deploy a project and so on.
    • For dependency management, Maven uses two types of artifact repositories which can be categorized as:
    • Remote, which are typically publicly available on the internet like Maven Central or privately available inside your company’s internal network.
    • And local repositories, which are typically inside a .m2 folder inside your local file system where maven runs and it’s used for caching and storing local builds.
  3. 3.

    Maven Archetypes

    • Maven archetypes can be used for creating new applications or modules without going through the hassle of configuring each piece of the app by hand, like the appropriate libraries or the plugins required, the different profiles to execute and many other different things needed to provide a seamless development experience. In our case adobe’s archetype will take care of even creating some mock content, example classes, components, configurations and other stuff for us so that we can start tinkering with our new app right away.
    •   
      mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate "-DarchetypeGroupId=com.adobe.granite.archetypes" "-DarchetypeArtifactId=aem-project-archetype" "-DarchetypeVersion=19" "-DarchetypeCatalog=https://repo.adobe.com/nexus/content/groups/public/" "-DgroupId=co.dlighthouse.aem.firstapp" "-DartifactId=aem-first-app" "-Dversion=0.1.0-SNAPSHOT" "-Dpackage=co.dlighthouse.aem.firstapp" "-DappsFolderName=first-app" "-DconfFolderName=first-app" "-DartifactName=First App Example" "-DcomponentGroupName=First App Example" "-DcontentFolderName=first-app" "-DcssId=first-app" "-DpackageGroup=first-app" "-DsiteName=First App Example" “-DoptionAemVersion=6.4.0”
        
      
    • Adobe’s archetype will generate a multi module project, with modules for the bundle that contais all the java classes like Sling models and sling servlets, another module for creating the AEM package that contains the AEM components and a final one with the site and assets. There are two final modules used for testing.
  4. 4.

    Building and deploying

    • For building and deploying the application into an Author instance running locally using the 4502 port
        
      mvn clean install -PautoInstallPackage -Padobe-public
        
      
  5. 5.

    Docker and Maven

    • If you recall from the video, maven uses a local .m2 folder for the local repository and that whenever a dependency is missing there it will try to download it from the remote repository. When using Docker containers and given that they are isolated from each other if we just try to execute the maven commands it will try to download everything every time you use a new container.
    • To avoid that we need to use Docker Volumes which will allow us to define a local folder on your machine so that docker can use it for storing the artifacts downloaded by maven and so that they can be reused from different containers.
    • For defining a volume when starting a container you just need to add a -v parameter followed by a mapping, in which you specify the local folder in which the files are or should be located, a colon and the internal container’s folder
        
      docker run ... -v LOCAL-FOLDER:CONTAINER-FOLDER ...
        
      
    • If you want to check the current contents of a folder using the container you can use bash instead of the maven command at the end
        
      docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/workdir -w /usr/src/workdir -v "$(pwd)"/.m2:/root/.m2 maven:3.6.1-jdk-8 /bin/bash
        
      
    • For creating the app you can use docker and append the complete maven command that uses the archetype after that
        
      docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/workdir -w /usr/src/workdir -v "$(pwd)"/.m2:/root/.m2 maven:3.6.1-jdk-8 mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate "-DarchetypeGroupId=com.adobe.granite.archetypes" "-DarchetypeArtifactId=aem-project-archetype" "-DarchetypeVersion=19" "-DarchetypeCatalog=https://repo.adobe.com/nexus/content/groups/public/" "-DgroupId=co.dlighthouse.aem.firstappdocker" "-DartifactId=aem-first-app-docker" "-Dversion=0.1.0-SNAPSHOT" "-Dpackage=co.dlighthouse.aem.firstappdocker" "-DappsFolderName=first-app-docker" "-DconfFolderName=first-app-docker" "-DartifactName=First App Docker Example" "-DcomponentGroupName=First App Docker Example" "-DcontentFolderName=first-app-docker" "-DcssId=first-app-docker" "-DpackageGroup=first-app-docker" "-DsiteName=First App Docker Example" “-DoptionAemVersion=6.4.0”
        
      
    • You can acomplish the same thing here by appending just the build MAven command after the docker one
        
      docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/workdir -w /usr/src/workdir -v "$(pwd)"/../.m2:/root/.m2 maven:3.6.1-jdk-8 mvn clean install -Padobe-public -PautoInstallPackage -Daem.host=YOUR-MACHINE-PORT
        
      
  6. 6.

Comentarios

Entradas populares de este blog

Creating a Mongo replicaset using docker: Mongo replicaset + Nodejs + Docker Compose

This is a Docker tutorial for creating a Mongo replica set using docker compose and also a couple more containers to experiment with the replica set, above is the vid and below you will find some of the steps followed.
StepsPre-reqsHave node.js installedAnd docker installed (make sure you have docker-compose as well)Create a container for defining configurations for a mongo instanceCreate a container for setting up the replica setCreate a simple node app using expressjs and mongoose (A modified version from the previous video)Create a docker-compose file with the mongo and setup containers and two additional containers for experimenting with the replica setBuild, Run and experiment with your new containers Create a dockerfile for the first mongo container (not really needed but you could configure more stuff if needed)Include container with mongo preinstalled: FROM mongoCreate default/working directory: WORKDIR /usr/src/configsCopy mongo's configurations file into the container
C…

Creating a Docker container for a NodeJs application: Express + Mongo + Docker Compose

This is a Docker tutorial for creating a NodeJs container using expressjs and mongoose, above is the vid and below you will find the steps followed.
StepsPre-reqsHave node.js installedAnd docker installed (make sure you have docker-compose as well)Create an simple node app using expressjs and mongooseModify your container and create a docker-compose fileBuild and Run your new container Create your simple node appInitialize the node appnpm initInstall the dependencies for our appnpm install --save express mongoose Create the database.jsCreate the index.js Create a dockerfileInclude container with node preinstalled: FROM nodeCreate default/working directory: WORKDIR /usr/src/appCopy package.json to workdir and install dependencies (which we will need in this case😊): COPY package.json .RUN npm install Copy the rest of the app (just the index.js file in this case)COPY . .Expose the port required for reaching our expressjs appEXPOSE 3000Add a command to run when this container is star…

Creating a tensorflow.js + vue.js simple application in javascript

This is a Tensorflow.js tutorial for creating a simple application using Vue.js, above is the vid and below you will find some of the steps followed. Steps Pre-reqs Have node.js installed Create the Vue.js application using nuxt.js Add support for tensorflow.js in vue.js and add a simple model Add the simple tensorflow.js model using vue.js into github pages Create the Vue.js application using nuxt.js Install vue.js cli npm install -g vue-cli Create a base Vue.js app using the starter kit from Nuxt vue init nuxt-community/starter-template simple-vue-tensorflow Start the dev server npm run dev Create empty component components/TensorflowExample.vue Add the component into the page pages/index.vue …