1

我有一个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 是有效的。

如何解决这个问题?

4

2 回答 2

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 中模块的顺序与项目之间的依赖关系对齐(在您的情况下只需颠倒顺序)。

于 2010-10-08T19:20:03.640 回答
1

我创建了一个类似的项目结构并粘贴了您提供的 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
│ │ ├── 类
│ │ ├── 库
│ │ │ ├── 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 项目本身、其结构或类似的东西有关。请在其上运行时显示其结构和 Maven 的输出war:war


顺便说一下,以下是 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 插件配置继承的(您可以通过help:effective-pom在 web 模块中运行来检查),但在 web 模块本身中配置它是有意义的。

于 2010-10-09T00:56:33.137 回答