Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using provided dependency with inheritance

Tags:

maven

I have a JAR project called "A" that have some libs that will be provided by wildfly 10 :

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.core.version}</version>
            <scope>provided</scope>
        </dependency>

I have another project called "B" that is a WAR project. This second project should not need insert hibernate dependencies again, but if i don't do it i can't use @Entity and others classes. I should "re-import" the hibernate-core in project "B" as provided again. There is any way to inherite it from project "A" ?

like image 374
Ronaldo Lanhellas Avatar asked Oct 18 '22 15:10

Ronaldo Lanhellas


2 Answers

A golden rule that I learned with the Maven community is to explicitly declare the dependencies your project needs, it's just good practice.

Don't bother about declaring a dependency multiple times, Maven does an excellent job on managing it, but one thing you are doing, and it's good, is to keep the version management in a single place.

Maven documentation - says that dependencies with <scope>provided</scope> are non-transitive, it means their availability scope is limited and are available only to the project declaring the dependency itself and it's direct childs(modules), but it won't be available to downstream projects that import the lib as a dependency.

Yet, the documentation says in the Importing Dependencies section that it's possible to inherit managed dependencies from another project using the scope import.

It requires some extra work, but this allows inheritance of provided dependencies; declare them without version, making it easy to keep its version in synch across multiple projects.

The key to make this work is to use both these tags when importing the other project:

  • <type>pom</type>
  • <scope>import</scope>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>maven</groupId>
            <artifactId>A</artifactId>
            <version>1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>managed-dependecy-group-id</groupId>
        <artifactId>managed-dependecy-itself</artifactId>
    </dependency>
</dependencies>
like image 89
cbaldan Avatar answered Oct 21 '22 05:10

cbaldan


but if i don't do it i can't use @Entity and others classes

I would encourage to read the documentation around the Dependency Scope in Maven where you can find that -

provided : This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

further to answer your questions

I should "re-import" the hibernate-core in project "B" as provided again. There is any way to inherite it from project "A" ?

If you are using the same dependency in project B, I would suggest including it in the list of dependencies in project B.

Also, if the projects are tightly coupled(in the sense that they would always be using the same version of hibernate-core) you can use the existing dependency by modifying the scope to compile as also used by default if you won't mention.

The choice remains over the design you want to implement, do read this -

Maven : Should I keep or remove declared dependencies that are also transitives dependencies?

Though IMHO, I prefer the answer made by @i000174 there specifically to be on the safer side and better control.

like image 24
Naman Avatar answered Oct 21 '22 03:10

Naman