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

发布于 2024-09-26 09:03:13 字数 4154 浏览 7 评论 0原文

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

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

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

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

Maybe我在复制和粘贴过程中破坏了某些内容,但 XML 是有效的。

  • 当我运行 mvn package 时,未创建项目 WAR 文件,但所有组件包已创建且格式良好。
  • 然后,如果我运行 mvn war:war,则 WAR 文件将生成为空。

如何解决这个问题?

I have a Maven project, with 4 components Web, Persistence, Common and Other.

The relevant stuff off my POM files:

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

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

Maybe I broke something in the copy&paste, but the XML is valid.

  • when I run mvn package, the project WAR file isn't created, but all components packages are created and well formed.
  • if, then, I run mvn war:war, the WAR file is generated empty.

How can fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2024-10-03 09:03:13

在根项目中安装插件不起作用。您可以在此处配置插件,如下所示

<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 插件配置移至根项目中的 pluginManagement 并包含

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

在您的 war 项目中,或者将整个配置移至 war 中。

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

Having plugins in the root project does not work. You can configure the plugins here, like this

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

but they still have to be referenced in the subprojects to be active (except for plugins that are part of the default process, like maven-compiler-plugin and maven-resource-plugin).

So either you move your war-plugin configuration to pluginManagement in the root project and include

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

in your war project or you move the entire configuration into the war.

Additional note:
Make sure the order of the modules in the root pom is aligned to the dependency relations between the projects (In your case just reverse the order).

一生独一 2024-10-03 09:03:13

我创建了一个类似的项目结构并粘贴了您提供的 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 项目本身、它的结构或类似的东西有关。请显示它的结构以及 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 模块本身中配置它是有意义的。

I created a similar project structure and pasted the POMs you provided but couldn't reproduce the problem. Running mvn package from the aggregating pom just works as expected:

$ 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] ------------------------------------------------------------------------
...

And produces the following result:

$ 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

So your problem must have something to do with your WAR project itself, its structure or something like this. Please show its structure and the output of Maven when running war:war on it.


By the way, here is how a POM should typically look like in a multi-modules build:

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

Another thing, the war plugin configuration from the parent project is inherited (you can check that by running help:effective-pom in the web module) but it would make sense to configure it in the web module itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文