HotSwaping code into “mvnDebug tomcat:run”

眉间皱痕 提交于 2019-12-03 15:32:52

You can use Jrebel - it works exactly as you expect. You just need to specify javaagent and it will reload all classes and framework changes during runtime. It also works with remote servers

To integrate it with maven project you have to add jrebel-maven-plugin, which will generate all configuration (rebel.xml) files.

Without JRebel you can use standard HotSwap mechanism but it only allows to reload method bodies

JRebel is a good option. It isn't cheap but there are open source licences available. The installation instructions for Maven are here: http://zeroturnaround.com/software/jrebel/learn/maven/

We get this working through a specific run profile and embedded Tomcat. This is a specific sub-module that depends on the other projects that build the web application wars. So if you configure something like the following in the runner submodule:

<profiles>
         <profile>
             <id>run</id>
             <build>
                 <plugins>
                     <plugin>
                         <groupId>org.apache.tomcat.maven</groupId>
                         <artifactId>tomcat7-maven-plugin</artifactId>
                         <executions>
                             <execution>
                                 <id>run-wars</id>
                                 <goals>
                                     <goal>run-war-only</goal>
                                 </goals>
                                 <phase>integration-test</phase>
                             </execution>
                         </executions>
                         <configuration>
                            <warDirectory>theWar</warDirectory>
                            <path>/relativepath</path>
                            <systemProperties>
                            <webapps>
                                <webapp>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>myArtifact</artifactId>
                                    <version>${project.version}</version>
                                    <type>war</type>
                                    <asWebapp>true</asWebapp>
                                    <contextPath>myContext</contextPath>
                                 </webapp>
                             </webapps> 
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
</profiles>

You can run this with mvn package -Prun which you should be able to shut down with Ctrl+C.

Then, whilst this is running in one terminal, make your code changes, open a new terminal and run mvn compile. With JRebel going the changes should be reflected virtually instantly in your web app.

There should be nothing preventing you from running these same goals through Eclipse's m2e plugin.

One of the best solutions for your situation is JRebel

  • What you need to do is make the appropriate changes to the framework and integrate it using a jrebel-maven-plugin.
    It allows to reload all the method bodies.
  • Once you are done with the link-up a auto xml configuration file will be generated which will be used as a config file for the plugin.
  • Mention Your Java Agent as well.
  • After the successful test you can also integrate it with a build

It seems that you may be using some old version of the maven tomcat plug-in, because newer versions should be referred as tomcat6 or tomcat7, e..g. mvnDebug tomcat7:run. I'm on 2.2 and hot swap from Eclipse works just fine.

To clarify a little more, that's what I see in the command prompt when starting the app with mvn tomcat:run:

...
[INFO] >>> tomcat-maven-plugin:1.1:run (default-cli) @ tomcatMvnDebug >>>
...

Notice the 1.1.

And that's what I see when starting the app with mvn tomcat7:run:

...
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) @ tomcatMvnDebug >>>
...

Notice that this time maven uses tomcat plug-in version 2.2.

I'd use the HotSwap functions of Eclipse or IntelliJ. They both work very well for me: Just run maven goal in debug mode (tomcat:run).

We generally develop the web applications using grails which already has reload feature, I believe it's done with the spring-loaded plugin. It may be a replacement to jRebel (although I'm not sure if it will work for you, but if you want to save a few hundreds $$, may be worth trying).

So, basically I see the following options:

  • Use the framework which already supports reloading classes (Grails, Play)
  • Use some javaagent like spring-loaded or JRebel
  • Use some javaagent integrated into the IDE (I use IntelliJ IDEA and highly recommend it. Not sure if it works in the free community edition though).

https://github.com/spring-projects/spring-loaded

There is a maven plugin. It doesn't seem to be maintained. However from short look at the source code I believe you can make it work.

It is based on similar plugin for ant, so alternatively you can use maven antrun to execute this

Nevertheless it is going to have the same capabilities (and limitations) as hotswap in any IDE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!