Install third party artifacts - not available in the public Maven repository
Learn how to install third-party artifacts that are not available in the public Maven repository when building and deploying an AEM project.
The third-party artifacts can be:
- OSGi bundle: An OSGi bundle is a Java™ archive file that contains Java classes, resources, and a manifest that describes the bundle and its dependencies.
- Java jar: A Java™ archive file that contains Java classes and resources.
- Package: A package is a zip file containing repository content in file-system serialization form.
Standard scenario
Typically you would install the third-party bundle, package that is available in the public Maven repository as a dependency in your AEM project’s pom.xml file.
For example:
-
AEM WCM Core Components bundle is added as a dependency in the WKND project’s
pom.xmlfile. Here theprovidedscope is used as the AEM WCM Core Components bundle is provided by the AEM runtime. If the bundle is not provided by the AEM runtime, you would use thecompilescope and it is the default scope. -
WKND Shared package is added as a dependency in the WKND project’s
pom.xmlfile.
Rare scenario
Occasionally, when building and deploying an AEM project, you may need to install a third-party bundle or jar or package that is not available in the Maven Central Repository or the Adobe Public Repository.
The reasons could be:
-
The bundle or package is provided by an internal team or third-party vendor and is not available in the public Maven repository.
-
The Java™ jar file is not an OSGi bundle and may or may not be available in the public Maven repository.
-
You need a feature that is not yet released in the latest version of the third-party package available in the public Maven repository. You decided to install the locally built RELEASE or SNAPSHOT version.
Prerequisites
To follow this tutorial, you need:
-
The local AEM development environment or Rapid Development Environment(RDE) set-up.
-
The AEM WKND project to add the third-party bundle or jar or package and verify the changes.
Set-up
-
Set up the AEM 6.X or AEM as a Cloud Service (AEMCS) local development environment or RDE environment.
-
Clone and deploy the AEM WKND project.
code language-none $ git clone git@github.com:adobe/aem-guides-wknd.git $ cd aem-guides-wknd $ mvn clean install -PautoInstallPackageVerify the WKND site pages render correctly.
Install a third-party bundle in an AEM project install-third-party-bundle
Let’s install & use a demo OSGi my-example-bundle that is not available in the public Maven repository to the AEM WKND project.
The my-example-bundle exports HelloWorldService OSGi service, its sayHello() method returns Hello Earth! message.
For more details, refer to the README.md file in the my-example-bundle.zip file.
Add the bundle to all module
The first step is to add the my-example-bundle to the AEM WKND project’s all module.
-
Download and extract the my-example-bundle.zip file.
-
In the AEM WKND project’s
allmodule, create theall/src/main/content/jcr_root/apps/wknd-vendor-packages/container/installdirectory structure. The/all/src/main/contentdirectory exists, you only need to create thejcr_root/apps/wknd-vendor-packages/container/installdirectories. -
Copy the
my-example-bundle-1.0-SNAPSHOT.jarfile from the extractedtargetdirectory to the aboveall/src/main/content/jcr_root/apps/wknd-vendor-packages/container/installdirectory.
Use the service from the bundle
Let’s use the HelloWorldService OSGi service from the my-example-bundle in the AEM WKND project.
-
In the AEM WKND project’s
coremodule, create theSayHello.javaSling servlet @core/src/main/java/com/adobe/aem/guides/wknd/core/servlet.code language-java package com.adobe.aem.guides.wknd.core.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletException; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.ServletResolverConstants; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import com.example.services.HelloWorldService; @Component(service = Servlet.class, property = { ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/sayhello", ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET }) public class SayHello extends SlingSafeMethodsServlet { private static final long serialVersionUID = 1L; // Injecting the HelloWorldService from the `my-example-bundle` bundle @Reference private HelloWorldService helloWorldService; @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // Invoking the HelloWorldService's `sayHello` method response.getWriter().write("My-Example-Bundle service says: " + helloWorldService.sayHello()); } } -
In the AEM WKND project’s root
pom.xmlfile, add themy-example-bundleas a dependency.code language-xml ... <!-- My Example Bundle --> <dependency> <groupId>com.example</groupId> <artifactId>my-example-bundle</artifactId> <version>1.0-SNAPSHOT</version> <scope>system</scope> <systemPath>${maven.multiModuleProjectDirectory}/all/src/main/content/jcr_root/apps/wknd-vendor-packages/container/install/my-example-bundle-1.0-SNAPSHOT.jar</systemPath> </dependency> ...Here:
- The
systemscope indicates that the dependency should not be looked up in the public Maven repository. - The
systemPathis the path to themy-example-bundlefile in the AEM WKND project’sallmodule. - The
${maven.multiModuleProjectDirectory}is a Maven property that points to the root directory of the multi-module project.
- The
-
In the AEM WKND project’s
coremodule’score/pom.xmlfile, add themy-example-bundleas a dependency.code language-xml ... <!-- My Example Bundle --> <dependency> <groupId>com.example</groupId> <artifactId>my-example-bundle</artifactId> </dependency> ... -
Build and deploy the AEM WKND project using the following command:
code language-none $ mvn clean install -PautoInstallPackage -
Verify the
SayHelloservlet works as expected by accessing the URLhttp://localhost:4502/bin/sayhelloin the browser. -
Commit above changes to the AEM WKND project’s repository. Then verify the changes in the RDE or AEM environment by running the Cloud Manager pipeline.
The tutorial/install-3rd-party-bundle branch of the AEM WKND project has the above changes for your reference.
Key learnings key-learnings-bundle
The OSGi bundles that are not available in the public Maven repository can be installed in an AEM project by following these steps:
-
Copy the OSGi bundle to the
allmodule’sjcr_root/apps/<PROJECT-NAME>-vendor-packages/container/installdirectory. This step is necessary to package and deploy the bundle to the AEM instance. -
Update the root and core module’s
pom.xmlfiles to add the OSGi bundle as a dependency with thesystemscope andsystemPathpointing to the bundle file. This step is necessary to compile the project.
Install a third-party jar in an AEM project
In this example, the my-example-jar is not an OSGi bundle, but a Java jar file.
Let’s install & use a demo my-example-jar that is not available in the public Maven repository to the AEM WKND project.
The my-example-jar is a Java jar file that contains a MyHelloWorldService class with a sayHello() method that returns Hello World! message.
For more details, refer to the README.md file in the my-example-jar.zip file.
Add the jar to all module
The first step is to add the my-example-jar to the AEM WKND project’s all module.
-
Download and extract the my-example-jar.zip file.
-
In the AEM WKND project’s
allmodule, create theall/resource/jardirectory structure. -
Copy the
my-example-jar-1.0-SNAPSHOT.jarfile from the extractedtargetdirectory to the aboveall/resource/jardirectory.
Use the service from the jar
Let’s use the MyHelloWorldService from the my-example-jar in the AEM WKND project.
-
In the AEM WKND project’s
coremodule, create theSayHello.javaSling servlet @core/src/main/java/com/adobe/aem/guides/wknd/core/servlet.code language-java package com.adobe.aem.guides.wknd.core.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletException; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.ServletResolverConstants; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; import org.osgi.service.component.annotations.Component; import com.my.example.MyHelloWorldService; @Component(service = Servlet.class, property = { ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/sayhello", ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET }) public class SayHello extends SlingSafeMethodsServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // Creating an instance of MyHelloWorldService MyHelloWorldService myHelloWorldService = new MyHelloWorldService(); // Invoking the MyHelloWorldService's `sayHello` method response.getWriter().write("My-Example-JAR service says: " + myHelloWorldService.sayHello()); } } -
In the AEM WKND project’s root
pom.xmlfile, add themy-example-jaras a dependency.code language-xml ... <!-- My Example JAR --> <dependency> <groupId>com.my.example</groupId> <artifactId>my-example-jar</artifactId> <version>1.0-SNAPSHOT</version> <scope>system</scope> <systemPath>${maven.multiModuleProjectDirectory}/all/resource/jar/my-example-jar-1.0-SNAPSHOT.jar</systemPath> </dependency> ...Here:
- The
systemscope indicates that the dependency should not be looked up in the public Maven repository. - The
systemPathis the path to themy-example-jarfile in the AEM WKND project’sallmodule. - The
${maven.multiModuleProjectDirectory}is a Maven property that points to the root directory of the multi-module project.
- The
-
In the AEM WKND project’s
coremodule’score/pom.xmlfile, make two changes:-
Add the
my-example-jaras a dependency.code language-xml ... <!-- My Example JAR --> <dependency> <groupId>com.my.example</groupId> <artifactId>my-example-jar</artifactId> </dependency> ... -
Update
bnd-maven-pluginconfiguration to include themy-example-jarinto the OSGi bundle (aem-guides-wknd.core) that is being built.code language-xml ... <plugin> <groupId>biz.aQute.bnd</groupId> <artifactId>bnd-maven-plugin</artifactId> <executions> <execution> <id>bnd-process</id> <goals> <goal>bnd-process</goal> </goals> <configuration> <bnd><![CDATA[ Import-Package: javax.annotation;version=0.0.0,* <!-- Include the 3rd party jar as inline resource--> -includeresource: \ lib/my-example-jar.jar=my-example-jar-1.0-SNAPSHOT.jar;lib:=true ]]></bnd> </configuration> </execution> </executions> </plugin> ...
-
-
Build and deploy the AEM WKND project using the following command:
code language-none $ mvn clean install -PautoInstallPackage -
Verify the
SayHelloservlet works as expected by accessing the URLhttp://localhost:4502/bin/sayhelloin the browser. -
Commit above changes to the AEM WKND project’s repository. Then verify the changes in the RDE or AEM environment by running the Cloud Manager pipeline.
The tutorial/install-3rd-party-jar branch of the AEM WKND project has the above changes for your reference.
In scenarios where the Java jar file is available in the public Maven repository but is NOT an OSGi bundle, you can follow the above steps except the <dependency>'s system scope and systemPath elements are not required.
Key learnings key-learnings-jar
The Java jars that are not OSGi bundles and may or may not be available in the public Maven repository can be installed in an AEM project by following these steps:
- Update the
bnd-maven-pluginconfiguration in the core module’spom.xmlfile to include the Java jar as an inline resource in the OSGi bundle that is being built.
The following steps are only required if the Java jar is not available in the public Maven repository:
-
Copy the Java jar to the
allmodule’sresource/jardirectory. -
Update the root and core module’s
pom.xmlfiles to add the Java jar as a dependency with thesystemscope andsystemPathpointing to the jar file.
Install a third-party package in an AEM project
Let’s install the ACS AEM Commons SNAPSHOT version built locally from the main branch.
It is purely done to demonstrate the steps to install an AEM package that is not available in the public Maven repository.
The ACS AEM Commons package is available in the public Maven repository. Refer the Add ACS AEM Commons to your AEM Maven project to add it to your AEM project.
Add the package to all module
The first step is to add the package to the AEM WKND project’s all module.
-
Comment or remove the ACS AEM Commons release dependency from the POM file. Refer the Add ACS AEM Commons to your AEM Maven project to identify the dependency.
-
Clone the
masterbranch of the ACS AEM Commons repository to your local machine. -
Build the ACS AEM Commons SNAPSHOT version using the following command:
code language-none $mvn clean install -
The locally built package is located @
all/target, there are two .zip files, the one ending with-cloudis meant for AEM as a Cloud Service and the other one is for AEM 6.X. -
In the AEM WKND project’s
allmodule, create theall/src/main/content/jcr_root/apps/wknd-vendor-packages/container/installdirectory structure. The/all/src/main/contentdirectory exists, you only need to create thejcr_root/apps/wknd-vendor-packages/container/installdirectories. -
Copy the locally built package (.zip) file to the
/all/src/main/content/jcr_root/apps/mysite-vendor-packages/container/installdirectory. -
Build and deploy the AEM WKND project using the following command:
code language-none $ mvn clean install -PautoInstallPackage -
Verify the installed ACS AEM Commons package:
-
CRX Package Manager @
http://localhost:4502/crx/packmgr/index.jsp
-
The OSGi console @
http://localhost:4502/system/console/bundles
-
-
Commit above changes to the AEM WKND project’s repository. Then verify the changes in the RDE or AEM environment by running the Cloud Manager pipeline.
Key learnings key-learnings-package
The AEM packages that are not available in the public Maven repository can be installed in an AEM project by following these steps:
- Copy the package to the
allmodule’sjcr_root/apps/<PROJECT-NAME>-vendor-packages/container/installdirectory. This step is necessary to package and deploy the package to the AEM instance.
Summary
In this tutorial, you learned how to install third-party artifacts (bundle, Java jar and package) that are not available in the public Maven repository when building and deploying an AEM project.