Thursday, May 5, 2011

How to get the name of goal in maven2's Mojo at runtime

For Maven2 how can I get the name of goal currently being executed in Mojo's execute method? Precisely I need value of @goal attribute inside Mojo's execute method.

From stackoverflow
  • public static String getGoalName(PluginDescriptor pluginDescriptor, String mojoClassName) {
         String goalName=null;
         List<MojoDescriptor> mojoDescriptorList = pluginDescriptor.getMojos();
         for (MojoDescriptor mojoDescriptor : mojoDescriptorList) {
          if (mojoDescriptor.getImplementation().equals(mojoClassName)) {
           goalName=mojoDescriptor.getGoal();
           break;
          }
         }
         return goalName;
        }
    

    Here, PluginDescriptor can be fetched from pluginManager.getPluginDescriptorForPrefix("prefix-for-your-plugin"). PluginManager is available as @component role="org.apache.maven.plugin.PluginManager"

0 comments:

Post a Comment