从多项目Maven配置生成WAR文件

时间:2010-10-08 18:16:45

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

我有一个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>

persistence 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>

web 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包时,项目WAR file未创建,但所有组件包都已创建并且格式正确。
  • 如果,那么,我运行mvn war:war,WAR文件将生成为空。

如何解决这个问题?

2 个答案:

答案 0 :(得分: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中模块的顺序与项目之间的依赖关系一致(在您的情况下,只需颠倒顺序)。

答案 1 :(得分:1)

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

$ mvn package 
[INFO] Scanning for projects...
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] Unnamed - com.test:test:pom:0.0.1-SNAPSHOT ............ SUCCESS [5.819s]
[INFO] Unnamed - com.test:test-common:jar:0.0.1-SNAPSHOT ..... SUCCESS [3.343s]
[INFO] Unnamed - com.test:test-persistence:jar:0.0.1-SNAPSHOT  SUCCESS [0.136s]
[INFO] Unnamed - com.test:test-other:jar:0.0.1-SNAPSHOT ...... SUCCESS [0.079s]
[INFO] Unnamed - com.test:test-web:war:0.0.1-SNAPSHOT ........ SUCCESS [1.899s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

并产生以下结果:

$ tree .
.
├── components
│   ├── TestCommon
│   │   ├── pom.xml
│   │   ├── ...
│   │   └── target
│   │       ├── maven-archiver
│   │       │   └── pom.properties
│   │       └── test-common-0.0.1-SNAPSHOT.jar
│   ├── TestOther
│   │   ├── pom.xml
│   │   ├── ...
│   │   └── target
│   │       ├── maven-archiver
│   │       │   └── pom.properties
│   │       └── test-other-0.0.1-SNAPSHOT.jar
│   ├── TestPersistence
│   │   ├── pom.xml
│   │   ├── ...
│   │   └── target
│   │       ├── maven-archiver
│   │       │   └── pom.properties
│   │       └── test-persistence-0.0.1-SNAPSHOT.jar
│   └── TestWeb
│       ├── pom.xml
│       ├── src
│       │   └── main
│       │       └── webapp
│       │           ├── index.jsp
│       │           └── WEB-INF
│       │               └── web.xml
│       └── target
│           ├── maven-archiver
│           │   └── pom.properties
│           ├── test-web-0.0.1-SNAPSHOT
│           │   ├── index.jsp
│           │   ├── META-INF
│           │   └── WEB-INF
│           │       ├── classes
│           │       ├── 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来检查)但是在它中配置它是有意义的网络模块本身。

相关问题