Question about maven - java

I read some useful posts here on SO about previous maven questions, I'm currently very interested in learning maven(cause I like it and because my boss requires me to). I'm currently reading [this][1] book and I'm working my way trough examples. Its a straightforward book but its has some errors inside(trivial ones), yet for a newbie like me can be hard to spot, once spotted it can be easily fixed. Is there any other book better to understand maven from top to bottom?
Second part of the question is relating an example in this book, maybe a simple explanations would resolve my doubts.
Here is the thing, I made a simple-weather project in java which retrieves the weather conditions from yahoo weather server, given the particular zip code it returns weather information.
Then I made an 'simple-webapp'(with maven as well as the one above I forgot to mention that), which is basicaly a web project which has some default servlet already there with maven and it does nothing.
And I have some parent-project I wanna merge those two projects into one, so I made a pom.xml which has 2 modules , 1 to retrieve info(java project) and other to display it on the web (web app).
I made everything work at the end, but here is the odd thing .. if I make webapp display any string "name" lets say then build it independently, it does exactly print that string. But when I put the webapp in the "parent-project" and change this string to "name1" and build it as sa partent-project sub module.. nothing changes ..
So I go back to the point, because simple-webapp is dependent on simple-weather I can't build it anymore on its own, so now if I wanna make some changes to the webapp.. modify the webapp outside the "parent-project" build it there then paste it back to the parent-project and then the changes will apply, why is that, why can't I directly change the servlet content/or add another one in the webapp as the part of the "parent-project"?
Thank you.. I know its a long and boring question, but I'm just trying to learn things and there is no better place to ask than here :D
EDIT - HERE ARE POM FILES FOR EACH PROJECT :
1. simple-parent pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>Multi Chapter Simple Parent Project</name>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2. simple-weather pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-weather</artifactId>
<packaging>jar</packaging>
<name>Multi Chapter Simple Weather API</name>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. simple-webapp pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<name>simple-webapp Maven Webapp</name>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-weather</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

I am not sure to completely understand your question. However, let's explain some principles in Maven.
So you have such a structure:
parent
+ simple-weather
+ simple-webapp
On a Maven point of view, we have 3 projects here:
parent, which is a pom project (i.e. its packaging attribute is set to pom)
simple-weather, which is a jar project and has parent as parent.
simple-webapp, which is a war project, has parent as parent and simple-weather as dependency.
The parent projects uses two concepts in Maven:
The inheritance, which say that all of his children (simple-weather and simple-webapp) will inherit all of his properties (this concept is almost the same thing as the extends in Java).
The aggregation, which is defined by the definition of <modules>. Aggregation means that every command that will be run on the project will also be run on each module.
What happen if I build (using mvn clean install) on the parent directory?
Maven will "compile" the parent project and then install the pom.xml in the local repository.
Maven will compile the simple-weather project, but as it has a parent, Maven will look the parent pom.xml file into the local repository. Once the JAR is created, it is installed in the local repository.
Maven will finally compile the simple-webapp project. Maven will do the same thing for the parent pom.xml, but also for the simple-weather project.
The situtation explained in the 3rd point is important: If you want to build the simple-webapp project, Maven will always try to find all of his dependencies - including simple-weather - from the local (or distant) repository.
That's why if you build only the simple-webapp without building and installing simple-weather, Maven will not find the latter project, or will find an older version.
So to summarize, when you work on multi-modules project with Maven, try to always run the build and install commands from the root (or parent) directory.
I hope that this explanation is clear enough and help you to understand what happen in your case. Do not hesitate to ask more information...

Related

Maven clean install failed to execute goal on project

I am developing a spring boot project which has few modules. I have on entry point module which has main class, the other module dependencis i have added in entry point module pom.xml. When i give command mvn clean install it throws an error saying `
Failed to execute goal on project api: Could not resolve dependencies
for project com.nikesh:api:jar:1.0.0: The following artifacts could
not be resolved: com.nikesh:lib:jar:1.0.0, com.nikesh:repo:jar:1.0.0,
com.nikesh:entity:jar:1.0.0, com.nikesh:dto:jar:1.0.0,
com.nikesh:service:jar:1.0.0, com.nikesh:common:jar:1.0.0: Failure to
find com.nikesh:lib:jar:1.0.0 in https://repo.maven.apache.org/maven2
was cached in the local repository, resolution will not be reattempted
until the update interval of central has elapsed or updates are forced
-`
This is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nikesh</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
<description>api module</description>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<start-class>com.nikesh.api.MultiModuleApp</start-class>
</properties>
<dependencies>
<dependency>
<groupId>com.nikesh</groupId>
<artifactId>lib</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.nikesh</groupId>
<artifactId>repo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.nikesh</groupId>
<artifactId>entity</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.nikesh</groupId>
<artifactId>dto</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.nikesh</groupId>
<artifactId>service</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.nikesh</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Please help me out resolving this issue.
You need to make sure that all sub modules are built in proper order before the main module build.
Guide to Working with Multiple Modules
The Reactor
The mechanism in Maven that handles multi-module projects is referred to as the reactor. This part of the Maven core does the following:
Collects all the available modules to build
Sorts the projects into the correct build order
Builds the selected projects in order
Because modules within a multi-module build can depend on each other, it is important that The reactor sorts all the projects in a way that guarantees any project is built before it is required.
The following relationships are honoured when sorting projects:
a project dependency on another module in the build
a plugin declaration where the plugin is another modules in the build
a plugin dependency on another module in the build
a build extension declaration on another module in the build
the order declared in the element (if no other rule
applies)
Note that only "instantiated" references are used - dependencyManagement and pluginManagement elements will not cause a change to the reactor sort order
for more info. You can refer this link for a sample multi module maven project.

Maven dependency not found for org.bukkit:bukkit

The groudID, artifactId and version dependencies are not being found in Maven?
I followed this tutorial to set up a Minecraft plugin which uses Maven.
But I get the error
org.bukkit:bukkit:1.7.2-RO3 not found
and the text font is red where everything else in the pom file is white.
Here is the code in the pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chrismepham</groupId>
<artifactId>TestPlugin</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>bukkit-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.7.2-R0.3</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Why is the dependency not being found?
EDIT:
I changed the repository and dependency version as suggested but still have the error as shown in the picture:
The reason the text is red is because that dependency you've specified can't be found in either maven central, or the additional repository you've added.
Paste the repo link into a browser, and you'll find that the dependency is actually 1.8-R0.1-SNAPSHOT not 1.7.2-RO3 as the tutorial mentioned (maybe the tutorial is out of date, or that dependency has since been removed for some reason)
Change the dep to
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8-R0.1-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
If you haven't already set auto-import on intelliJ, you'll get a popup in the top right hand corner asking if you want to re-import. IntelliJ then reads your pom.xml file, works out what dependencies you need and then downloads them.
You should see org.bukkit.bukkit under external dependencies in the left hand "projects" window, along with some other libraries it depends on, such as guava and commons lang.
Bukkit as a Maven dependency suggests a different repository:
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public/</url>
</repository>
On inspection, it includes 1.7.2-R0.3.
(It also works over SSL, as https://repo.bukkit.org/content/groups/public/, which you should prefer.)
The dependency doesn't exist in the repo. If you follow the repository link, you see that the only version inside is 1.8-R0.1-SNAPSHOT. So you must change that version inside your pom.

Maven error when pom.xml is packaged an ejb, but goes away when packaged as jar

When I package my pom.xml as an EJB I get an error that I do not understand. This error shows up if I touch the package in any way, to include Ctrl+S and Maven update. The error goes away if I change the packaging back to jar.
Code:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.myorg.buildparent</groupId>
<artifactId>BuildParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.myorg.myEjbPackage</groupId>
<artifactId>MyEjbPackage</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ejb</packaging>
</project>
By changing
<packaging>ejb</packaging>
to
<packaging>jar</packaging>
The error will go away.
Error string for reference:
'Building workspace' has encountered a problem.
Errors occurred during the build.
Errors running builder 'Maven Project Builder' on project 'MyEjbPackage'.
org.codehaus.plexus.archiver.ArchiverException
Even though I can remove the error, I still feel like it should be packaged as an ejb. Does this make sense to anyone? Does my EJB package need to be packaged as a EJB in the pom? I should admit that my skills with Maven are around that of a novice/low intermediate.
Try adding this configuration in your POM. The EJB plugin (which is bound to the lifecycle by default when you select ejb packaging type) doesn't use the same version of the archiver that the jar/war/ear plugins do.
There is a JIRA issue for updating the dependency versions, however it has not been worked yet.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>${maven.ejb.plugin.version}</version>
<dependencies>
<!-- Use the same archiver as the other [j/w/e]-ar plugins -->
<!-- See http://jira.codehaus.org/browse/MEJB-52 -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-archiver</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
Update
MEJB-52 is fixed in maven-ejb-plugin version 2.4 and later. Consider updating the plugin version to the latest before adding this configuration.

communicate between wars and jar inside ear

I'm new to creating ear and communicaton between wars and jar...
I was having two wars having completely independent functionality.
Now I've to create a Ear, where two application have to work on same functionality,
which is enclosed in a jar.But requirement is I must not include the jar in Pom.xml of both but make use of that jar, where all 3 are under single ear.is this is possible?
I've tested Ear with 2 independent wars and it's working fine now how to achieve above I'm not getting this.
I'm using Maven with Jboss7.1.1.
I went through links like MessageHandler in JAR/WAR/EAR , https://stackoverflow.com/questions/1796255/tell-me-a-clear-differnece-between-ear-war-and-jar but got no idea about above problem.
You can put multiple wars and jars into a ear and they can share the same classloader. This means that all the classes are accessible from all the jars/wars. i.e. it is as if all the classes / resouces are in one archive without the sub-packaging.
I am assuming this is what you mean by "communication between wars and jars".
EDIT: check Making an EAR with Maven for an example of the pom.xml for building an ear. In the example, there is one jar and one war but you could have any number of wars / jars.
Hi got the solution >> here it is.. this is a pom.xml of ear project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ear</packaging>
<dependencies>
<!--Dependency for jar-->
<dependency>
<groupId>com.jar</groupId>
<artifactId>com.jar</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
<!--Dependency for war1-->
<dependency>
<groupId>com.war2</groupId>
<artifactId>com.war2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<!--Dependency for war2-->
<dependency>
<groupId>com.war1</groupId>
<artifactId>com.war1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<finalName>Project</finalName>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<finalName>MyEarFile</finalName>
<version>5</version>
<modules>
<!--Webmodule for war1-->
<webModule>
<groupId>com.war1</groupId>
<artifactId>com.war1</artifactId>
<uri>war1.war</uri>
<bundleFileName>war1.war</bundleFileName>
</webModule>
<!--Webmodule for war2-->
<webModule>
<groupId>com.war2</groupId>
<artifactId>com.war2</artifactId>
<uri>war2.war</uri>
<bundleFileName>war2.war</bundleFileName>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note:: groupId and artifactId metioned here must match with groupId and artifactId mentioned in the project's pom.xml.
Also dependency of jar must be present in this i.e. ear's pom.xml and not in both app's pom.xml.
At time of maven install it automatically refers to jar's contents..

How to define organization specific parent pom

Env: Maven 2.2.1
I have two java projects under svn (projectA, projectB). My maven structure is as follows..
For projectA
pom.xml (contains ProjectA parent pom definitions)
module moduleA
module moduleB
For projectB
pom.xml (contains ProjectB parent pom definitions)
module moduleC
module moduleD
projectA/pom.xml and projectB/pom.xml contain common definitions like junit, selenium, compiler, eclipse plug-ins which are common to both projects. (e.g. given below)
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency
How should I create / organize a organization specific project pom which includes such common definitions, so that individual projects don't have to re-create / maintain one. Can someone provide some snippets or projects which have already done this before?
EDIT1:
company/pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>company</artifactId>
<packaging>pom</packaging>
<name>parent</name>
<version>1.0.0</version>
<build>
<defaultGoal>install</defaultGoal>
</build>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
projectA/pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany</groupId>
<artifactId>company</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>projectA</artifactId>
<packaging>pom</packaging>
<name>projectA</name>
<version>1.0.0</version>
<modules>
<module>moduleA</module>
<build>
<defaultGoal>install</defaultGoal>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
projectA/moduleA/pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany</groupId>
<artifactId>projectA</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>moduleA</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>moduleA</name>
<build>
<finalName>moduleA</finalName>
<defaultGoal>install</defaultGoal>
</build>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
</dependencies>
Throws the following error:
Project ID: com.mycompany:moduleA
POM Location: c:\temp\maven\projectA\moduleA\pom.xml
Validation Messages:
[0] 'dependencies.dependency.version' is missing for commons-lang:comm
ons-lang:jar
[1] 'dependencies.dependency.version' is missing for javax.servlet:ser
vlet-api:jar
I would seriously reconsider adding dependencies into a "super" POM, this unnecessarily couples the projects (but may hint that if the projects aren't disparate then they should be merged anyway).
I think the last comment by #lexicore is poignant too, to expand on the OOP analogy it also feels like "mixing levels of abstraction".
Alex Gitelman provides the correct answer, you need to use dependencyManagement as shown here Dependency Scope
Maven3 is supposed to be supporting POM fragments see How to use Maven 3 mixins? which I've long been waiting for.
We have an organisation Über POM but this just contains:
<organization>
<name>...</name>
<url>...</url>
</organization>
<developers>
<developer>
<id>...<id>
<name>...</name>
<email>...</email>
<roles>
<role>...</role>
</roles>
</developer>
<distributionManagement>
...
</distributionManagement>
<repositories>
<!-- your proxy repo here -->
</repositories>
These are things that change very rarely (if we change our repository/distribution-management then all projects must change, if a developer leaves or joins we can update the project POMs at any time convenient).
Dependencies belong specifically to the module under consideration, just because two independent project happen to share dependencies now doesn't mean they always will. I completely understand the annoyance of having to copy 'n' paste reams of XML for each project (compiler plugin, reporting plugins, junit etc), but differing levels of activity in each project will surely mean they diverge at some point.
WRT cascade builds in Continuous Integration, if project A demands a change in the super dependencies POM, then all you other projects will be forced to rebuild - maybe fine if you've only 2 projects but even then did you checkout and build both before committing the change?
If it's only dependencies that you need to reuse, create another project with packaging pom and specify dependencies there. Let's call it OrgDependencies Then include it as dependency in your projectA and projectB. It will transitively pull all dependencies from OrgDependencies project.
In your example, in projectA, instead of
<parent>
<groupId>com.mycompany</groupId>
<artifactId>company</artifactId>
<version>1.0.0</version>
</parent>
Try putting
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>company</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
And remove dependencies to commons-lang etc from modules.
Update.
While my previous solution with transitive dependencies should work, actually what you need
is <dependencyManagement> section in your company wide pom.xml
That's where you define versions.
Note: Anything in dependencyManagement section is not really a dependency but just a descriptor that allows to specify version and exclude transitive dependencies (if necessary) in case normal dependencies section specifies that dependency. So you can put as many items in dependencyManagement as you want, it will not make all descendants dependent on them.
I tested it and it works:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>company</artifactId>
<packaging>pom</packaging>
<name>parent</name>
<version>1.0.0</version>
<build>
<defaultGoal>install</defaultGoal>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
In OS projects I am normally using 3+ levels of POMs:
"Company-wide POM" contains dev-wide definitions like distribution management, individual plugin versions etc. Very stable, normally has one-number version. Example: Sonatype OSS Parent.
"Project POM" contains project-wide definitions: Java compiler version, dependency management etc. Parent is company-wide POM. Example :JAXB2 Basics Project. Version is updated with each release.
"Module POMs" on different levels. List individual dependencies (versions of dependencies are inherited from the project POM), add "special" build steps. Example: JAXB Basics.
I saw a similar pattern an other OS projects (like Apache's) as well.
A few more comments:
You may also have the "department POM" or "product POM" depending on the company size and product organization.
Think of POM inheritance pretty much as of OOP inheritance. What would you put into which abstract class so that class hierarchy is stable but dynamic? For instance, it would not make sense to define versions of dependencies in the company-wide POM since versions change too often. On the contrary, defining distribution management in earch of the projects would hurt the DRY principle.

Categories