Maven Multi Module Project

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.

在这里输入图像描述

Presently the thing we dont like are

  • 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.
  • As a solution I want to have a cleaner organization between poms , and possible avoid above issue.
  • What i thought is ( may be incorrect )

    在这里输入图像描述

    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


    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.

  • 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.

  • 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.


    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.

    链接地址: http://www.djcxy.com/p/37580.html

    上一篇: 如何用maven打包switchyard应用程序

    下一篇: Maven多模块项目