In Maven, including dependency via OR importing the package inside . Which one of the two should be used?

karanmahi 08-04-2019

In Maven, including dependency via <dependency> OR importing the package inside <Import-Package>. Which one of the two should be used?

  1. 1. In my project core pom.xml, there is <Import-Package>javax.inject;version=0.0.0<Import-Package>  and also following dependency. I am not clear why both are being used? I mean, if we are already including the dependency via below-mentioned method, then why do we need to use the same inside <Import-Package>?

   <configuration>
  
<instructions>  
  
<Import-Package>
  javax.inject;version=0.0.0, 
  org.xbc.web.core.service;version=0.0.0,
  *
  
</Import-Package>
  
<Sling-Model-Packages>
  org.xbc.core
  
</Sling-Model-Packages>
  
</instructions>
  
</configuration>

Above mentioned packages are duplicated in below dependencies

   <dependency>
  
<groupId>javax.inject</groupId>
  
<artifactId>javax.inject</artifactId>
  
<version>1</version>
  
</dependency>

  
<dependency>
  
<groupId>org.xbc.web</groupId>
  
<artifactId>fullton-project.core</artifactId>
  
<scope>provided</scope>
  
</dependency>

  1. 2. Same scenario is happening in my project. My project A is dependent on project B, there also I have noticed that package path has been added in <Import-Package> and dependency has been added for project B in core/pom.xml of Project A. why both? Somebody please clarify this.

Accepted Solutions (1)

Accepted Solutions (1)

gauravb41175071
MVP
09-04-2019

At a high level-

Import-Package is for importing available dependencies when the state of osgi bundle is changed during its lifecycle in the container (Felix), Version 0.0.0 indicates that it should start from the lowest version, pick whatever version is available as mentioned in manifest and satisfy the dependency at runtime in Felix.

The packages mentioned here may/may not be available/packaged within the bundle but may be available/registered in Felix

You may choose to control it at a granular level that import * but not one package or something similar to that.  import-package is required to create the manifest file properties

Dependency on other hand works with dependency management to download the packages in local repo/nexus etc. and to mark that a particular bundle should be applied at specific scope mentioned inside <scope>. It defines the scopes of dependent bundles and which can again be superseded with import-package configuration to allow/deny the packaging of that bundle

Answers (5)

Answers (5)

karanmahi 09-04-2019

my only point is whatever we include in <import-pakage> tag, Can't we just include a dependency for that tag like below. What is the harm? what is the benefit of <import-pakage> tag providing over a dependency tag?

   <dependency>
  
<groupId>javax.inject</groupId>
  
<artifactId>javax.inject</artifactId>
  
<version>1</version>
  
</dependency>

  
<dependency>
  
<groupId>org.xbc.web</groupId>
  
<artifactId>fullton-project.core</artifactId>
  
<scope>provided</scope>
  
</dependency>

Hemant_arora 08-04-2019

javax.inject is for dependency injection. whatever dependencies that you download, you can inject and use via import-package.

consider the example of sling models , check a sample sling model and see the use of javax.inject

Prince_Shivhare 08-04-2019

<import-package>:import packages means including a existing jar. you can include the third party jar using it. suppose you have a another different project (like web service calls) in your complete project. then you can include it using import packages. plugins generates the Import-Package bundle manifest header based on the contents of the bundle.

<dependency> : It downloads the jar at run time.

karanmahi 08-04-2019

Hi Hemant - I have gone through the link. But don't understand why javax.inject, is defined under the import-package tag and in dependency-tag both.