Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Spring libs using Maven

Tags:

java

spring

maven

I learned Spring via Spring In Action 3 few month ago. I downloaded Spring libraries from official site (list was like in SIA3(aop, asm, aspects, beans ...)), added them to my project and everything worked fine. Now I want to use Maven, but I am getting a lot of errors and sinking in searching what library to add.

I am newby, dont know all Spring dependencies(within it libs) and the question is not about my errors, but about the way to add all Spring libraries to my project via Maven. How do you usually add Spring libs using Maven?

like image 351
user2171669 Avatar asked Jan 12 '23 09:01

user2171669


1 Answers

You don't have to download the libraries themselves anymore. That is what Maven is for. (and quite some more, of course)

  • set up Maven properly
  • set up Maven in the IDE tool you have (like this)
  • edit the pom.xml to include what you need, adding the dependencies in the in the dependencies tag.
  • Maven takes care of resolving the dependencies of the specified packages. If a package depends on other packages, it will do it for you. You only have to specify the packages you directly need

For example

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>

You can easily find the packages using Google, and searching for "maven repository "

Avoiding version clashes

Also, as Bart mentioned, the common way of having Spring in the pom.xml - as it has way too many versions, and clashes can occur - is through a common property specifying the version for all Spring components. (Based on this answer)

Specify the property in the properties tag:

<properties>
    <spring.version>3.0.5.RELEASE</spring.version>
</properties>

Then use it in the dependencies like this:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>

Be careful to use it for ALL components to avoid version clashes. (of course, issues mught still occur, bz having different libraries reference spring too, but that is another story in its own.)

Side note

Keep in mind note that Maven projects use specific directory layout. When I first started using maven for my own projects, first I created a new blank one, and played around with it, before I began migrating my older projects to use maven. Believe me, it pays off.

like image 90
ppeterka Avatar answered Jan 20 '23 12:01

ppeterka