2

I havent used Maven extensively

Presently have 5 different maven projects , Each has a different pom.xml. As of now there is dependency relationship between them , each one points to other in < dependency > if needed.

enter image description here

Presently the thing we dont like are

  1. When we release a child projectA , then we need to manually modify all projects having projectA as a dependency to use new version. Saw Maven has a version plugin , not sure how that will help.
  2. As a solution I want to have a cleaner organization between poms , and possible avoid above issue.

What i thought is ( may be incorrect )

enter image description here

Where fat arrow represents Parent Child relationship and a thin arrow represents sub-module. But this does not seem to be working, see code and errors below

Child Project 2 pom

  <groupId>ChildProject2</groupId>
  <artifactId>ChildProject2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
     <groupId>Parent</groupId>
     <artifactId>Parent</artifactId>
     <version>${parent.version}</version>
     <relativePath>../Parent/pom.xml</relativePath>
  </parent>
  <dependencies> ...   </dependencies>

ChildProject2 - Error Project build error: Non-resolvable parent POM: Failure to transfer Parent:Parent:pom:${parent.version} from http://repo1.maven.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. Original error: Could not transfer artifact Parent:Parent:pom:${parent.version} from/to central (http://repo1.maven.org/maven2): Illegal character in path at index 45: http://repo1.maven.org/maven2/Parent/Parent/${parent.version}/Parent-${parent.version}.pom and 'parent.relativePath' points at wrong local POM

Parent pom

  <groupId>Parent</groupId>
  <artifactId>Parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
   <parent.version>0.0.1-SNAPSHOT</parent.version>
  </properties>

  <modules>
    <module>../ChildProject2</module>
    <module>../ChildProject1</module>
  </modules>

  <dependencies> ...  </dependencies>

GrandParent2 pom

  <groupId>GrandParent2</groupId>
  <artifactId>GrandParent2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
   <grandparent2.version>0.0.1-SNAPSHOT</grandparent2.version>
  </properties>

  <modules>
    <module>../Parent</module>
  </modules>

  <dependencies>...  </dependencies>

ParentMain.java

public class ParentMain {

    public static void main(String[] args) {
        DocumentFactory df = new DocumentFactory();
        ChildProject1Main cp1 = new ChildProject1Main();
        ChildProject2Main cp2 = new ChildProject2Main();
    }

}

ParentMain - Errors

    • ChildProject1Main cannot be resolved to a type
    • ChildProject1Main cannot be resolved to a type

I am currently using Maven Version 2.2.1 (open to upgrading if this can be solved using upgraded maven version) One of the comments below says can solve this using " CI tools out there such as Jenkins and TeamCity" .. any pointer (examples) how to solve this using Maven ( and or Hudson )??

What am i doing wrong , how to get best design for such project dependencies

Lav
  • 1,283
  • 5
  • 21
  • 48
  • 1
    why are you using an obsolete version of maven? Version 3.x fixes several nasty issues with dependency management. Anyway your parent pom doesn't refer your grandparent as a parent and in any case you can't have more than one parent. I'm not sure what you are trying to achieve but probably you are better off just using dependencies and removing the cycles. – Jilles van Gurp Jun 30 '13 at 09:35
  • Yes, as we cant have two parents hence was unable to add , was stuck at this point hence stopped and looking for guidance ... added more description to my question , just to understand if i am thinking in right direction or not – Lav Jun 30 '13 at 10:36
  • If i can solve this using 3.x sure i can try solution on my local machine ... showing that can ask team to upgrade to 3.x ... any pointers ... saw this http://stackoverflow.com/questions/10582054/maven-project-version-inheritance-do-i-have-to-specify-the-parent-version – Lav Jun 30 '13 at 10:39

2 Answers2

1

The first question I thought of when I saw this diagram was, "Why would the business logic have any kind of dependencies on submodules?" Two thoughts sprung to mind, and I'll go through each of them and what you shouldn't be doing to repeat these.

  1. Tightly coupled code. This manifests itself in code duplication or the large class/method smell.

    You want your code to be modular, such that a project only depends on what it needs to in order to compile and run without error.

  2. Illogical code hierarchy. This (eventually) manifests itself as circular dependencies, or dependencies that suddenly go missing when <exclude> blocks show up.

    You want the lines of what depends on what to be explicit, such that your code hierarchy is well laid out.

I'm going to take your arrows in one cardinality to mean that a project depends on another, so it makes sense that the UI and CLI depend on the Business Logic to function. It also makes sense that Business Logic can depend on the children modules to do some other function not quite related to the core.

What doesn't make sense is that these children models also depend on Business Logic. The children modules should be unique enough that they do not need to depend on anything from Business Logic; if they do, then perhaps they should live there instead.

As for the versioning - there are CI tools out there such as Jenkins and TeamCity that can aid you with that problem. The thrust would be to have that set up in such a way that it occurs independent of human intervention/error.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • the figure suggests what i am thinking as a solution ... i may be incorrect. Actual working code is simply having < dependency > tag where needed. SO ARE You suggesting that parent module and child should just have parent child relation. and not sub module relation ?? ** also any inputs on why i am getting error in childproject2 pom and parent java will help** – Lav Jun 30 '13 at 04:19
  • added more description to question , let me know if i am thinking in right direction – Lav Jun 30 '13 at 10:37
0

How do I tell Maven to use the latest version of a dependency?

Take a look at the above thread. I like the answer by Adam Gent to use the versions plugin to update versions in your poms in jenkins. I agree with him that maven and continuous deployment are a particularly poor match.

So use that and a simple parent pom for any shared configuration (plugins, dependencies, etc) but don't make them multi module projects and version and release the parent pom as an independent thing.

Community
  • 1
  • 1
Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46