I'm having a slight problem with incorporating the appropriate maven dependencies into my project for sling models.
When I deploy my bundle, I get the following import that can't be resolved:
org.apache.sling.models.annotations,version=[1.1,2) -- Cannot be resolved
I believe I have included this with the following dependencies:
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.impl</artifactId>
</dependency>
I tried using:
<Embed-Transitive>true</Embed-Transitive>
<Import-Package>*</Import-Package>
in my bundle compile instructions, but this has just resulted in a ton of other dependencies not being resolved.
Surely I've gone down the garden path here somewhere. Any help would be greatly appreciated.
The org.apache.sling.models.api V1.0.2 bundle does export the following packages:
javax.inject,version=0.0.0
org.apache.sling.models.annotations,version=1.1.0
org.apache.sling.models.annotations.injectorspecific,version=1.0.0
org.apache.sling.models.spi,version=1.0.0
org.apache.sling.models.spi.injectorspecific,version=1.0.0
So if that bundle is active in your Sling instance, the org.apache.sling.models.annotations,version=[1.1,2) import should resolve.
Note that adding bundles to your maven dependencies might not be sufficient to install them in the running instance, what matters is whether the models.api bundle is active as seen from /system/console/bundles
Using Embed-Transitive is almost always a terrible idea. It traverses the entire transitive dependency hierarchy in Maven and pulls each one of those JARs into your own JAR. As a result you inherit all the package dependencies of all that crap you have dragged in.
When you have a bundle such as yours that requires an import -- in this case org.apache.sling.models.annotations -- the best solution is to find another bundle already available that exports the same package.
Related
I am trying to import
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.directory.ldap.client.api.DefaultLdapConnectionFactory;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapConnectionConfig;
import org.apache.directory.ldap.client.api.LdapConnectionPool;
import org.apache.directory.ldap.client.api.ValidatingPoolableLdapConnectionFactory;
import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
and using
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-ldap-client-api</artifactId>
<version>2.0.0.AM4</version>
</dependency>
in the parent pom and
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-ldap-client-api</artifactId>
</dependency>
in the bundle pom.
The issue is
- The artifact is not present in osgi after build and
- Project bundle is in resoved state due to error
org.apache.commons.pool2,version=[2.6,3) -- Cannot be resolved
org.apache.commons.pool2.impl,version=[2.6,3) -- Cannot be resolved
org.apache.directory.ldap.client.api,version=[2.0,3) -- Cannot be resolved
org.apache.directory.ldap.client.template,version=[2.0,3) -- Cannot be resolved
UPDATED QUESTION -
"How to resolve these dependencies? Which ldap dependency/library
could be used in OSGi?"
Objective - I'm trying to connect LDAP
LdapConnection connection = new LdapNetworkConnection( "localhost", 10389 );
Reference - LDAP Connection documentation
You have to distinguish between build time and runtime dependencies. In Maven you define your build time dependencies. Per default they have nothing to do with the bundles installed in AEM (runtime dependencies).
To make it clear:
Dependencies defined in Maven are not automatically installed into AEM.
There are a few options how you can deploy the required runtime dependencies to AEM:
Install them by hand (/system/console/bundles)
Put them in a content package and deploy that content package by hand.
Extend your Maven build to create a content-package which includes the bundles you need at runtime (e.g. org.apache.directory.api:api-ldap-client-api:2.0.0.AM4)
Install the bundles using the install directory of AEM on the hard disk: crx-quickstart/install.
All of them have advantages and disadvantages. I usually opt for option #3. I have written a lengthy answer that explains this here: https://stackoverflow.com/a/56698917/190823
If you do not require them, you can exclude them in the section, by doing something like this:
<configuration>
<instructions>
<Import-Package>
!org.apache.commons.pool2,
And so on.
I am trying to import
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.directory.ldap.client.api.DefaultLdapConnectionFactory;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapConnectionConfig;
import org.apache.directory.ldap.client.api.LdapConnectionPool;
import org.apache.directory.ldap.client.api.ValidatingPoolableLdapConnectionFactory;
import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
and using
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-ldap-client-api</artifactId>
<version>2.0.0.AM4</version>
</dependency>
in the parent pom and
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-ldap-client-api</artifactId>
</dependency>
in the bundle pom.
The issue is
- The artifact is not present in osgi after build and
- Project bundle is in resoved state due to error
org.apache.commons.pool2,version=[2.6,3) -- Cannot be resolved
org.apache.commons.pool2.impl,version=[2.6,3) -- Cannot be resolved
org.apache.directory.ldap.client.api,version=[2.0,3) -- Cannot be resolved
org.apache.directory.ldap.client.template,version=[2.0,3) -- Cannot be resolved
UPDATED QUESTION -
"How to resolve these dependencies? Which ldap dependency/library
could be used in OSGi?"
Objective - I'm trying to connect LDAP
LdapConnection connection = new LdapNetworkConnection( "localhost", 10389 );
Reference - LDAP Connection documentation
You have to distinguish between build time and runtime dependencies. In Maven you define your build time dependencies. Per default they have nothing to do with the bundles installed in AEM (runtime dependencies).
To make it clear:
Dependencies defined in Maven are not automatically installed into AEM.
There are a few options how you can deploy the required runtime dependencies to AEM:
Install them by hand (/system/console/bundles)
Put them in a content package and deploy that content package by hand.
Extend your Maven build to create a content-package which includes the bundles you need at runtime (e.g. org.apache.directory.api:api-ldap-client-api:2.0.0.AM4)
Install the bundles using the install directory of AEM on the hard disk: crx-quickstart/install.
All of them have advantages and disadvantages. I usually opt for option #3. I have written a lengthy answer that explains this here: https://stackoverflow.com/a/56698917/190823
If you do not require them, you can exclude them in the section, by doing something like this:
<configuration>
<instructions>
<Import-Package>
!org.apache.commons.pool2,
And so on.
I'm having trouble correctly importing a library into a project that I'm running. I have added the library as a dependency in the .pom, refreshed the pom, run mvn clean install, and I have set auto-import up so that the project gets updated correctly, but the project does not get added as an External Library, and I can't use it in my project. I get no errors. What am I doing wrong?
Here is the relevant part of my pom
..properties
<crowd.version>2.5.0</crowd.version>
.. end properties
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.atlassian.crowd</groupId>
<artifactId>crowd-integration-springsecurity</artifactId>
<version>${crowd.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
Here is the question I was following to debug my error:
Import Maven dependencies in IntelliJ IDEA
I think you missed the point of dependency management; read more in official docs. This is a feature that you can centralize common dependency information that is then shared been different projects. All by itself, this definioition will not import the dependency.
What you probably want is just a plain dependency: drop the dependencyManagement tags, and move you dependency into the correct block in the pom.
I am trying to use Jsoup library by creating an OSGi bundle out of it. I have created the bundles before so I am pretty much familiar with the process. I have also added the dependencies in the pom.xml like this:
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.3</version>
</dependency>
I am also following the steps given here: https://helpx.adobe.com/experience-manager/using/html-parser-service.html
I have tried everything but still the dependencies are not getting resolved. Attached are the screenshots.
What do I do ?
Thanks in advance
There is a version mismatch. Your bundle depends on the packages org.jsoup and friends having version 1.7 or higher. Strictly speaking it imports from version 1.7.0 (inclusive) up to version 2.0.0 (exclusive).
However, the bundle installed in your runtime exports as version 0.0.0. Therefore the import does not match the available exports and your bundle cannot resolve.
It seems you need to find an org.jsoup bundle that exports as version 1.7, and install that into your runtime instead of the bundle you have used here.
There a couple of various ways to resolve your problem:
Force import 0.0.0 version exported by your Jsoup bundle. (easiest)
This can be performed by update of Import-Package section of your bundle which consumes Jsoup packages:
<Import-Package>
org.jsoup.*;resolution:=required;version="[0.0,0.1)"
</Import-Package>
Wrap your Jsoup dependency with Maven POM and set version which you would like to export via update of Export-Package section (optional):
<Export-Package>
org.jsoup.*;version="1.7.2"
</Export-Package>
Details: http://www.cqblueprints.com/tipsandtricks/build-and-deploy-osgi/deploy-third-party-libs.html
Install another Jsoup JAR as OSGi bundle.
Original Jsoup 1.7.2 (http://mvnrepository.com/artifact/org.jsoup/jsoup/1.7.2) was good in my project.
Update your Maven "dependency" item to import:
<dependency>
<groupId>com.adobe.cq.jsoup</groupId>
<artifactId>jsoupservice-bundle</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
I have a Maven project that's building fine, and I'm attempting to add a reference to twitter4j.
So I add this to the pom:
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[2.2,)</version>
<type>jar</type>
<optional>false</optional>
<scope>compile</scope>
</dependency>
When I build (mvn clean install) I get:
[ERROR] Error building bundle net.stevex.tweetfetcher:tweetfetcher-bundle:bundle:1.0-SNAPSHOT : Unresolved references to [twitter4j] by class(es) on the Bundle-Classpath[Jar:dot]: [net/stevex/tweetfetcher/impl/TweetFetcherImpl.class]
This makes sense .. the twitter4j package isn't embedded in the bundle. So I add an Embed-Dependency to the maven-bundle-plugin's instructions:
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
And now when I build, I get:
[ERROR] Error building bundle net.stevex.tweetfetcher:tweetfetcher-bundle:bundle:1.0-SNAPSHOT : Unresolved references to [dalvik.system, javax.crypto, javax.crypto.spec, javax.management, javax.management.openmbean, org.apache.commons.logging, org.apache.log4j, org.slf4j.impl, twitter4j.internal.http.alternative] by class(es) on the Bundle-Classpath[Jar:dot, Jar:twitter4j-core-2.2.3.jar]: [twitter4j/internal/logging/CommonsLoggingLogger.class, twitter4j/internal/logging/Log4JLoggerFactory.class, twitter4j/auth/OAuthToken.class, twitter4j/internal/http/HttpClientFactory.class, twitter4j/auth/OAuthAuthorization.class, twitter4j/internal/logging/Log4JLogger.class, twitter4j/conf/ConfigurationBase.class, twitter4j/TwitterAPIMonitor.class, twitter4j/internal/logging/CommonsLoggingLoggerFactory.class, twitter4j/management/APIStatisticsOpenMBean.class, twitter4j/internal/logging/Logger.class]
I don't understand why the twitter4j classes are missing, and I don't understand the references to dalvik.system, javax.crypto, etc. What's wrong here?
The problem is that the twitter4j project requires all of the packages listed in the error list. When you use it does not include all transitive dependencies. There is an Embed Transitive instruction for the maven-bundle-plugin that will embed all transitive dependencies
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
However, this kind of defeats the purpose of OSGi. This leaves you with two options that I know of:
Search for the packages in the second in the SpringSource EBR. You simply enter the package name in the search box, and the results will include the xml for your POM file. The SpringSource bundles will include references to other bundles in the EBR, eliminating the problem of transitive dependencies.
Use the bundle-all goal of the maven-bundle-plugin. This goal will run the maven bundle plugin for every dependency of your project, and place the resulting bundles into your target directory. You could then install these bundles into your local repository.
I would recommend using option 1 for as many of the bundles as you can find, then defaulting to option 2 when SpringSource doesn't have them.