Expand my Community achievements bar.

SOLVED

Unable to resolve twilio dependancy issue in aem

Avatar

Employee

Hi Everyone,

      I have to use twilio message api [https://www.twilio.com/en-us], For that I have added dependancy in core pom file like below to use their apis.

 

core pom.xml:

<!-- communication web API https://mvnrepository.com/artifact/com.twilio.sdk/twilio -->
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>10.1.5</version>
</dependency>

 

Post this, I got below build error:

if I build core module- build is success but getting cannot be resolved issue.

jeromeleslyv_0-1714713535460.png

 

if I build whole project I got below build error:

[ERROR] The analyser found the following errors for author and publish :
[ERROR] [api-regions-exportsimports] com.sampleproject:sampleProject.core:1.0.0-SNAPSHOT: Bundle sampleProject.core:1.0.0-SNAPSHOT is importing pack
age(s) [com.twilio.type, com.twilio, com.twilio.rest.api.v2010.account, com.twilio.base] in start level 20 but no bundle is exporting these for that start level. (com.sampleproject:sampleProject.all:1.0.0-SNAPSHOT)

 

Due to above error I have embeded this in all pom.xml and I got another build error like below:

all pom.xml

<embedded>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<type>jar</type>
<target>/apps/sampleProject-packages/application/install</target>
</embedded>

Post this, I got below build error:

[ERROR] Failed to execute goal com.adobe.aem:aemanalyser-maven-plugin:1.4.10:project-analyse (aem-analyser) on project sampleProject.all: A fatal er
ror occurred while analysing the features, see error cause:: Unable to get bundle symbolic name from artifact com.twilio.sdk:twilio:10.1.5 -> [Help 1]

 

FYI - It looke like not a bundle, it's jar file. So I need your help to solve this dependancy issue, Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@jeromeleslyv 

I was just checking the build success message and confirmed that it's working. 

I work on your example and faced the same issue. Below is the recommendation 

 

Step 1: convert the Jar to OSGi bundle refer to below link

https://experienceleague.adobe.com/en/docs/experience-cloud-kcs/kbarticles/ka-17475

 

Step 2: load the bundle from your project location or from your separate artifact repo

 

Step 3: 

embed the package as shown in below plugin example

 

<embedded>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<type>jar</type>
<target>/apps/<your-project>/application/install</target>
</embedded>

 

<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<type>jar</type>
<scope>system</scope>
<systemPath>${project.basedir}/../<your-project>.ui.apps/src/main/resources/twilio-10.1.5.jar</systemPath>
</dependency>

View solution in original post

10 Replies

Avatar

Community Advisor

@jeromeleslyv 

The Twilio SDK is not an OSGi bundle out of the box, so you cannot directly include it as a dependency in your AEM project.

  1. Embed the Non-OSGi Dependencies: You can embed the Twilio SDK in your project's bundle. This will include the Twilio SDK classes directly in your bundle, allowing you to use them even though they're not OSGi bundles. You can do this by adding the <Embed-Dependency> instruction in your maven-bundle-plugin configuration in your pom.xml:<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
    <instructions>
    <Embed-Dependency>twilio;scope=compile|runtime;inline=true</Embed-Dependency>
    <!-- other instructions -->
    </instructions>
    </configuration>
    </plugin>
  2. Convert the Dependencies to OSGi Bundles: You can use the Apache Felix Maven Bundle Plugin to convert the Twilio SDK into an OSGi bundle. This involves creating a separate project that wraps the Twilio SDK and adds the necessary OSGi metadata. This is a more complex solution, but it allows you to manage the Twilio SDK as a separate bundle in your AEM instance.

Avatar

Employee

I have added like this in core pom.xml is this correct?

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Embed-Dependency>twilio;scope=compile|runtime;inline=true</Embed-Dependency>
<Bundle-SymbolicName>com.twilio.sdk.twilio</Bundle-SymbolicName>
<Bundle-Name>twilio</Bundle-Name>
<Bundle-Version>10.1.5</Bundle-Version>
<Private-Package>com.twilio.*</Private-Package>
</instructions>
</configuration>
</plugin>

Avatar

Employee

After raw build without servlet code, it got success but didn't deployed to aem, But If i run with one simple servlet which dependent on this getting compilation error 

[ERROR] /D:/My Project/sampleProject/core/src/main/java/com/sampleproject/core/servlets/MessageServlet.java:[14,18] package com.twilio does not exist
[ERROR] /D:/My Project/sampleProject/core/src/main/java/com/sampleproject/core/servlets/MessageServlet.java:[15,41] package com.twilio.rest.api.v2010.account does not exist
[ERROR] /D:/My Project/sampleProject/core/src/main/java/com/sampleproject/core/servlets/MessageServlet.java:[16,23] package com.twilio.type does not exist

Avatar

Community Advisor

@jeromeleslyv  try adding below dependency 

 

<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>8.22.0</version> <!-- Replace with the latest version -->
</dependency>
</dependencies>
 
 
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<!-- Other configuration -->
<configuration>
<instructions>
<!-- Other instructions -->
<Embed-Dependency>
twilio;scope=compile|runtime;inline=true
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>lib</Embed-Directory>
<Include-Resource>{maven-resources},{maven-dependencies}</Include-Resource>
</instructions>
</configuration>
</plugin>
 
 
This worked for me in local

Avatar

Employee

@Jagadeesh_Prakash , I have added whatever you have mentioned in previous chat. Still I'm getting below error, can you try with below servlet?
ERROR:
[ERROR] The analyser found the following errors for author and publish :
[ERROR] [api-regions-exportsimports] com.sampleproject:sampleProject.core:1.0.0-SNAPSHOT: Bundle sampleProject.core:1.0.0-SNAPSHOT is importing pack
age(s) [com.twilio.type, com.twilio, com.twilio.rest.api.v2010.account, com.twilio.base] in start level 20 but no bundle is exporting these for that start level. (com.sampleproject:sampleProject.all:1.0.0-SNAPSHOT)

Servlet:
servlet call won't work in your post call but maven build should be success

package com.sampleproject.core.servlets;

import lombok.extern.slf4j.Slf4j;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import java.io.IOException;

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS;

@Component(service = { Servlet.class },
property = {
SLING_SERVLET_PATHS + "=/bin/sendmessage"
})
@Slf4j
public class MessageServlet extends SlingAllMethodsServlet {
public static final String ACCOUNT_SID = "abcd";
public static final String AUTH_TOKEN = "efgh";

@Override
protected void doPost( SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Twilio.init("abcd", "efgh");
Message message = Message.creator(
new com.twilio.type.PhoneNumber("+919700606004"), //To number
new com.twilio.type.PhoneNumber("+13347006328"), //from number
"Hi, Admin sending a test message?")
.create();
log.debug(message.getSid());
response.getWriter().write("responsemessage:" +message.getSid());
}
}




Avatar

Correct answer by
Community Advisor

@jeromeleslyv 

I was just checking the build success message and confirmed that it's working. 

I work on your example and faced the same issue. Below is the recommendation 

 

Step 1: convert the Jar to OSGi bundle refer to below link

https://experienceleague.adobe.com/en/docs/experience-cloud-kcs/kbarticles/ka-17475

 

Step 2: load the bundle from your project location or from your separate artifact repo

 

Step 3: 

embed the package as shown in below plugin example

 

<embedded>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<type>jar</type>
<target>/apps/<your-project>/application/install</target>
</embedded>

 

<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<type>jar</type>
<scope>system</scope>
<systemPath>${project.basedir}/../<your-project>.ui.apps/src/main/resources/twilio-10.1.5.jar</systemPath>
</dependency>

Avatar

Administrator

@jeromeleslyv Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni