Asanke
Asanke

Reputation: 601

How to determine the <parent> dependency for a set of springframework dependencies

I like to know if below is possible and how.

I was following a tutorial for spring boot and it was mentioned there we can have a parent dependency.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

And then define the dependencies without the version number.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

This will add the dependencies version 1.5.6.RELEASE of spring-boot-starter and spring-boot-starter-web in to the projects dependencies.

Just like that I want to find what is the <parent> code snippet for the following dependencies I need to add in to a new project. Dependencies in <groupId>org.springframework</groupId>. I need to use the version 4.3.9.RELEASE.

Thanks!

Upvotes: 2

Views: 2183

Answers (3)

Gregory.K
Gregory.K

Reputation: 1337

Use spring-framework-bom if you don't use Spring Boot and need Spring Framework dependencies only:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

In such case dependency would be without version was specified:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
</dependency>

Also, yet another option exists if you use Spring Boot but you don't want to use spring-boot-starter-parent as parent artifact:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

See Spring Boot docs for more details. An important note from the docs:

Each release of Spring Boot is associated with a base version of the Spring Framework so we highly recommend you to not specify its version on your own.

It means that you should use Spring Framework version is defined for Spring Boot.

Upvotes: 1

Norbert Bicsi
Norbert Bicsi

Reputation: 1568

Since you are going though the tutorials I'm assuming you are new to spring.

The folks at spring were nice enough to setup a site that generates projects. It is very easy to use. I recommend trying that while learning. Download a few apps with the dependencies you want and look at how they are set up.

Once you are comfortable and want to dive deeper, read @glytching's answer again, it is very good.

Upvotes: 1

glytching
glytching

Reputation: 47905

If you are using Spring Boot then these three dependencies will be provided for you by the following starters:

  • spring-test will be provided by spring-boot-starter-test
  • spring-context will be provided by spring-boot-starter-data-jpa
  • spring-jdbc will be provided by spring-boot-starter-jdbc

So, with the following parent:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

... if you add these dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

... then you will get

  • spring-context
  • spring-jdbc
  • spring-test

However, Spring Boot 1.5.6.RELEASE depends on v4.3.10.RELEASE of those core Spring libraries not 4.3.9.RELEASE as suggested in your question. Typically, you would accept Spring's curation of dependencies so if Sping provides 4.3.10.RELEASE then either (a) you should use that version or (b) downgrade Spring Boot toa version which provides 4.3.9.RELEASE.

Read on for details on how to identify the correct starter for a given curated library ...

The spring-boot-starter-parent is a special starter that provides useful Maven defaults and a dependency-management section which defines numerous dependencies which you might want to use in your POM. These dependencies are often referred to as "curated" or "blessed" and since they are defined in a dependency-management section somewhere in the maven hierarchy you can refer to them in your POM without a version tag (i.e. they inherit the version from the dependency-management section entry.)

You can see the spring-boot-starter-parent POM here and peeking inside you can see that it references the spring-boot-dependencies POM here.

Looking at your question you mentioned that you can declare a dependency like so ...

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

... this is because the spring-boot-dependencies POM declares the following:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${revision}</version>
</dependency>

So, the parent and the starters are just a means of wrapping up dependency declarations and making them easier for application developers to use. The Spring docs summarise this as:

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

However, this does not mean that all dependencies must be declared via parents or starters so, if you are not using Spring Boot then you can declare a dependency without using a parent or a starter and what you have described in your question (declaring dependencies on 3 core Spring libraries) can be safely covered by simply depending on those 3 libraries explicitly. For example, just add the following to your your pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.9.RELEASE</version>
    <scope>test</scope>
</dependency>

Upvotes: 2

Related Questions