2

我的 spring MVC 和 hibernate 应用程序有一个多 maven 模块。

我的布局如下:

/myapp-web
/myapp-web/src/main/java
/myapp-web/src/main/webapp
/myapp-web/src/main/webapp/web-inf
/myapp-web/src/main/webapp/web-inf/web.xml
/myapp-web/src/main/webapp/web-inf/web-context.xml (spring wiring code etc)
/myapp-web/src/main/webapp/web-inf/config/hibernate.cfg.xml
/myapp-web/src/main/webapp/assets/... (folder for css, javascript, images)
/myapp-web/src/main/webapp/views (folders with freemarker .ftl files)

我的 pom.xml:

    ..
    <packaging>war</packaging>
    ..
    <dependency>
        <artifactId>jetty-server</artifactId>
        <groupId>org.eclipse.jetty</groupId>
        <version>${jetty-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>${jetty-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>${jetty-version}</version>
    </dependency>

我有另一个嵌入式码头实例,我成功设置并且可以运行。我什至设置了一个启动/停止脚本,所以它可以在 Ubuntu 上运行。这不是一个 spring mvc 应用程序,所以它更容易,因为它没有 web.xml 文件等。 我创建了一个 /assembly/assembly.xml 文件,我是否也需要为 spring mvc 应用程序这样做?

然后我将此添加到我的 pom.xml 插件部分:

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>${maven-assembly-plugin}</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>
        <!-- jar plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven-jar-plugin}</version>
            <configuration>
                <archive>

                </archive>
            </configuration>
        </plugin>
        <!-- jetty plugin -->
        <plugin>
            <!-- This plugin is needed for the servlet example -->
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty-version}</version>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.myapp.HttpServer</mainClass>
            </configuration>
        </plugin>

我需要为我的 spring mvc 应用程序做同样的事情吗?

我在 stackoverflow 上发现了一些线程,但我无法理解 ant 部分,因为我没有使用 ant。我也不确定如何将其移植到 Maven 中:

令我惊讶的是,除了单个 servlet 示例之外,没有关于如何执行此操作的单一好的参考。

我需要为此创建一个 /assembly/assembly.xml 文件,还是为 Web 应用程序创建一个不同的文件?

有人可以逐步向我解释这个,以便我可以让它工作吗?请询问,我会发布我的项目所需的任何内容,但我认为我涵盖了我当前环境的所有内容,即:

  • 使用多 Maven 设置
  • 春天mvc
  • 休眠
  • 模板的freemarker
  • 上面给出了文件夹结构

目标是能够部署这个war文件,创建启动/停止脚本,这样我就可以将它部署到我的服务器并启动/停止码头实例。

4

3 回答 3

8

您不需要 assembly.xml 文件。

Jetty 是用来启动 webapp 的,springified 与否。因此,您必须在主类中创建一个 Jetty 服务器,添加您的 webapp 上下文并启动服务器。您可以通过 2 种方式解决您的问题: - 使用“通用”Java EE 服务器创建一个 web.xml,并将此描述符添加到您的 Jetty 服务器。- 使用 servlet 上下文编程在主类中加载您的 Spring 上下文: https ://github.com/s4nchez/jetty-spring-mvc-jar

于 2012-07-04T12:05:49.570 回答
1

assembly.xml 文件主要用于创建 zip 文件。对于简单的 web 存档,war 文件 maven 的正常结构应该足够了。

您要求提供多模块示例。我建议寻找应用程序保险丝示例。即使您不使用 app fuse 框架,它也具有良好的 maven 文件结构。我用它的快速启动创建了多模块休眠 spring mvc 应用程序。

这是项目的目录结构。

D:.
    core
        src
            main
                java
                resources
                    META-INF
    src
        site
    web
        src
            main
                java
                    com
                    mycompany
                        webapp
                            controller
                            filter
                            jsp
                            listener
                            search
                            spring
                            taglib
                            util
            resources
                META-INF
            webapp
                common
                decorators
                images
                scripts
                    calendar
                WEB-INF

这是 web 项目 pom.xml 的相关部分。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>

<artifactId>web</artifactId>
<packaging>war</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.26</version>
            <configuration>
                <contextPath>/</contextPath>
                <scanIntervalSeconds>3</scanIntervalSeconds>
                <scanTargetPatterns>
                    <scanTargetPattern>
                        <directory>src/main/webapp/WEB-INF</directory>
                        <excludes>
                            <exclude>**/*.jsp</exclude>
                        </excludes>
                        <includes>
                            <include>**/*.properties</include>
                            <include>**/*.xml</include>
                        </includes>
                    </scanTargetPattern>
                </scanTargetPatterns>
            </configuration>
        </plugin>
    </plugins>
...
<dependencies>
    <dependency>
        <groupId>${project.parent.groupId}</groupId>
        <artifactId>core</artifactId>
        <version>${project.parent.version}</version>
    </dependency>
 ...
</dependencies>
</project>

它依赖于核心项目。每当您编译多模块项目时,它都有所有必要的部分。但是,如果您只需要从 Web 项目目录中工作。您需要在 core 目录中执行 mvn install ,这样它就安装在您的本地存储库中并且您的 Web 项目可以使用它。如果您对核心项目进行了任何更改,您需要从主目录编译。否则,您可以照常使用 web 项目目录中的 maven。

如您所见,不需要 assembly.xml 文件。您应该将 assembly.xml 文件用于比 war 文件更复杂的结构,例如将包含的自定义安装程序结构;war 文件、帮助文件、Web 服务器可执行文件、将所有这些文件放在一起的安装程序文件。

我可以在主目录中使用 mvn package 命令来创建 war 文件。我也可以在 web 项目目录中使用 jetty:run 命令来运行 jetty 和测试 web 应用程序。

于 2012-07-06T07:30:30.387 回答
0

我有一个非常相似的设置,我认为您可以通过最小的更改来使用它。我正在运行一个带有 maven、spring、hibernate 和 freemarker 等的 webapp。我不需要任何 assembly.xml 或 ant 任务。

我的方法是将应用程序打包为 jar。然后这个 jar 有一个主类,它启动 jetty 并将自身部署为战争。有关更多详细信息,请参见此处:

http://draconianoverlord.com/2009/01/10/war-less-dev-with-jetty.html

这个类可以很容易地定制为使用端口号等参数。

要运行应用程序,我认为有两个不错的选择:

只需使用 Java 执行该类。这里的问题是正确设置类路径。为此,我使用 maven dependency:build-classpath 目标。这将输出可在您的 java 执行命令中使用的完整类路径。这样做的缺点是您需要在运行它的机器上配置 maven 和 maven repo。就我而言,这是可以接受的,而且这种方法效果很好。

或者您可以创建一个带有依赖项的可执行 jar。为此,我推荐 maven shade 插件。请参阅http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html这种方法的缺点是您最终会得到一个巨大的文件,因此类路径错误和依赖问题可能很难解决。此外,如果库依赖于 Meta-Inf 文件,它们有时会相互覆盖。Maven 阴影插件提供了解决此问题的方法,但仍然很难诊断问题。

这两种方法都应该允许您保留当前的文件夹结构和 web.xml 等,但如果您确实有任何问题 - 请在评论中告诉我,我可以扩展答案。

于 2012-07-11T18:23:02.940 回答