原文http://1.blog4dearshor.sinaapp.com/2011/11/03/%E5%AE%9E%E8%B7%B5%EF%BC%9A%E5%9C%A8-eclipse-based-ide-%E4%B8%AD%E4%BD%BF%E7%94%A8maven/
转载自:第二少 http://blog4dearshor.sinaapp.com/
引言
Introduction
有两种maven和eclipse(结合)使用的方式:
把 Eclipse 作为一个编辑器,(用于编辑POM),用 maven 命令行工具来执行 maven build,或者
使用 Eclipse plugin for Maven (参考 Eclipse Integration )
如果你觉得在命令行调用maven更舒服,但也希望得益于eclipse提供的便利功能(代码完成,重构,等等),那么应该使用第一种。
如果你想让eclipse来做所有这些事情 —— 从创建(project)到编辑(代码、pom)到执行(maven build),那么应该使用第二种。
在这里,我们将会讨论 Eclipse plugin for Maven。
There are two ways to use Maven and Eclipse:
1. Eclipse as the editor, and Maven command line for commands, or
2. Using an Eclipse plugin for Maven (see Eclipse Integration )
Use the first one if you’re much more comfortable with using the command line for your maven executions, but would like to take advantage of eclipse’ features ( code complete, refactoring, etc ). Use the second one if you want eclipse to handle all these things – creation to editing to execution.
For this page, we will discuss the Eclipse plugin for Maven
安装(环境)
Setting up
要完成环境的安装,你必须安装一个maven eclipse插件,参考 Eclipse Integration.
To do so, you must install an eclipse plugin for maven, see Eclipse Integration.
在eclipse里调试 Maven Project
Debugging your Maven Project in Eclipse
注意,maven有2种调试模式:Generic 和 Surefire. 如果你调试maven本身,maven插件,或一个maven工程,则使用Generic 途径。如果你想调试一个surefire plugin 启动的 test,则使用 Surefire 途径
Note that there are two debug modes in maven: the Generic and the Surefire. If you want to debug maven itself, one of maven’s plugins, or a maven project, use the Generic Approach. If you want to debug a test in your project launched by surefire, use the Surefire Approach.
Maven 2.0.8+ 的调试环境安装
Setting up Maven 2.0.8+
注意:如果你使用 Maven 2.0.8 或更高版本,只需简单的用 “mvnDebug” 命令来替代 “mvn” 命令(执行maven build即可),接着你可以直接跳到下面的eclipse的调试环境安装
Note: If you are using Maven 2.0.8 or newer, simply run the “mvnDebug” command in place of “mvn” and skip down to setting up Eclipse.
Setting up Maven 2.0.8-
Open %M2_HOME%/bin/mvn.bat
At line 30, you’ll find this,
@REM set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
Uncommenting the line by deleting @REM
Also add
set MAVEN_OPTS=
in the :end section, otherwise the debug will be turned on even when you run the normal mvn.bat
Save that file as mvn-debug.bat (or any convenient name you may want). You now have two maven batch files, one for normal use, and one for debugging.
Now when you want to debug maven, run mvn-debug instead of mvn (i.e. instead of mvn install, use mvn-debug install)
安装eclipse的调试环境
Setting up Eclipse
创建一个Java Project用户maven调试会话
Create a Java Project for the Maven Debug session
创建一个java project并命名为“Maven Debug”. 这个project里不会有任何源代码,它仅仅是一个“外壳”,用来连接上eclipse debugger的。
Create a new Java Project and call it “Maven Debug”. This Project will never have any source code in it, it is just a shell for attaching the debugger.
创建 远程Java应用程序 调试启动配置
(译者注:即“被调式的应用程序所在的jvm”与“调试器所在的jvm”不是同一个,而是两个独立的jvm)
Create Remote Java Application Debug Configurations
创建2个调试配置,一个用于 Generic 调试,命名为 “Maven” ,另一个用于 Surefire 调试,命名为 “Maven Surefire“:
Run > Debug... 然后右键单击Remote Java Application, 接着选择 New Launch Configuration.
在 Connect 标签点击 Browse... 按钮,然后选择 “Maven Debug” project. 确保 Connection Properties >Port 为 8000。(这和上面提到的 mvnDebug.bat/mvnDebug 文件里设置的 MAVEN_OPTS 命令行选项里的端口一致。)
在 Source 标签页点击 Add... 然后选择所有包含你想调试的源代码的 projects.
现在重复上述步骤,以创建第二个调试配置,命名为 “Maven Surefire” ,并指定监听端口为 5005.
Create two debug configurations, one for Generic debugging called “Maven” and one for Surefire debugging called “Maven Surefire”
Run > Debug... and then right click on Remote Java Application and chose New Launch Configuration.
On the Connect tab click the Browse... button and select the “Maven Debug” project. Make sure that theConnection Properties > Port is 8000. (This matches the address value set on the MAVEN_OPTS command line in the mvn-debug.bat file from above)
On the Source tab click Add... and select all projects that have any Maven source that you want to debug.
Now do the same thing to create a second Remote Java Application called “Maven Surefire” with a port of 5005.
Generic 调试
Generic Debugging
在你将要运行的代码中设置好断点
运行maven到调试模式,例如(执行以下命令):mvnDebug install
通过执行调试启动配置——“Maven”—— 连接上上面创建的调试器
然后eclipse将会在你设置的断点出“暂挂”。
译者注:用 mvnDebug 启动build后,当看到
Listening for transport dt_socket at address: 8000
时,会“暂挂”,直到你启动“maven调试配置”、连接到运行maven的jvm监听的8000端口时,才会“继续”;如果你启用了Surefire Debugging(下文会提到),那么接下来会看到
Listening for transport dt_socket at address: 5005
这时又会“暂挂”,直到你启动“maven surefire调试配置”、连接到运行maven的jvm监听的5005端口时,才会“继续”。
Select break points in the code you’re are going to run.
Run maven in debug mode, e.g mvn-debug install
Attach the debugger to the running maven by selecting the “Maven” debug configuration created above.
Eclipse will now stop Maven at the breakpoints you have enabled.
Surefire 调试
Surefire Debugging
在你将要运行的代码中设置好断点
在命令行执行的命令里追加一个选项:
-Dmaven.surefire.debug
例如,要调试被maven install生命周期启动的 test ,执行以下命令:
mvn install -Dmaven.surefire.debug
等待maven“暂挂”它的build过程并显示消息:
Listening for transport dt_socket at address: 5005
通过执行调试启动配置——“Maven Surefire”—— 连接上上面创建的调试器然后eclipse将会在你设置的断点出“暂挂”。
Select break points in the code you’re are going to run.
From your command line, append the following to your maven command.
-Dmaven.surefire.debug
For example, to debug the tests run by the maven lifecycle install, do mvn install -Dmaven.surefire.debugWait for maven to pause its execution and display the message,
Listening for transport dt_socket at address: 5005
Attach the debugger to the running maven by selecting the “Maven Surefire” debug configuration created above.