Maven Magic in AEM: Build & Share Artifacts with Nexus
In the dynamic AEM (Adobe Experience Manager) development world, harnessing the power of Maven and Sonatype Nexus can be the key to unlocking seamless project builds and efficient artifact sharing. We’ll delve into the intricacies of incorporating Maven into your AEM projects and leveraging Sonatype Nexus for enhanced build processes and simplified artifact sharing.
Setup Nexus Repository
Nexus is a self-managed repository manager that centralizes the storage and distribution of software artifacts, offering organizations control over their infrastructure for efficient management and retrieval of dependencies in the development process.
Install:
Follow this documentation to install the Nexus repo. https://help.sonatype.com/en/installation-methods.html
For quick setup I used docker.
docker run -d -p 8081:8081 --name nexus sonatype/nexus3After successfully installing we will see something like this screen. (http://localhost:8081/#browse/browse)
Login to the Nexus repo. For 1st time login, we need to get the admin password from sonatype-work/nexus3/admin.password. Copy the password and logged in with:
Username: admin
Password: Retrieved password
Setup
Create Nexus Role
Create a custom role first, for example, ‘nexus-access,’ and apply privileges based on your access requirements. In my case, I added nx-repository-view-*-*-* where CRUD operations can be performed.
Create Nexus User
Create a new user and Grant the previously created Role. Took the user ID and password.
Make sure you disallow the anonymous access. Otherwise, anyone can access your repository. If you are planning to make a public repo then you can enable it.
Part 1: Maven Deploy
In your project mail pom.xml file add the following XML.
<distributionManagement>
<repository>
<id>nexus-repo</id>
<name>releases</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-repo</id>
<name>snapshots</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>Also in your .m2/setting.xml file
<server>
<id>nexus-repo</id>
<username>nexus-repo</username>
<password>root</password>
</server><profile>
<id>nexus-central</id>
<repositories>
<repository>
<id>nexus-repo</id>
<url>http://localhost:8081/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-repo</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile><activeProfiles>
<activeProfile>adobe-public</activeProfile>
<activeProfile>nexus-central</activeProfile>
<activeProfile>maven-central</activeProfile>
</activeProfiles>You will get the related changes here: https://github.com/Sady-Rifat/aem-demo/commit/0a86d8e3006f9f727e6d8f9e67968234af1f2f4c
Now from your terminal run this command: mvn clean deploy
Based on your project version it will be in either maven-releases or maven-snapshots.
Part 2: Maven Import
Now consider another AEM project where I intend to import a Maven dependency deployed on the Nexus Repository. The concept revolves around installing our primary project, wherein, during build time, it downloads the dependent project from Nexus and installs it into CRX.
For this import this into the project main pom.xml file,
<dependency>
<groupId>com.aem.demo</groupId>
<artifactId>aem-demo.all</artifactId>
<type>zip</type>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.aem.demo</groupId>
<artifactId>aem-demo.core</artifactId>
<version>1.0.0</version>
</dependency>In ui.apps > pom.xml
<dependency>
<groupId>com.aem.demo</groupId>
<artifactId>aem-demo.all</artifactId>
<type>zip</type>
</dependency>
<dependency>
<groupId>com.aem.demo</groupId>
<artifactId>aem-demo.core</artifactId>
<type>jar</type>
</dependency>In core > pom.xml
<dependency>
<groupId>com.aem.demo</groupId>
<artifactId>aem-demo.core</artifactId>
</dependency>In all > pom.xml
<embedded>
<groupId>com.aem.demo</groupId>
<artifactId>aem-demo.all</artifactId>
<target>/apps/my-project-vendor-packages/application/install</target>
</embedded>Usages
Component Level:
Extend the HelloWorld component from aem-project-demo and see the result. All the related data are coming from the imported bundle.
Service Level:
Also, Create a Service in AEM Demo Project and use these codes from different bundles.
The related changes can be found here: https://github.com/Sady-Rifat/my-project/commit/95c07f90feac9cddfd90b10069a01883af830cf0
Integrating Sonatype Nexus into your AEM development workflow offers a streamlined approach to managing project builds and dependencies. By leveraging Nexus as a centralized repository manager, you can efficiently store, share, and retrieve artifacts, facilitating collaboration and enhancing the reliability of your AEM projects. With the ability to import Maven dependencies directly from Nexus, you can ensure seamless integration of external components into your AEM applications.