Do I need to download the dependencies that Spring need in Maven? - java

I am starting out with Spring and I am reading Pro Spring 2.5. On page 17 they talk about Spring dependencies and I wonder if I need to add this myself in the POM, or does the dependency I have added below do this? Such as CGLib, dom4j etc?
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>

You shouldn't need to. Maven will read the pom for spring-context and get any necessary dependencies that it has too, so you won't need to specifically put them in your own pom.
Check out this link it's really handy.

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
This will just instruct maven that the current POM in which this is declared is dependent on this artfact, so when you compile the app it would make it available for you in classpath

Related

spring-context and spring-web 4.2.4.RELEASE

Why I need to include spring-context in my new spring boot project to work ?
I have this in my pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
I can see inside spring-web dependency, the dependency spring-context
if I remove spring-context from my pom.xml, the application does not run.
Below is the error I get :
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nameOfController'
Do you see the spring-context to be provided with spring-web or really that it just depends on it? You might want to do yourself a favor and use the spring boot starter artefacts that actually come with all required dependencies and don't mark any of them as "provided".
In your case, it would be spring-boot-starter-web.
When we go through the compile dependencies of spring-web , then spring-context is not included in the latest-version.
https://frontbackend.com/maven/artifact/org.springframework/spring-web/5.2.6.RELEASE
Though it was present in the previous versions.
https://frontbackend.com/maven/artifact/org.springframework/spring-web/4.3.20.RELEASE
Since, I cannot see the specific version that you'r adding to your pom.xml file, I am assuming it is getting the latest version, where the dependency of spring-context is not included in spring-web.
Also, the error stack trace that you have provided is very less and generic in nature, so it becomes difficult to debug your problem further, though I think it is a problem with version.
You can give the specific version in this way :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>

What is the difference between ${spring.version} and literal version number

I saw many tutorial some people use maven dependency such way
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
and some people use maven dependency such way
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
What is the main difference between <version>${spring.version}</version> and <version>4.3.3.RELEASE</version> in maven?
The notation of ${spring.version} is a POM variable. This means that somewhere in POM (or in one of parent POMs), you'll see a definition similar to this:
<spring.version>4.3.3.RELEASE</spring.version>
They both achieve the same end result, but using variables is a bit easier to maintain, especially if you use several artifacts sharing the same version. This way, when you choose to upgrade them, you only need to change the version in one place instead of once per artifact.

How can I update the default version of Spring that Intellij provides?

When I create a Spring project in IntelliJ IDEA, it suggests I use Spring 4.1.6. I want to use 4.2.1 (which I can do after creating the project).
Where can I make the change?
Thanks,
Andy
I don't know if you can change that yourself without an upgrade but you can allow it to create it with that version then manually change it your pom.xml file.
For example,
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
You can also provide a property that an be used on all your Spring dependencies and then you only have to change it one place.
For example,
<properties>
<org.springframework.version>4.2.0.RELEASE</org.springframework.version>
...
</properties>
Then use it like...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>

What is the right Maven dependency for javax.jms.* classes?

I need to import javax.jms.* classes. What is the right dependency to include into a Maven project? I'm trying javax.jms:jms:1.1, but no luck (it's pom, not jar).
ps. The only workaround I've found so far is: javax:javaee-api:6.0 (from Maven Central).
In ActiveMQ as well as some other projects like Qpid JMS we pull in the JMS spec classes from Apache Geronimo JARs, the 1.1 APIs are available in this dependency:
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1.1</version>
</dependency>
For JMS 2 APIs you'd need to use a different dependency, for instance
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<version>1.0-alpha-2</version>
</dependency>
These are both Apache 2.0 licensed dependencies.
Another option which is not Apache licensed is here as others have pointed out.
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
The Sun license doesn't allow maven repositories to host this (and other) artifacts.
Here is the documentation explaining this and what you should do instead...
Maven - Guide to coping with Sun JARs
What it says is you need to download the JAR manually and then install it into your own local repository or nexus server.
The pom.xml files hosted at maven central for these artifacts contain information on where you can download the JARs from.
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
I have successfully used this one:
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
Go to Maven Search site and search for javax. Open the latest version for groupId javax and artifactId javaee-api
The current version is 7.0 [Maven dependency information]
If you just want the JMS libs, without the rest of javaee, use the following:
https://mvnrepository.com/artifact/javax.jms/javax.jms-api/2.0.1
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
According to mvnrepository, the dependency to add in the pom of your project is the following:
<dependency>
<groupId>jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
Check out the dependencies listed on grepcode.com.
I only discovered this site recently, and it rocks!
http://grepcode.com/search/?query=javax.jms.*
It looks like the Geronimo jars on maven central should sort your issues out.
This worked for myself
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>

How do I override the spring framework version in Spring Batch?

I'm starting a new project with Spring Batch 2.1.6.RELEASE and I use Maven for depemdency management.
By default, it imports spring framework 2.5.6, but I'd like to use 3.0.5.RELEASE instead.
This post says it's compatible, but I don't know how to declare that in my maven pom file.
I tried just putting the dependencies for spring-core, spring-beans and spring-context versions 3.0.5.RELEASE, but that adds the libraries to the project without removing the 2.5.6 version.
I had a look at spring-batch-parent pom file, and there is a profile called "spring3" that uses the spring version I want. How do I activate a profile in my project's pom file ?
Thanks in advance,
Philippe
You can exclude the transient dependency on Spring Framework v2.5.6 of Spring Batch by using the dependency exclusions element of the spring-batch dependency in maven. Something like...
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.1.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<!-- Other exclusions here -->
</exclusions>
</dependency>

Categories