Maven 2.xx dependency override in child - maven

Are there any way to override a dependecy version in child project over parent's version?
Parent pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
<dependency>
Child pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.1.RELEASE</version>
<dependency>
I did not find any working articles on this subject.

Related

How can I download the third-party dependencies of spring-web.jar by maven?

The dependencies of Spring-web.jar do not contain third-party dependencies now. It only has other jar of spring framework. For example it depends on spring-core.jar. But the spring-web depends on some third-party jars. httpclient.jar is one of them. When I use maven to package my project that depends on spring-web, the httpclient.jar is not download automatically.
this is a segement of spring-web-5.2.15.RELEASE.pom.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.15.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.15.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
I read the pom of spring-web in early version. I find that the denpendencies include httpclient.jar. But it is disappear in new version now. this is a segment of spring-web-5.1.8.RELEASE.pom.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
<optional>true</optional>
</dependency>
How can I download the dependencies?

How to deal with "omitted for conflict with.." message in pom.xml?

I have this situation:
I know that "Maven resolves version conflicts with a nearest-wins strategy". So here wins the aop 3.0.7 based on this rule. But i also define a dependencyManagement section in my pom and it seems like this:
<properties>
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
<org.springframework.security.version>3.1.4.RELEASE</org.springframework.security.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${org.springframework.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${org.springframework.security.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
</project>
And this is what it all looks like on the dependencies tab:
So i expect the spring-aop to use the 3.2.4.RELEASE version instead of 3.0.7 like the webmvc, as i define this in the dependeny management.. Why is there still being used the older version 3.0.7?
Your dependency management declaration has a typo (com.springframework instead of org.springframework).
This is the correct pom entry:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
Unfortunately, since the dependency is not being used, Maven (or Eclipse) will not flag as a missing artifact.

Defining named set of dependencies in parent pom

I would like to be able to define set of dependencies in parent pom and to be able to include these sets in children poms.
For instance:
parent pom {
set1 {artifact1, artifact2, artifact3}
set2 {artifact4, artifact5}
set3 {artifact6, artifact7}
}
child1 pom {
dependencies {set1, set2}
}
child2 pom {
dependencies {set2, set3}
}
This behaviour is also described here (not implemented): http://docs.codehaus.org/display/MAVEN/Profiles+for+optional+dependencies
Is there any way to do this? Thanks!
I don't think there is a way to achieve what you want via parent/child relationship of POMs (although would be glad to be corrected on this), but one solution that might work for you is to define groups of dependencies in their own POM file and then add a dependency to this POM in your individual modules.
So for example, here is a POM definition for some Spring dependencies:
<groupId>com.xyz</groupId>
<artifactId>spring-deps</artifactId>
<version>SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
</dependencies>
Then in your module's pom you define a dependency to this pom:
<groupId>com.xyz</groupId>
<artifactId>module1</artifactId>
<version>SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.xyz</groupId>
<artifactId>spring-deps</artifactId>
<version>SNAPSHOT</version>
<type>pom</type>
</dependency>
</dependencies>
and all the Spring dependencies defined in the spring-deps pom are automatically included for you.

Maven dependency exclusion doesn't seem to work

I have a Maven project depending on couple other Maven projects. I am using Spring 3.1.1 in my project and dependent projects have 3.0.6. I am trying to exclude Spring 3.0.6 when deploying since having both isn't possible. I have added an explicit exclusion in my POM for that but for some reason I still see old version of spring core jars in the WEB-INF/lib folder when I start the Tomcat server. Can someone point me out where I am going wrong. Here is my pom.xml:
<project>
....
<properties>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
</properties>
<dependency>
<groupId>com.test.abc</groupId>
<artifactId>abc</artifactId>
<version>1.0</version>
<type>war</type>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-xml</artifactId>
<version>1.0-m2</version>
</dependency>
....
</project>
Your dependency type is war so there is no resolution happening here. Maven overlays the war contents over your project.
When the war is published to repository, the artifact will contain dependent libraries in WEB-INF lib folder. During overlay it does not treat lib folder any different from any static resource unless you tell it to exclude in different way.Check 'overlay' property here
In my case I thought I excluded the right dependency. With eclipse, on dependency hierarchy tab you can right click on it and click exclude artifact and it excluded the right dependency (they both had the same artifactId but different groupId)

Spring JUnit Test Error

I am receiving the following error when I attempt to run my Spring JUnit test. I am just trying to get familiar with creating JUnits using the Spring Framework.
JUnit Class:
package org.xxx.springdao.mongo_datadictionary;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.xxx.springdao.mongo_datadictionary.SimpleSpringApp;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public class SpringJunitTest {
#Test
public void testSpringTest() {
SimpleSpringApp test = new SimpleSpringApp();
assertNotNull("Success", test);
}
}
SpringJunitTest-context.xml:
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
</block>
Pom.xml:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.0.M5</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-cross-store</artifactId>
<version>1.0.0.M5</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-log4j</artifactId>
<version>1.0.0.M5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.beans</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.aspects</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-java-driver</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>3.1.0.RC1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.asm</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.aop</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.expression</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.orm</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.transaction</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.test</artifactId>
<version>3.1.0.RC1</version>
</dependency>
Error:
Tests in error:
initializationError(org.saic.springdao.mongo_datadictionary.SpringJunitTest):
org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
Stack Trace:
java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
at org.springframework.test.context.ContextLoaderUtils.resolveContextLoader(ContextLoaderUtils.java:87)
at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:298)
at org.springframework.test.context.TestContext.<init>(TestContext.java:100)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:117)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:119)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:108)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I had this exact same issue when upgrading from Spring 3.0.6 and to 3.1.0 release. Here is the solution:
Some of your dependencies are listed as being the 3.1.0 version, but they resolve their transitive dependencies to the older versions which don't have the method listed in the error message.
If you use Eclipse, open the pom.xml and look at the dependency hierarchy tab. Some libraries you declared will be listed as version 3.1.0 but have a resolved dependency of 3.0.6. For me, I had spring-security-config and spring-security-taglibs listed as version 3.1 in my pom, but they resolved dependencies such as spring-core and spring-expression as 3.0.6.
To resolve this, explicitly list each of the highest transitive dependencies explicitly in your pom. For example, I was originally counting on spring-security-config to bring in spring-core as a transitive dependency, so I did not list the spring-core artifact in my pom. But to fix this issue I changed my pom to explicitly include spring-core (version 3.1.0). I added direct dependencies for every resolved dependency in my pom that was being resolved to an older version, and then it worked!
Hope this helps...
If solutions above are not working, it might be eclipse's fault:
in eclipse, on the project -> properties -> java build path -> Librairies -> if there is "Junit 4", remove it.
I had this stacktrace:
java.lang.NoSuchMethodError: org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runLeaf(Lorg/junit/runners/model/Statement;Lorg/junit/runner/Description;Lorg/junit/runner/notification/RunNotifier;)V
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
And of course updating the pom wasn't the solution...
In my case,I had dependency one junit-dep and one junit, when I remove the junit-dep, everything is ok.
It complains about java.lang.NoSuchMethodError and I think this has to do with your spring dependencies. Try changing your spring dependencies like this:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.RC1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.RC1</version>
</dependency>
And I don't know where following dependencies come from, but I think they might need correction with their artifactId as well:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.transaction</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.0.M5</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-cross-store</artifactId>
<version>1.0.0.M5</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-log4j</artifactId>
<version>1.0.0.M5</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-java-driver</artifactId>
<version>2.7.2</version>
</dependency>
And one final thing that I want you to consider, do you really, really need ALL of those dependencies?
Jay is correct. Based on what he said and the error originating from org.springframework.beans, review your dependency graph for said project and resolve any version conflicts by excluding transitive dependencies on org.springframework.beans and explicitly stating a dependency on the latest or the correct version of org.springframework.beans.
The same should probably be done to other dependencies, but if it ain't broke don't fix it ;) unless you can't get passed your OCD, like myself.
I leveraged Netbeans by selecting the project, right click-> show dependency. Once in the dependency graph view, search for Beans. Double click on the dependency and right click to resolve version conflict where a wizard like window will provide additional configurations to resolve the conflict. Piece of cake!
Thanks Jay!
add this one to your pom File, I had the same ERROR, but I have done this, now it works for me,
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
Note: I have POM parent, and POM child,
and I wanted to test my child project, but I got error. mvn test....
so I have done this thing. in Parent.
with version and in child without. like this one
org.springframework
spring-beans
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
try it.

Resources