code_junkie

How do you force a maven MOJO to be executed only once at the end of a build?

Nov 11th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. if (!getProject().isExecutionRoot()) {
  2. return ;
  3. }
  4.  
  5. /**
  6. * The projects in the reactor.
  7. *
  8. * @parameter expression="${reactorProjects}"
  9. * @readonly
  10. */
  11. private List reactorProjects;
  12.  
  13. public void execute() throws MojoExecutionException {
  14.  
  15. // only execute this mojo once, on the very last project in the reactor
  16. final int size = reactorProjects.size();
  17. MavenProject lastProject = (MavenProject) reactorProjects.get(size - 1);
  18. if (lastProject != getProject()) {
  19. return;
  20. }
  21. // do work
  22. ...
  23. }
  24.  
  25. boolean result = mavenSession.getExecutionRootDirectory().equalsIgnoreCase(basedir.toString());
  26.  
  27. /**
  28. * The Maven Project Object
  29. *
  30. * @parameter expression="${project}"
  31. * @required
  32. * @readonly
  33. */
  34. protected MavenProject project;
  35.  
  36.  
  37. /**
  38. * The Maven Session.
  39. *
  40. * @parameter expression="${session}"
  41. * @required
  42. * @readonly
  43. */
  44. protected MavenSession session;
  45.  
  46. ...
  47.  
  48.  
  49. @Override
  50. public void execute() throws MojoExecutionException, MojoFailureException
  51. {
  52. //Register the event handler right at the start only
  53. if (project.isExecutionRoot())
  54. registerEventMonitor();
  55. ...
  56. }
  57.  
  58.  
  59. /**
  60. * Register an {@link EventMonitor} with Maven so that we can respond to certain lifecycle events
  61. */
  62. protected void registerEventMonitor()
  63. {
  64. session.getEventDispatcher().addEventMonitor(
  65. new EventMonitor() {
  66.  
  67. @Override
  68. public void endEvent(String eventName, String target, long arg2) {
  69. if (eventName.equals("reactor-execute"))
  70. printSummary();
  71. }
  72.  
  73. @Override
  74. public void startEvent(String eventName, String target, long arg2) {}
  75.  
  76. @Override
  77. public void errorEvent(String eventName, String target, long arg2, Throwable arg3) {}
  78.  
  79.  
  80. }
  81. );
  82. }
  83.  
  84.  
  85. /**
  86. * Print summary at end
  87. */
  88. protected void printSummary()
  89. {
  90. ...
  91. }
Add Comment
Please, Sign In to add comment