多模块项目的结构化。如何包装模块? [英] Estructuration of multi module project. how to packaging modules?

查看:187
本文介绍了多模块项目的结构化。如何包装模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用maven创建多模块项目的结构。
父母的pom:

i'm creating the estructure of a multi modul project with maven. The parent's pom:

<?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>cat.base.gpt</groupId>
  <artifactId>gpt</artifactId>
  <version>0.0.1</version> <!-- application version -->
  <packaging>pom</packaging>
  <name>gpt</name>

  <parent>
        <groupId>cat.base.baseframe</groupId>
        <artifactId>projecte-pare-baseframe</artifactId>
        <version>0.0.11.a</version>
  </parent>

  <modules>
    <module>gpt.domini</module>
    <module>gpt.ui</module>
    <module>gpt.logica</module>
    <module>gpt.ejb</module>
    <module>gpt.ear</module>
  </modules>
  <dependencies>
  <!-- dependencies pel testeig TDD -->
          <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.5-rc1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.kubek2k</groupId>
            <artifactId>springockito</artifactId>
            <version>1.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
    <!--  A més, en el cas de provatures UI, s'ha d'afegir la següent dependència:-->
        <dependency>
            <groupId>cat.base.baseframe</groupId>
            <artifactId>baseframe-test-swf</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
       </dependencies>
  </project>

好的,第一个问题,我把所有依赖项放在paren的pom上这是真的正确吗?

Ok, first question, i put all the dependencies at paren's pom is this really correct?

和最有趣的部分,我不知道如何编译grafic interfade项目,(我称之为ui),它最好创建一个战争或创建和耳朵与所有必要的(ui + logica + domini + ejb)我对此感到困惑,我使用已经构建的项目。我希望你能理解我的问题,我把剩下的pom留意了。 TY。

and the most interesting part, i don't know hos to compile the grafic interfade project,(i call ui), it's better create a war or create and ear with all the necessary (ui+logica+domini+ejb) i 'm a litlle bit confused about that, i uset o work with projects already estructure created. I hope you to unsderstand my question, i put the rest of pom to keep an eye. ty.

pom's gpt.domini. 

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>gpt</artifactId>
    <groupId>cat.base.gpt</groupId>
    <version>0.0.1</version>
  </parent>

  <groupId>cat.base.gpt.domini</groupId>
  <artifactId>gpt.domini</artifactId>
  <packaging>jar</packaging>

  <name>gpt.domini</name>
  <description>Definició del model de dades i de la façana del servei</description>
  </project>

pom的gpt.ear

pom's gpt.ear

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>gpt</artifactId>
    <groupId>cat.base.gpt</groupId>
    <version>0.0.1</version>
  </parent>

  <groupId>cat.base.gtp.ear</groupId>
  <artifactId>gpt.ear</artifactId>
  <name>gpt.ear</name>


  <packaging>ear</packaging>
  <description>Paquet de l'aplicació J2EE</description>

  <dependencies>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.domini</artifactId>
            <version>${project.parent.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.ejb</artifactId>
            <version>${project.parent.version}</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.logica</artifactId>
            <version>${project.parent.version}</version>
            <type>jar</type>
        </dependency>

  </dependencies>
</project>

pom的gpt.logica

pom's gpt.logica

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>gpt</artifactId>
    <groupId>cat.base.gpt</groupId>
    <version>0.0.1</version>
  </parent>


  <groupId>cat.base.gtp.logica</groupId>
  <artifactId>gpt.logica</artifactId>
  <name>climbing.logica</name>
  <packaging>jar</packaging>
  <description>Implementació del servei</description>


  <dependencies>
  <!-- de moment nomes el domini -->
    <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.domini</artifactId>
            <version>${project.parent.version}</version>
            <scope>provided</scope>
        </dependency>
  </dependencies>
</project>

pom的gpt.ejb

pom's gpt.ejb

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <artifactId>gpt</artifactId>
    <groupId>cat.base.gpt</groupId>
    <version>0.0.1</version>
  </parent>

  <groupId>cat.base.gtp.ejb</groupId>
  <artifactId>gpt.ejb</artifactId>
  <name>gpt.ejb</name>
  <packaging>ejb</packaging>
  <description>Publicació d'un servei en forma EJB</description>


  <dependencies>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.domini</artifactId>
            <version>${project.parent.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.logica</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.logica</artifactId>
            <version>${project.parent.version}</version>
            <classifier>tests</classifier>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jboss</groupId>
            <artifactId>jboss-annotations-ejb3</artifactId>
            <version>4.2.2.GA</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.client</groupId>
            <artifactId>jbossall-client</artifactId>
            <version>4.2.3.GA</version>
            <scope>test</scope>
        </dependency>
  </dependencies>
</project>

pom的gpt.logica

pom's gpt.logica

   <?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>gpt</artifactId>
    <groupId>cat.base.gpt</groupId>
    <version>0.0.1</version>
  </parent>


  <groupId>cat.base.gtp.logica</groupId>
  <artifactId>gpt.logica</artifactId>
  <name>climbing.logica</name>
  <packaging>jar</packaging>
  <description>Implementació del servei</description>


  <dependencies>
  <!-- de moment nomes el domini -->
    <dependency>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>${project.parent.artifactId}.domini</artifactId>
            <version>${project.parent.version}</version>
            <scope>provided</scope>
        </dependency>
  </dependencies>
</project>

pom's gpt.ui

pom's gpt.ui

此处全部spring-rich.faces的依赖关系......或者更好的父母的pom?
包装像战争?或者在de module ear? ty。

here all the dependencies of spring-rich.faces..or better at parent's pom? packaging like a war?? or inside de module ear?? ty.

推荐答案

虽然已经有一个公认的答案,但我认为值得向你提供更多信息,因为在我看来那个,提问者和接受的答案都被Maven中的不同概念弄乱了。

Although there is already an accepted answer, I believe it worth to give more information to you as it seems to me that, both the questioner and the accepted answer got messed up with different concepts in Maven.

Maven中有两个概念经常混淆。聚合(又称多模块)POM和父POM是无关紧要的,尽管使用一个POM来实现这两个目的是可以的。

There are two concepts in Maven often got mixed up. Aggregation (aka Multi-module) POM and Parent POM are something irrelevant, although it is fine to use one POM to serve for both purpose.

多模块项目旨在描述项目之间的聚合关系,以便我们可以构建多个相关项目作为一个整体,并且所有子项目都是在同一个项目中构建的反应堆。父项目旨在提供共享项目设置。它甚至可以存在于项目结构之外(例如,我可能有一个公司明智的父POM)

Multi-module project aims to describe the aggregation relationship between projects, so that we can build multiple related project as a whole, and all sub-projects are built in the same reactor. Parent project aims to provide shared project settings. It can even exists out of the project structure (e.g. I may have a company-wise parent POM)

我个人建议只有一个多模块POM来声明项目的聚合(层次结构),以及用于声明共享设置的单独父POM。

Personally I recommend to have a multi-module POM only to declare the aggregation (hierarchy) of projects, and having a separate parent POM to be used to declare shared settings.

ie

my-proj    // aggregation only
    + my-proj-parent    // parent POM
    + my-proj-main
    + my-proj-web
    + my-proj-ear






2)POM与EAR的共享依赖关系



同样,这是两个独立的概念。


2) Shared Dependency for POM vs in EAR

Again, these are two separate concepts.

可以将依赖项放在父POM中。当你把它放在那里时,这意味着继承的项目将具有这种依赖性。这方面没有对错,只要你知道你在做什么(我个人用不同的方式,将在后面描述)。

It is fine to put dependencies in parent POM. When you put it there, it means the inherited project is going to have such dependency. There is no right or wrong on this, as long as you know what you are doing (personally I am using different way, will be described later).

但是,是否将共享JAR放在EAR中并保持瘦的WAR,或者是否具有完整WAR的普通EAR与您的依赖关系无关。它更多的是关于EAR的包装策略。因此,改变Maven依赖的范围只是因为你要将项目打包为瘦战,这样的方法简直搞乱了整个maven依赖的概念。更可怕的是,在创建EAR时,您需要找出其包含的WAR的所有依赖关系并将其逐个添加到EAR POM中,这无疑不是最佳解决方案

However, whether to put shared JARs in EAR and keep skinny WAR, or have a plain EAR with a "full" WAR has nothing to do with your dependency. It is more about the packaging strategy of EAR. Therefore, changing scope of Maven dependency just because you are going to package the project as skinny war, such approach is simply messing up the whole concept of maven dependency. Even more horrible is, when creating your EAR, you need to find out all the dependencies of its included WARs and add it one by one to the EAR POM, that's doubtless not an optimal solution

遗憾的是,目前的Maven EAR插件仍然没有办法宣布一个瘦小的战争包装策略。但是,有一些解决方法允许您这样做,而不会弄乱Maven依赖范围。 http://maven.apache.org/plugins/maven -ear-plugin / examples / skinny-wars.html http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html

A pity that current Maven EAR plugin still has no way to declare a skinny war packaging strategy. However there are some workarounds which allow you to do so, without messing around the Maven dependency scope. http://maven.apache.org/plugins/maven-ear-plugin/examples/skinny-wars.html http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html

(更新:瘦弱的战争似乎已经更新,其中一个解决方法似乎已经消失。 http://docs.codehaus.org/display/MAVENUSER/Solving+the+Skinny+Wars+problem?focusedCommentId=212631587#comment-212631587 这包括WAR类型POM POM类型,因此我们不需要在EAR中再次声明依赖项)

(Update: The skinny war receipe seems updated and one of the workaround seems gone. http://docs.codehaus.org/display/MAVENUSER/Solving+the+Skinny+Wars+problem?focusedCommentId=212631587#comment-212631587 This is to include WAR type POM as POM type, so that we do no need to declare the dependencies again in EAR)

正如我前面提到的,放置依赖没有对错父母的倾向。但是你应该知道,这种方式实际上意味着所有继承的项目都会有这种依赖,这大多是不正确的。

As I mentioned before, there is no right or wrong to put dependencies in parent. However you should know that, such way actually means all inherited project is going to have such dependency, which is mostly incorrect.

例如,我有一个 foo -a foo-b foo 下的项目,它们都继承了 foo的亲。假设foo-a正在使用 spring-core ,而foo-b的整个逻辑与它无关,如果你将spring-core作为依赖关系放在foo-parent中,你看看foo-b,它是不可思议的有无关的依赖关系spring-core。

for example, I have a foo-a and foo-b projects under foo, which both inherits foo-parent. Assume foo-a is using spring-core while whole logic of foo-b has nothing to do with it, if you put spring-core as dependency in foo-parent, when you look at foo-b, it is unncessarily having unrelated dependencies spring-core.

正确的方法是在父POM中只包含依赖项(和其他设置)应该在所有继承的项目中共享。例如,单元测试相关的依赖关系可能是一个不错的选择。集成测试的依赖性可能是另一个例子。

The proper way to do is only include dependencies (and other settings) in parent POM that should be shared across all inherited projects. For example, unit testing related dependencies may be a good choice. Dependencies for integration testing may be another example.

但是,这并不意味着我们应该单独声明每个项目中的依赖项。最大的问题之一是这种方法很难在整个项目中维护相同版本的依赖项。

However, it doesn't mean we should declare dependencies in each project individually. One of the biggest problem is such approach is going to be hard to maintain same version of dependencies across the whole project.

为了解决这个问题,我的建议是在父POM中使用 dependencyManagement ,它声明了版本(可能还有其他设置,如范围,排除)。声明dependencyManagement不会在继承的POM中引入实际依赖项。它只是声明:如果你宣布这样的依赖,这将是要使用的设置。在每个继承的POM中,只需声明依赖项的组和工件(可能还有一些项目特定的设置),这样就可以遵循父POM中声明的版本。

In order to solve such issue, my recommendation is to make use of dependencyManagement in parent POM, which declares the version (and maybe other settings like scope, excludes). Declaring dependencyManagement is not introducing actual dependencies in inherited POM. It simply declare: "If you declare such dependency, this will be the settings to use". In each inherited POM, simply declare the dependencies' group and artifact (and maybe some project specific settings), so that you can follow the version declared in parent POM.

也许有点难以理解,这是一个例子:

Maybe a bit hard to understand, here is an example:

foo-parent

<project>
  <dependencyManagement>  // dependency management doesn't bring actual dependency
    <dependencies>
      <dependency>
         <groupId>org.springframework<groupId>
         <artifactId>spring-core<artifactId>
         <version>3.1.0.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.hibernate<groupId>
         <artifactId>hibernate-core<artifactId>
         <version>3.6</version>
      </dependency>
    </dependencies>
  <dependencyManagement>

  <dependencies>    // actual shared dependency
    <dependency>
       <groupId>junit<groupId>
       <artifactId>junit<artifactId>
       <version>4.11</version>
       <scope>test</scope>
    </dependency>
  </dependencies>
  .....
<project>

foo -a

<project>
  <dependencies>
    <dependency> // note: no version declared
      <groupId>org.springframework<groupId>
      <artifactId>spring-core<artifactId>
    </dependency>

    // junit dependency is inherited
  <dependencies>
<project>

这篇关于多模块项目的结构化。如何包装模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆