jrebel configuration and usage with maven

2018-07-05 开始 myJrebel 不再免费

下面内容针对jrebel仍是一样可以参考。

注册激活 myJRebel

  1. 进入 https://my.jrebel.com/ 网页,按要求登陆并获取激活用的授权 license。
  2. 进入 https://zeroturnaround.com/software/jrebel/download/ 下载JRebel Standalone独立版本,也可以选择 IDE 插件安装。
  3. 根据 https://my.jrebel.com/account/how-to-activate 提示激活myJRebel

安装 myJRebel

  1. 解压文件到${HOME}/.jrebel目录。
  2. 复制上面得到的 license 并保存为文件:${HOME}/.jrebel/license.key
  3. 根据${HOME}/.jrebel/readme.txt说明,在${HOME}/.jrebel目录下执行以下命令。
bin/activate.sh license.key
2018-04-05 15:56:35 JRebel: Contacting myJRebel server ..
JRebel successfully activated!
License type: myJRebel
Licensee name: test

配置 myJRebel

简单配置 maven 的全局环境变量MAVEN_OPTS,可以使所有的mvn命令都经过myJRebel代理,在 Mac OSX 的${HOME}/.bash_profile或者是${HOME}/.zshrc中加入以下配置:

export MAVEN_OPTS="-agentpath:$HOME/.jrebel/lib/libjrebel64.dylib"

或者是 Linux 下面的${HOME}/.bashrc或者是${HOME}/.zshrc中加入以下配置:

export MAVEN_OPTS="-agentpath:$HOME/.jrebel/lib/libjrebel64.so"

然后执行 maven 项目的mvn clean操作:

jrebel clean
2018-04-05 16:04:53 JRebel: Contacting myJRebel server ..
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building language-java 0.3.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ language-java ---
2018-04-05 16:04:55 JRebel: Starting logging to file: /Users/test/.jrebel/jrebel.log
2018-04-05 16:04:55 JRebel:
2018-04-05 16:04:55 JRebel: #############################################################
2018-04-05 16:04:55 JRebel:
2018-04-05 16:04:55 JRebel: JRebel Agent 2018.1.0 (201803221138)
2018-04-05 16:04:55 JRebel: (c) Copyright ZeroTurnaround AS, Estonia, Tartu.
2018-04-05 16:04:55 JRebel:
2018-04-05 16:04:55 JRebel: Over the last 1 days JRebel prevented
2018-04-05 16:04:55 JRebel: at least 10 redeploys/restarts saving you about 0.4 hours.
2018-04-05 16:04:55 JRebel:
2018-04-05 16:04:55 JRebel: Licensed to test (using myJRebel).
2018-04-05 16:04:55 JRebel:
2018-04-05 16:04:55 JRebel:
2018-04-05 16:04:55 JRebel: #############################################################
2018-04-05 16:04:55 JRebel:
[INFO] Deleting /github.com/maven-jrebel/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.543 s
[INFO] Finished at: 2018-04-05T16:04:55+08:00
[INFO] Final Memory: 24M/251M
[INFO] ------------------------------------------------------------------------

下面设置2个mvn命令别名,避免上述修改全局环境变量的方式:

Mac OSX

alias jrebel="MAVEN_OPTS='-agentpath:$HOME/.jrebel/lib/libjrebel64.dylib' mvn"
alias jdebug="MAVEN_OPTS='-agentpath:$HOME/.jrebel/lib/libjrebel64.dylib -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000' mvn"

Linux

alias jrebel="MAVEN_OPTS='-agentpath:$HOME/.jrebel/lib/libjrebel64.so' mvn"
alias jdebug="MAVEN_OPTS='-agentpath:$HOME/.jrebel/lib/libjrebel64.so -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000' mvn"

JRebel Maven 插件

<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.7</version>
<executions>
<execution>
<id>generate-rebel-xml</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>

rebel.xml 生成

一般是使用 IDE 插件会自动生成rebel.xml文件,监控项目中的classes目录,或者其他指定目录的变化,并将这些变化直接反映到当前运行的应用程序中,也可以使用这个 Maven 插件,手动生成并配置这个rebel.xml文件,Maven 项目中默认会生成这个文件到target/classes目录中,更多详细说明参考官方文档

mvn jrebel:generate -Drebel.generate.show=true

如果上述命令提示No plugin found for prefix 'jrebel' in the current project and in the plugin groups错误,可以输入完 Maven 的目标完整的信息,并显示生成的rebel.xml文件内容:

mvn org.zeroturnaround:jrebel-maven-plugin:generate -Drebel.generate.show=true

rebel.xml example

一般将rebel.xml文件放在 maven 项目的src/main/resources目录下。

<?xml version="1.0" encoding="UTF-8"?>
<!--
This is the JRebel configuration file. It maps the running application to your IDE workspace, enabling JRebel reloading for this project.
Refer to https://manuals.zeroturnaround.com/jrebel/standalone/config.html for more information.
-->
<application generated-by="intellij" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_1.xsd">
<classpath>
<dir name="/path/to/maven/project/target/classes">
</dir>
</classpath>
</application>

Spring Boot using JRebel

Spring Boot 1.x 版本中加入JRebel支持:

mvn spring-boot:run -Drun.jvmArguments="-agentpath:[/path/to/JRebel library]"

Spring Boot 2.x 版本中加入JRebel支持:

mvn spring-boot:run -Dspring.boot.run.jvmArguments="-agentpath:[/path/to/JRebel library]"

在上述mvn命令中将实际jrebel库路径,替换进去。

References

  1. https://my.jrebel.com/
  2. https://zeroturnaround.com/software/jrebel/download/
  3. https://my.jrebel.com/account/how-to-activate
  4. Application configuration using rebel.xml
  5. Using the JRebel Maven plugin