2012-07-01 23 views
2

我有一个用于我的spring MVC和hibernate应用程序的多重maven模块。嵌入式跳台MVC

我的布局如下:

/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上发现了几个threads,但我不明白蚂蚁的一部分,因为我没有使用ant。我也不知道如何移植到maven的工作:

我很惊讶,没有一个单一的良好参考如何做到这一点,而不是单个servlet的例子。

是否需要为此创建/assembly/assembly.xml文件,或者针对Web应用程序,情况有所不同?

有人可以解释这一步一步一步,所以我可以得到这个工作? 请让我将张贴必要的东西为我的项目,但我想我涵盖了所有我现在的环境了,即:

  • 使用多行家设置
  • Spring MVC的
  • 冬眠
  • 为模板的freemarker
  • 文件夹结构上面

给出的目标是能够部署这个娃r文件,创建启动/停止脚本,以便我可以将其部署到我的服务器并启动/停止jetty实例。

+0

如果你坚持标准的maven项目布局(你似乎做了什么),maven jetty:run命令将从相应的路径中选择你的web应用程序,而不是加载.war。在我使用的简单项目中,不需要额外的配置,足以进行测试和开发。如果你需要部署生产。战争,看看[货物插件](http://cargo.codehaus.org/Maven2+plugin)? – Pyranja

+0

我不想在本地使用mvn jetty运行码头:运行命令,这是为了部署。 – Blankman

回答

7

你不需要assembly.xml文件。

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

1

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

您要求提供多模块示例。我建议寻找app fuse examples。即使你不使用应用保险丝框架,它也具有良好的Maven文件结构。我通过快速入门创建了多模块hibernate 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项目目录下工作。您需要在核心目录中执行mvn install,这样它就会安装在本地存储库中,并且您的web项目可以使用它。如果您对核心项目进行了任何更改,则需要从主目录进行编译。否则,您可以正常使用web项目目录中的maven。

正如你所看到的,不需要assembly.xml文件。您应该使用assembly.xml文件来创建比war文件更复杂的结构,例如,将包含的自定义安装程序结构;战争文件,帮助文件,网络服务器可执行文件,将所有这些文件放在一起的安装程序文件。

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

0

我有一个非常类似的设置,我认为你可以用最小的改变。我正在用maven,spring,hibernate和freemarker等运行webapp。我不需要任何assembly.xml或ant任务。

我的方法是将应用程序打包为jar。这个罐子然后有一个主要类开始码头,并基本上作为战争部署。在这里看到更多的细节:

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

这个类可以方便地定制需要的参数,如端口号等

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

只需使用Java执行该类。但是,这里的疑惑是正确设置类路径。为此,我使用maven依赖项:build-classpath目标。这会输出可用于Java执行命令的完整类路径。这样做的缺点是你需要在你运行这个的机器上配置maven和maven repo。在我的情况下,这是可以接受的,这种方法很好。

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

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