How to use third party JAR into AEM? - aem

I have a jar file that i received from other team and need to use in AEM. I can not use jar directly in AEM so i converted the Jar into bundle with help of the link "https://helpx.adobe.com/experience-manager/kb/ConvertAJarIntoOsgiBundle.html" , Now my bundle is ready and uploaded into AEM through felix console. Bundle is active. Now i need use the classe which are there in bundle to my java classes. How to use that bunlde in java classes . do i need to added the bundle in POM.xml? if so then how i can use that bundle into POM.xml so that my code can complile.

Now my bundle is ready and uploaded into AEM through felix console
That is not a good idea. Yes, you can install bundles from the Felix console but bundles installations in AEM ideally should be managed by the Sling OSGi Installer which can scan the JCR repository for bundles.
As said in the other response, you should put your bundle in a folder named "install" below the /apps folder.
My recommendation would be to use the Maven Content Package Plugin that is used to generate AEM comments to embedd your bundle in your AEM package:
<build>
<plugins>
<plugin>
<groupId>com.day.jcr.vault</groupId>
<artifactId>content-package-maven-plugin</artifactId>
<configuration>
<failOnMissingEmbed>true</failOnMissingEmbed>
<filterSource>src/main/META-INF/vault/filter.xml</filterSource>
<filters combine.self="override" />
<embeddeds>
<embedded>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.validation-impl</artifactId>
<target>/apps/example/install</target>
</embedded>
</embeddeds>
</configuration>
</plugin>
</plugins>
</build>
Also, dont forget to add /apps/example/install to your filter.xml.
More information on the content package plugin

You can put your lib into src/main/jcr_root/apps/your_app/libs/install folder(path depends on your project structure). Now it will be installed to AEM using maven.
To import necessary classes use provided scope, we have the following configuration for Jedis lib:
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jedis</artifactId>
<version>2.7.3_1</version>
<scope>provided</scope>
</dependency>

Related

AEM How to update POI version in OSGi Bundle Wrapper For The Apache POI Library?

In AEM, there's a bundle named "OSGi Bundle Wrapper For The Apache POI Library".
I want to update POI version of that bundle to use POI 5.2.3 with AEM 6.3.
Is there's any way to easily update POI version in this bundle?
Thanks.
Apache POI provides a module to build an all-in-one OSGi bundle, see https://github.com/apache/poi/tree/trunk/osgi
It can either replace the OOB POI bundle, or you can build your own one on top of it, having the POI code in the internal classpath.
In your custom core bundle pom, you may specify embed dependency for the poi version you require. So that your bundle will refer to the poi version from embedded jar and not provided by AEM container.
Have you tried using the tag of maven-bundle-plugin -
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version></version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Name>${bundle.name}</Bundle-Name>
<Export-Package>${bundle.namespace}</Export-Package>
<Bundle-Activator>Activator.class</Bundle-Activator>
<Embed-Dependency>*;scope=!provided|test;groupId=!org.mybundle.test</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Import-Package>*;resolution:=optional</Import-Package>
</instructions>
</configuration>
</plugin>
You can either include all the dependencies or you can configure to not to include specific ones. The scope attribute lets you configure which dependency jars you want to include based on their include scope. You should not include the dependencies which are only needed for compile and for test.
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-embed-dependent-jar/m-p/225793

Eclipse plugin with Maven dependencies for third party libraries

I am converting Subclipse to build with Eclipse Tycho and Maven.
Subclipse depends on a few third party JAR files that are not Eclipse plugins so do not exist in any p2 repository. Currently, I just include these in a lib folder within the plugin.
Since these JAR files do exist in Maven, I was hoping that by converting the plugins to build with Maven I could use Maven dependencies. IOW, the plugin would have a pom.xml where I used Maven dependencies to grab and include the third party jar's that have to be included in the plugin. Basically, it would just automate having to refresh what I include in the lib folder of the plugin.
Is this possible? I tried doing what I said above by when I build, I saw no sign that Maven/Tycho was trying to fetch the dependencies. I imagine it is because when the packaging is eclipse-plugin it looks solely at the Eclipse configuration files for the dependency information.
Thanks
To add plain (without OSGi metadata) jar files into your folder at biuild time, you can specify an <execution> of the maven-dependency-plugin to fetch them. However it will require to update your MANIFEST.MF Bundle-Classpath directive whenever a version changes.
It's usually better to hunt for OSGi-able jars or to make an extra effort to package existing libs as OSGi bundles/p2 artifacts like Eclipse Orbit or JBoss Tools Locus do.
Did you try doing the following after adding the dependencies to the pom.xml file?
Project->Clean
Right click on project: Maven->Update dependencies
Right click on project: Maven->Update project configuration
Just adding the plugin to pom dependencies and including the entry <pomDependencies>consider</pomDependencies> in the configuration of target-platform-configuration makes it work.
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<!-- The configuration to make tycho consider the maven dependencies -->
<pomDependencies>consider</pomDependencies>
<!-- other configurations -->
</configuartion>
</plugin>
<!-- other plugins-->
</plugins>
<dependencies>
<!-- An example third-party bundle (plugin) present in maven repository-->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.shell</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Reference link here.

Service Component missing in AEM

I am trying to create a project in Eclipse using the AEM Developer Tools plugin. The project is published in AEM server and can be seen in: Websites and CRXDE Lite.
The project was created using Maven Archetype-10 in Eclipse and the default project has two pages in English and French. When I open the page in the Site URL, I get the end page, where the message from the HelloWorldModel class should be displayed, along with the other text. But I don't get the message from the respective class. Instead, I am getting the following error message:
org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.sightly.SightlyException: Cannot find a a file corresponding to class com.pen.mypen.core.models.HelloWorldModel in the repository.
It looks like the Java files in the CORE project are not published or are not visible to the web page. But the 3 packages: Core, Apps, and Content are all published and synchronized in Eclipse. Is there any way to check if the Java package is deployed to AEM server? Please guide me to what I am missing here. Please find the error stack trace below:
Caused by: org.apache.sling.scripting.sightly.SightlyException: Cannot find a a file corresponding to class com.pen.mypen.core.models.HelloWorldModel in the repository.
at org.apache.sling.scripting.sightly.impl.compiler.SightlyJavaCompilerService.compileRepositoryJavaClass(SightlyJavaCompilerService.java:212)
at org.apache.sling.scripting.sightly.impl.compiler.SightlyJavaCompilerService.getInstance(SightlyJavaCompilerService.java:113)
at org.apache.sling.scripting.sightly.impl.engine.extension.use.JavaUseProvider.provide(JavaUseProvider.java:127)
at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:84)
at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:66)
at org.apache.sling.scripting.sightly.apps.pen.components.content.helloworld.SightlyJava_helloworld.render(SightlyJava_helloworld.java:53)
at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderUnit.render(RenderUnit.java:54)
at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.evaluateScript(SightlyScriptEngine.java:92)
at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.eval(SightlyScriptEngine.java:78)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388)
UPDATE
AEM - 6.2
Eclipse - Luna
Java - 1.8
You can check the following things:
http://SERVER:PORT/system/console/bundles shows all the java bundles that are installed on your AEM machine. Check if your bundle is listed there. If not then it could not be installed => check the error log
If your bundle is listed there, check if it is status is active, if not there might be missing dependencies or stuff like that. => click the bundle name to see more details what went wron, also check the error log.
If the status is active, click on the bundle and see if the package that contains your java class is listed in the Exported Packages section.
If the package is listed, you might need to check the error log (again).
UPDATE (because of AEM 6.2):
The problem with your project is, that with AEM versions prior to 6.2, there was a dependency to javax.inject version 0 whereas now they need version 1 and that's not reflected in the pom.xml.
Just add this to your pom.xml of the core project - that's the only change you'll have to do:
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
and everything will work. Sorry for the confusion...
In case you are using SlingModels the javax.inject is exposed by the org.apache.sling.models.api
Can you check your pom for the SlingModel dependencies
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
<version>VERSION_NUMBER</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.impl</artifactId>
<version>VERSION_NUMBER</version>
<scope>provided</scope>
</dependency>
Check the VERSION_NUMBER you are using and then in /system/console/bundles verify that the same version bundle is present and Active.
With AEM 6.x the Sling Model bundles are available by default. The 6.0 will have a lower version 1.0.x. Fixing the version either by updating the pom or installing the SlingModel bundles with version VERSION_NUMBER fix your issue.
Update for 6.2
For 6.2 with models with version 1.2.2 use following in dependencies -
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
<version>1.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.impl</artifactId>
<version>1.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
geronimo-atinject_1.0_spec is the one that is exposing javax.inject in sling models 1.2.2
Sharing my solution which will be helpful.
Tested on AEM 6.2 working perfect.
In core pom.xml file add Import-Package tag with "javax.inject;version=0.0.0,*" inside "org.apache.felix" plugin tag.
Following is the sample :
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<!--
<Embed-Dependency>
artifactId1,artifactId2;inline=true
</Embed-Dependency>
-->
<!-- Import any version of javax.inject, to allow running on multiple versions of AEM -->
<Import-Package>javax.inject;version=0.0.0,*</Import-Package>
<Sling-Model-Packages>
com.next.sample_test_impl.core
</Sling-Model-Packages>
</instructions>
</configuration>
</plugin>

Jetty Maven plugin - Put properties file in classpath

I'm using the eclipse jetty plugin in my project:
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.3.v20130506</version>
I have my application that read a file in the classpath of the server, but I can't manage to put it in the Jetty classpath...
I would to put it in a folder in my project and make that folder part of the Jetty classpath, how can I do it?
Is there a better solution to make that file external to the application?
It was just a matter of configuration of the maven plugin
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<jettyXml>jetty/jetty.xml</jettyXml>
<webApp>
<extraClasspath>${basedir}/jetty/classpath</extraClasspath>
</webApp>
</configuration>
to add a classpath folder to Jetty, it needs the extraClasspath folder in the webApp tag.

Maven dependency and eclipse classpath

I tried to search for existing questions but cant find any - feels like my question is quite simple but probably because it's quite specific I cant find the answers on Stackoverflow / Google.
Anyways - I have few projects with Maven that are depend on each other. In certain cases I want the dependency to be on the JAR rather than a project dependency (ie. I want the dependency to be part of the "Libraries" in Eclipse rather than "Projects" in the Build Path).
Your help is greatly appreciated! Thanks
To get the referenced projects in the same workspace as jar files instead of the projects, we could use the VM parameter -Declipse.useProjectReferences=false or add it in the pom file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<useProjectReferences>false</useProjectReferences>
</configuration>
</plugin>
See this URL for more info.
In your project properties in Eclipse, select Maven. There is a checkbox, 'Resolve dependencies from workspace projects'. If this is checked, then the Eclipse projects are used. Otherwise the jars are used as with other maven dependencies, assuming that you've got the dependencies in your pom.xml as normal.
EDIT: If your project is not a maven project, then you'll have to create the jar outside Eclipse and add it as a jar or external jar as normal. If the project is a maven project, then the above will work.
Say Client-Project depends on Services-Project. If Services-Project generates a JAR. In the Client-Project POM you would express a dependency on this JAR. It would be something like:
<dependency>
<groupId>group.id.of.services.project.goes.here</groupId>
<artifactId>artifact.id.of.services.project.goes.here</artifactId>
<version>version.number.of.services.jar</version>
</dependency>
If services project generates a JAR called com.mycompany.services-1.3.jar, the dependency would be:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>services</artifactId>
<version>1.3</version>
</dependency>