On Fri, Nov 11, 2011 at 3:54 PM, xuhb <[email protected]> wrote:
> when we define dependency in parent POM, the defined dependency sometimes can 
> be inherited automatically, sometimes it cannot;
> for example:
> if I define a module A in parent POM
>    <dependency>
>        <groupId>groupA</groupId>
>        <artifactId>A</artifactId>
>        <version>1.1</version>
>  </dependency>
>
> Sometimes: if I doesn't declare the anything in child module's pom, it will 
> automatically  all dependency in parent POM;
> But sometimes, I find I must declare the dependency in child module, it 
> cannot be automatically inherited by child module;
> Why?
>
> For example , I must declare the module A in child pom as following, 
> otherwise, the child module cannot inherit dependency of parent pom; But the 
> child module does inherit the version
>    <dependency>
>        <groupId>groupA</groupId>
>        <artifactId>A<artifactId>
>        <!--here we need not declare the version, the child pom will inherit 
> version declare in parent POM -->
>    </depdency>
>
> So why? How could I control the way for child pom to inherit parent POM ?

Your sentences are difficult to understand (and that's fine, it looks
like english isn't your first language ).
So I will try to answer what I think you are saying.

There are two sections in the Maven pom.xml.
pom.xml/project/dependencyManagement  (see
http://maven.apache.org/ref/3.0.3/maven-model/maven.html#class_dependencyManagement)
and
pom.xml/project/build/dependencies/dependency (see
http://maven.apache.org/ref/3.0.3/maven-model/maven.html#class_dependency

Also check out 
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
and the free books http://maven.apache.org/articles.html

In general, you want your parent pom's to be free of
pom.xml/project/build/dependencies/dependency.
This is because a <packaging>pom</packaging> does not build anything
itself and so what would be the point in declaring a dependency in
this pom?
If you accidentally do declare a dependency, then the disadvantage is
that every pom that uses this parent pom will now include the
dependency, and there is no way to disable this behaviour.

What you want to do instead is to use dependencyManagement.
Your root pom defines all dependencies used and also specifies all the
versions to be used.
Then in your child pom's you omit the version in the
pom.xml/project/build/dependencies/dependency section.
This ensures that your project will not use different versions of the
same artifact.
And it ensures that whenever you add a new dependency your build will
fail because you must first add the dependency to the
dependencyManagement section of your parent pom.

Again, I highly recommend you read the free books available, they will
be able to explain this in much greater detail.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to