【问题标题】:Generating WAR files from a multiproject Maven configuration从多项目 Maven 配置生成 WAR 文件
【发布时间】:2010-10-08 18:16:45
【问题描述】:

我有一个Maven 项目,有 4 个组件 Web、Persistence、Common 和 Other。

我的POM 文件中的相关内容:

父 POM:

<groupId>com.test</groupId>
<artifactId>test</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
    <module>components/TestWeb</module>
    <module>components/TestOther</module>
    <module>components/TestPersistence</module>
    <module>components/TestCommon</module>
</modules>


<build>
    <defaultGoal>package</defaultGoal>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <dependentWarExcludes>
                    **/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**
                </dependentWarExcludes>
            </configuration>
        </plugin>
     </plugins>
 </build>

常见的pom:

<modelVersion>4.0.0</modelVersion>
<artifactId>test-common</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <relativePath>../../pom.xml</relativePath>
    <version>0.0.1-SNAPSHOT</version>
</parent>

持久化pom:

<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-persistence</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <relativePath>../../pom.xml</relativePath>
    <version>0.0.1-SNAPSHOT</version>
</parent>


<dependencies>
  <dependency>
    <groupId>com.test</groupId>
    <artifactId>test-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

网络 pom:

<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestWeb</name>

<parent>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <relativePath>../../pom.xml</relativePath>
    <version>0.0.1-SNAPSHOT</version>
</parent>

  <dependency>
    <groupId>com.test</groupId>
    <artifactId>test-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>


  <dependency>
    <groupId>com.test</groupId>
    <artifactId>test-persistence</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>


  <dependency>
    <groupId>com.test</groupId>
    <artifactId>test-other</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>

也许我在复制和粘贴中破坏了某些东西,但 XML 是有效的。

  • 当我运行 mvn package 时,没有创建项目 WAR file,但所有组件包都已创建且格式正确。
  • 然后,如果我运行mvn war:war,则生成的 WAR 文件为空。

如何解决这个问题?

【问题讨论】:

    标签: java maven-2 maven-war-plugin


    【解决方案1】:

    在根项目中包含插件不起作用。你可以在这里配置插件,像这样

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.0.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    

    但它们仍然必须在子项目中被引用才能处于活动状态(作为默认进程一部分的插件除外,例如 maven-compiler-plugin 和 maven-resource-plugin)。

    因此,要么将您的 war-plugin 配置移动到根项目中的 pluginManagement 并包含

    <build>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1</version>
            <execution>
                <goals>...</goals>
            </execution>
        </plugin>
    </build>
    

    在您的战争项目中,或者您将整个配置移动到战争中。

    补充说明: 确保根 pom 中模块的顺序与项目之间的依赖关系对齐(在您的情况下只是颠倒顺序)。

    【讨论】:

    • 谢谢阿里安。所以,这个想法已经全部包含在网络战争中,对吗? (我认为这将是父项目中的一场战争)
    • Having plugins in the root project does not work 嗯...插件及其配置 被继承,因此可以正常工作。是否应该配置战争插件是另一回事,但它“有效”。
    • @Pascal:好的,让我换个说法:我没有做 Hugo 想要的。即使设置被继承,插件仍然必须在子项目中引用才能执行。
    • 不,这也不是真的,插件及其配置是继承的(实际上是所有项目),您可以使用mvn help:effective-pom 进行检查。因此,当war:warpackage 期间为war 模块执行时,配置处于活动状态。换句话说,这不是问题(即使我不会像 OP 那样在父项目中配置 war 插件,但这是另一回事)。
    • 顺便说一下,模块的顺序并不重要。 Maven 从依赖项而不是模块的顺序派生构建顺序。
    【解决方案2】:

    我创建了一个类似的项目结构并粘贴了您提供的 POM,但无法重现该问题。从聚合 pom 运行 mvn package 就像预期的那样:

    $ mvn 包 [INFO] 正在扫描项目... ... [信息] --------------------------------------------- ------------------------- [信息] 反应堆摘要: [信息] --------------------------------------------- ------------------------- [信息] 未命名 - com.test:test:pom:0.0.1-SNAPSHOT .... 成功 [5.819s] [信息] 未命名 - com.test:test-common:jar:0.0.1-SNAPSHOT ..... 成功 [3.343s] [信息] 未命名 - com.test:test-persistence:jar:0.0.1-SNAPSHOT SUCCESS [0.136s] [信息] 未命名 - com.test:test-other:jar:0.0.1-SNAPSHOT ...... 成功 [0.079s] [信息] 未命名 - com.test:test-web:war:0.0.1-SNAPSHOT ........ 成功 [1.899s] [信息] --------------------------------------------- ------------------------- [信息] --------------------------------------------- ------------------------- [信息] 构建成功 [信息] --------------------------------------------- ------------------------- ...

    并产生以下结果:

    $树。 . ├── 组件 │   ├── TestCommon │   │   ├── pom.xml │ │ ├── ... │   │   └── 目标 │   │   ├── maven-archiver │   │   │   └── pom.properties │   │   └── test-common-0.0.1-SNAPSHOT.jar │   ├── TestOther │   │   ├── pom.xml │ │ ├── ... │   │   └── 目标 │   │   ├── maven-archiver │   │   │   └── pom.properties │   │   └── test-other-0.0.1-SNAPSHOT.jar │   ├── TestPersistence │   │   ├── pom.xml │ │ ├── ... │   │   └── 目标 │   │   ├── maven-archiver │   │   │   └── pom.properties │   │   └── test-persistence-0.0.1-SNAPSHOT.jar │   └── TestWeb │   ├── pom.xml │   ├── src │   │   └── 主要 │   │   └── webapp │ │ ├── index.jsp │   │   └── WEB-INF │   │   └── web.xml │   └── 目标 │   ├── maven-archiver │   │   └── pom.properties │   ├── test-web-0.0.1-SNAPSHOT │ │ ├── index.jsp │   │   ├── META-INF │   │   └── WEB-INF │   │   ├── 类 │   │   ├── lib │   │   │   ├── test-common-0.0.1-SNAPSHOT.jar │   │   │   ├── test-other-0.0.1-SNAPSHOT.jar │   │   │   └── test-persistence-0.0.1-SNAPSHOT.jar │   │   └── web.xml │   └── test-web-0.0.1-SNAPSHOT.war └── pom.xml

    所以你的问题一定与你的 WAR 项目本身、它的结构或类似的东西有关。请在上面运行war:war时显示它的结构和Maven的输出。


    顺便说一下,以下是 POM 在多模块构建中的典型外观:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <!--groupId>com.test</groupId--> <!-- unnecessary, you inherit it -->
      <artifactId>test-web</artifactId>
      <packaging>war</packaging>
      <!--version>0.0.1-SNAPSHOT</version--> <!-- unnecessary, you inherit it -->
      <parent>
        <groupId>com.test</groupId>
        <artifactId>test</artifactId>
        <relativePath>../../pom.xml</relativePath>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <dependencies>
        <dependency>
          <groupId>${project.groupId}</groupId> <!-- DRY, use built-in properties -->
          <artifactId>test-common</artifactId>
          <version>${project.version}</version> <!-- DRY, use built-in properties -->
        </dependency>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>test-persistence</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>test-other</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    </project>
    

    另一件事,来自父项目的war插件配置继承的(您可以通过在web模块中运行help:effective-pom来检查)但在web模块中配置它是有意义的本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-20
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      相关资源
      最近更新 更多