Maven 程序集插件生成不可部署的 war 文件

发布于 2024-11-02 16:01:35 字数 3206 浏览 4 评论 0 原文

我正在使用 Spring、Hibernate、GWT 和 Maven 3 开发一个多模块项目,并尝试将 war 文件分发部署到 servlet 容器(最好是 Tomcat 7 或 Jetty 8)。我使用 maven-assemble-plugin 2.2.1 创建 war 文件(我正在运行 maven 3)。在 Maven 构建过程中,一切都运行良好,并且创建了分发存档。

但是,当我尝试部署 war 文件时,尽管一切都已就位(Web-inf/lib 等页),但我得到了 ClassNotFound-Exceptions(例如 Spring ContextLoaderListener 等)。所以,网络应用程序没有启动。然后我将 war 文件解压到 servlet 容器的 webapp 目录中,一切正常......?

经过进一步调查,我发现了以下事情: 如果我获取由 maven-war-plugin (!) 创建的任意 war 文件 A',请将其内容替换为我使用 maven-assemble-plugin 创建的 war 文件中的解压内容(让我称之为 A.) 我可以看到发生了两件事:

  1. 生成的战争文件 A' 比我的原始文件 A 小几个字节,尽管它们的内容是相同的
  2. A' 的部署突然像一个魅力

这很奇怪,我完全不知道发生了什么。也许存在问题,maven-war-plugin 和 maven-assemble-plugin 处理 war 文件打包的方式不同?!我的意思是,战争只是一个具有某些预定义结构的重命名的 zip 文件...也许它与 Maven 完全无关,但与文件编码或其他东西有关?对此有什么想法吗?我很感激任何提示,这可以帮助我调查这个......

这是我的程序集描述符

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>dist</id>
<formats>
    <format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <unpack>true</unpack>
        <includes>
            <include>mypackage.client:parametermgmt:*</include>
            <include>mypackage.static:client:*</include>
        </includes>
        <useProjectArtifact>false</useProjectArtifact> 
        <outputDirectory>/</outputDirectory> 
    </dependencySet>

    <dependencySet>
        <unpack>false</unpack>
        <includes>
            <include>mypackage.server.database:domain:*</include>
            <include>mypackage.server.businessobjects:BOdomain:*</include>
            <include>mypackage.server.security:security:*</include>
            <include>mypackage.server.services:paramgmt:*</include>
        </includes>
        <useProjectArtifact>false</useProjectArtifact>    
        <outputDirectory>Web-inf/lib</outputDirectory>   
    </dependencySet>

    <dependencySet>
        <unpack>true</unpack>
        <includes>
            <include>mypackage.static:server:*</include>
        </includes>
        <useProjectArtifact>false</useProjectArtifact> 
        <outputDirectory>/</outputDirectory> 
    </dependencySet>

    <dependencySet>
        <unpack>false</unpack>
        <includes>
            <include>*</include>
        </includes>
        <excludes>
            <exclude>mypackage.*</exclude>
        </excludes>
        <scope>runtime</scope>
        <useProjectArtifact>false</useProjectArtifact>
        <outputDirectory>Web-inf/lib</outputDirectory>
    </dependencySet>

</dependencySets>

I'm working on a multi module project using Spring, Hibernate, GWT and Maven 3 and try to deploy a war-file distribution to a servlet container (preferably Tomcat 7 or Jetty 8). I create the war-file using maven-assembly-plugin 2.2.1 (I'm running maven 3). During the maven build everything works perfectly and the distribution archive is created.

But when I try to deploy the war file, I get ClassNotFound-Exceptions (e.g. Spring ContextLoaderListener and the like) although everything is in place (Web-inf/lib etc. pp.). So, the webapp isn't starting. Then I unpack the war-file into the servlet containers webapp directory and everything is working fine... ??

After further investigation I came across the following thing:
If I take an arbitrary war file A' created by maven-war-plugin (!), replace its content with the unpacked content from the war file I created using maven-assembly-plugin (let me call it
A.) I can see two things happen:

  1. the resulting war file A' is a few bytes smaller than my original file A although their content is identical
  2. the deployment of A' suddenly works like a charm

This is weird and I have absolutely no idea what's happening. Maybe there is an issue and maven-war-plugin and maven-assembly-plugin handle war file packaging differently?! I mean, a war is only a renamed zip file with some predefined structure... Maybe it has absolutely nothing to do with maven but with file encoding or other things? Any ideas on this? I appreciate any hint, that could help me in investigating this...

This is my assembly descriptor

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>dist</id>
<formats>
    <format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <unpack>true</unpack>
        <includes>
            <include>mypackage.client:parametermgmt:*</include>
            <include>mypackage.static:client:*</include>
        </includes>
        <useProjectArtifact>false</useProjectArtifact> 
        <outputDirectory>/</outputDirectory> 
    </dependencySet>

    <dependencySet>
        <unpack>false</unpack>
        <includes>
            <include>mypackage.server.database:domain:*</include>
            <include>mypackage.server.businessobjects:BOdomain:*</include>
            <include>mypackage.server.security:security:*</include>
            <include>mypackage.server.services:paramgmt:*</include>
        </includes>
        <useProjectArtifact>false</useProjectArtifact>    
        <outputDirectory>Web-inf/lib</outputDirectory>   
    </dependencySet>

    <dependencySet>
        <unpack>true</unpack>
        <includes>
            <include>mypackage.static:server:*</include>
        </includes>
        <useProjectArtifact>false</useProjectArtifact> 
        <outputDirectory>/</outputDirectory> 
    </dependencySet>

    <dependencySet>
        <unpack>false</unpack>
        <includes>
            <include>*</include>
        </includes>
        <excludes>
            <exclude>mypackage.*</exclude>
        </excludes>
        <scope>runtime</scope>
        <useProjectArtifact>false</useProjectArtifact>
        <outputDirectory>Web-inf/lib</outputDirectory>
    </dependencySet>

</dependencySets>

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

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

发布评论

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

评论(1

人间不值得 2024-11-09 16:01:35

maven-assemble-plugin 并不是为了制造战争。为此,请使用 maven-war-plugin

如果您需要为不同的环境(如测试、质量保证、生产)创建 war 文件。这也可以用作不同应用程序服务器的基础。但如果我们谈论的是应用程序服务器,这意味着我们应该创建ear文件而不是war文件(这意味着使用maven-ear-plugin而不是maven-war-plugin)。

|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |-- resources
    |   |-- environment
    |   |   |-- test
    |   |   |   `-- database.properties
    |   |   |-- qa
    |   |   |   `-- database.properties
    |   |   `-- production
    |   |       `-- database.properties
    |   `-- webapp

您需要以下程序集(对于每个环境),可以基于此作为模板创建:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

为了支持不同环境的不同工件,现在是使用 maven- assembly-plugin 的时候了,如下所示:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>test</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>qa</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>production</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>

结果是创建三个不同的程序集war 文件可以通过一次调用通过适当的分类器来区分 (详细说明可以在这里阅读):

mvn clean package

The maven-assembly-plugin is not intended to create a war. For this purpose use the maven-war-plugin instead.

If you need to create war files for different environments like test,qa, production. This can be used as well a base for different app servers. But if we are talking about a application server this means we should create ear files instead of war files (This means to use maven-ear-plugin instead of maven-war-plugin).

|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |-- resources
    |   |-- environment
    |   |   |-- test
    |   |   |   `-- database.properties
    |   |   |-- qa
    |   |   |   `-- database.properties
    |   |   `-- production
    |   |       `-- database.properties
    |   `-- webapp

You need the following assembly for that (for every environment) which can be created based on this as a template:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

To support different artifacts for different environments this is the time for the maven-assembly-plugin like this:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>test</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>qa</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>production</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>

The result is creating three different war files which can be distinguished by their appropriate classifier with a single call (detailed description can be read here):

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