【问题标题】:How to hot deploy JSP changes in maven app?如何在 Maven 应用程序中热部署 JSP 更改?
【发布时间】:2013-05-28 17:29:09
【问题描述】:

我有一个 Maven 网络应用程序。我正在使用 springsource 工具套件及其内置的 tc Fabric 服务器。

每次我进行任何更改时,我都必须执行 mvn clean install 并重新启动服务器。甚至对于JSP 的改动。

有什么方法可以让我只进行 JSP 更改,它们会像常规 Web 应用程序(而不是 maven 应用程序)一样在刷新时反映在浏览器中。

我已经在互联网上搜索过,但还没有成功。

仍然没有任何线索。它使开发过程非常缓慢。我研究了 jrebel,但它不是免费的,我不是在寻找类的热部署,而只是 JSP、javascript、css 等的热部署。

【问题讨论】:

    标签: maven hotdeploy tcserver


    【解决方案1】:

    这是我在 Linux 上使用 JBoss 时的简单解决方案(解决方法?)。它也应该对 Tomcat 或任何其他支持 exploaded war 的容器有效。

    1. 使用mvn war:inplacesrc/main/webapp 中创建爆炸战争。 这将在那里复制类和库。如果您只是更改 JSP(或其他 /webapp/ 文件),则无需重复此步骤。 或者,您可以在src/main/webapp/WEB-INF 中创建指向classeslib 的符号链接:

    cd src/main/webapp/WEB-INF
    ln -s ../../../../target/classes
    mvn package
    ln -s ../../../../target/*/WEB-INF/lib
    

    2. 在部署目录中创建指向 src/main/webapp 的符号链接:

    cd $DEPLOYMEN_DIR
    ln -s $MYAPP_DIR/src/main/webapp myapp.war
    

    这使得对 JSP 和其他 webapp 文件的更改立即可用。 如果您想查看classes 中的更改,那么您可以触发应用程序的重新加载,只有我正在修改 web.xml。你可以运行这个脚本来监控触发重启:

    #!/bin/bash
    while sleep 1; do 
        if [[ -n $(find WEB-INF/classes -newer WEB-INF/web.xml -type f) ]];then
            date
            touch WEB-INF/web.xml
        fi
    done
    

    src/main/webapp/ 目录运行它。

    关于 JBoss AS 7 的注意事项:这里要触发重新加载,您需要创建一个 myapp.war.dodeploy 文件而不是触摸 web.xml

    【讨论】:

      【解决方案2】:

      您可以使用 Eclipse Filesync plugin 来实现这一点。您可以配置插件以将 maven 输出文件夹映射到应用程序服务器目录

      我知道这不是 maven 方式,但它确实有效。

      也许这个question 会提供更多见解。

      【讨论】:

        【解决方案3】:

        您可以使用 clean 和 resources 插件轻松清理和复制静态文件,但它并不总是适用于 JSP 文件。如果要复制的 JSP 文件中的 java 源引入了新的依赖项,则您不会将其复制到 lib 文件夹。在这种情况下,应用程序会以 ClassNotFoundException 中断。

        即使它被复制,它仍然可能会中断,因为必须将服务器配置为扫描具有依赖关系的文件夹并刷新类路径。我相信这就是热部署的开始 (details)。

        也试试 Vinay 的建议。从他的回答来看,tc 服务器似乎默认支持扫描依赖项,并且通过适当的 maven 构建,这可能是一个令人满意的解决方案。

        清除源目录中的静态文件并将其复制到部署位置:

              <plugin>
                    <groupId>org.apache.maven.plugins</groupId>           
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.5</version>
                    <executions>
                        <execution>
                            <id>clean-loaded</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>clean</goal>
                            </goals>
                            <configuration>
                                <excludeDefaultDirectories>true</excludeDefaultDirectories>
                                <filesets>
                                    <fileset>
                                        <directory>${path.server.input}</directory>
                                        <followSymlinks>false</followSymlinks>
                                        <useDefaultExcludes>false</useDefaultExcludes>
                                        <includes>
                                            <include>**/*.jsp</include>
                                            <include>**/*.js</include>
                                            <include>**/*.html</include>
                                            <include>**/*.css</include>
                                            <include>**/*.png</include>
                                            <include>**/*.gif</include>
                                            <include>**/*.jpg</include>
                                            <include>**/*.jpeg</include>
                                        </includes>                               
                                    </fileset>
                                </filesets>
                            </configuration> 
                        </execution>
                    </executions>
                </plugin>
                            
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                      <execution>
                        <id>copy-compile-output</id>                    
                        <phase>compile</phase>
                        <goals>
                          <goal>copy-resources</goal>
                        </goals>
                            <configuration>
                                <outputDirectory>${path.server.input}</outputDirectory>
                                <overwrite>true</overwrite>
                                <includeEmptyDirs>true</includeEmptyDirs>
                                <filtering>true</filtering>
                                <resources>
                                    <resource>
                                        <directory>${path.jsp.source}</directory>
                                        <targetPath>${path.element.jsp.deploy}</targetPath>
                                        <includes>
                                            <include>**/*.jsp</include>
                                        </includes>
                                    </resource>                     
                                    <resource>                      
                                        <directory>${path.static.source}</directory>
                                        <targetPath>${path.element.static.deploy}</targetPath>
                                        <includes>
                                            <include>**/*.js</include>
                                            <include>**/*.html</include>
                                            <include>**/*.css</include>
                                            <include>**/*.png</include>
                                            <include>**/*.gif</include>
                                            <include>**/*.jpg</include>
                                            <include>**/*.jpeg</include>
                                        </includes>
                                    </resource>                     
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
        

        将此添加到您的 properties 部分:

        <path.server.input>ABSOLUTE_PATH_TO_DEPLOYED_WEBAPP_ROOT</path.server.input>
        <path.jsp.source>ABSOLUTE_PATH_TO_JSP_SOURCE_ROOT</path.jsp.source>
        <path.static.source>ABSOLUTE_PATH_TO_STATIC_SOURCE_ROOT</path.static.source>
        <path.element.jsp.deploy>REALTIVE_PATH_TO_JSP_DEPLOY_ROOT</path.element.jsp.deploy>
        <path.element.static.deploy>REALTIVE_PATH_TO_STATIC_DEPLOY_ROOT</path.element.static.deploy>
        

        path 开头的属性必须是绝对路径或以${project.basedir} 或类似名称开头。以path.element 开头的属性是相对路径,这意味着它们不能以/ 开头或以另一个绝对路径的属性开头。之所以如此,是因为资源插件在 outputDirectory/targetPath (@987654323@, @987654324@) 中复制

        根据我的经验,IDE 通常将其清理并构建 UI 操作绑定到编译阶段。此外,IDE 通常有一种方法可以将 shell 命令或 maven 自定义目标映射到其 UI 菜单中。

        使用 clean-and-build 运行

        插件已经绑定到编译阶段。为确保在编译阶段结束时干净插件将在资源插件之前运行,请将它们放在插件部分的末尾。一个插件是否定义了两次都没关系,只要确保从顶部读取 pom 时,第一个干净的插件定义在第一个资源插件定义之前。

        作为单独的动作运行

        execution 标签下更改如下:

        <id>default-cli</id>
        <phase>never</phase>
        

        现在它不会在compile 阶段运行,而是通过命令行调用:

        mvn clean:clean resources:copy-resources
        

        在这种情况下,插件定义在 pom 中的位置是无关紧要的,因为您使用命令参数 order 定义它们的顺序。如果这适合您,您的 IDE 很可能有办法将此命令映射为从其 UI 菜单可见的自定义目标。

        在这两种情况下,我建议在第一次运行时备份项目文件夹。

        【讨论】:

          【解决方案4】:

          我目前使用配置文件将 JSP 复制到我的目标目录,一旦我需要更新 JSP,我会从 Eclipse 调用它。 你也可以通过添加执行来复制这样的类文件。

          <profile>
              <id>copyJsps</id>
              <build>
                  <plugins>
                      <plugin>
                          <artifactId>maven-resources-plugin</artifactId>
                          <version>2.5</version>
                          <configuration>
                              <outputDirectory>${basedir}/target/app/WEB-INF/jsp</outputDirectory>
                              <resources>
                                  <resource>
                                      <directory>src/main/webapp/WEB-INF/jsp</directory>
                                      <filtering>true</filtering>
                                  </resource>
                              </resources>
                          </configuration>
                      </plugin>
                  </plugins>
              </build>
          </profile>
          

          使用:mvn resources:copy-resources -PcopyJsps

          【讨论】:

          • 最有帮助,并且可以完全按照问题的要求进行。 +1
          猜你喜欢
          • 1970-01-01
          • 2015-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-18
          • 2011-12-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多