Add maven-build-classpath to plugin execution classpath

后端 未结 3 1592
天命终不由人
天命终不由人 2020-12-02 11:31

I am writing some code-gen maven-plugin.

I need my project classpath be injected in to my plugin execution classpath.

I found this article. The solution ther

3条回答
  •  甜味超标
    2020-12-02 12:07

    Found the answer!

    OK , Pascal is right , here it is for the foundation!!

    So here is the cleanest way ( as far as i know ) to add the compile classpath to the execution of you plugin.

    Here are some code samples from my code-gen plugin, that is actually generating some template code based on the code compiled. So I needed first the code compiled, then analyzed, generate some code, and then compiled again.

    1. Use @configurator in the Mojo class:

      /**
       * @goal generate
       * @phase process-classes
       * @configurator include-project-dependencies
       * @requiresDependencyResolution compile+runtime
       */
      public class CodeGenMojo
              extends AbstractMojo
      {
          public void execute()
                  throws MojoExecutionException
          {
               // do work....   
          }
      }
      

      Please pay attention to the @configurator line in the javadoc header, it is essetial for the plexus IOC container and is not just another comment line.

    2. The implementation of the include-project-dependencies configurator. There is this very nice class that I took from some Brian Jackson, add it to the source of your plugin.

      /**
       * A custom ComponentConfigurator which adds the project's runtime classpath elements
       * to the
       *
       * @author Brian Jackson
       * @since Aug 1, 2008 3:04:17 PM
       *
       * @plexus.component role="org.codehaus.plexus.component.configurator.ComponentConfigurator"
       *                   role-hint="include-project-dependencies"
       * @plexus.requirement role="org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup"
       *                   role-hint="default"
       */
      public class IncludeProjectDependenciesComponentConfigurator extends AbstractComponentConfigurator { 
      
          private static final Logger LOGGER = Logger.getLogger(IncludeProjectDependenciesComponentConfigurator.class);
      
          public void configureComponent( Object component, PlexusConfiguration configuration,
                                          ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm,
                                          ConfigurationListener listener )
              throws ComponentConfigurationException {
      
              addProjectDependenciesToClassRealm(expressionEvaluator, containerRealm);
      
              converterLookup.registerConverter( new ClassRealmConverter( containerRealm ) );
      
              ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
      
              converter.processConfiguration( converterLookup, component, containerRealm.getClassLoader(), configuration,
                                              expressionEvaluator, listener );
          }
      
          private void addProjectDependenciesToClassRealm(ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm) throws ComponentConfigurationException {
              List runtimeClasspathElements;
              try {
                  //noinspection unchecked
                  runtimeClasspathElements = (List) expressionEvaluator.evaluate("${project.runtimeClasspathElements}");
              } catch (ExpressionEvaluationException e) {
                  throw new ComponentConfigurationException("There was a problem evaluating: ${project.runtimeClasspathElements}", e);
              }
      
              // Add the project dependencies to the ClassRealm
              final URL[] urls = buildURLs(runtimeClasspathElements);
              for (URL url : urls) {
                  containerRealm.addConstituent(url);
              }
          }
      
          private URL[] buildURLs(List runtimeClasspathElements) throws ComponentConfigurationException {
              // Add the projects classes and dependencies
              List urls = new ArrayList(runtimeClasspathElements.size());
              for (String element : runtimeClasspathElements) {
                  try {
                      final URL url = new File(element).toURI().toURL();
                      urls.add(url);
                      if (LOGGER.isDebugEnabled()) {
                          LOGGER.debug("Added to project class loader: " + url);
                      }
                  } catch (MalformedURLException e) {
                      throw new ComponentConfigurationException("Unable to access project dependency: " + element, e);
                  }
              }
      
              // Add the plugin's dependencies (so Trove stuff works if Trove isn't on
              return urls.toArray(new URL[urls.size()]);
          }
      
      }
      
    3. Here is the build part of my plugin that you will have to add.

      4.0.0
      com.delver
      reference-gen-plugin
      Reference Code Genration Maven Plugin
      
      maven-plugin
      1.2
      
      http://maven.apache.org
      
      
          2.2.1
      
      
      
          
              
                  org.codehaus.plexus
                  plexus-maven-plugin
                  
                      
                          
                              descriptor
                          
                      
                  
              
          
      
      
      
          
              org.apache.maven
              maven-artifact
              ${maven.version}
          
          
              org.apache.maven
              maven-plugin-api
              ${maven.version}
          
          
              org.apache.maven
              maven-project
              ${maven.version}
          
          
              org.apache.maven
              maven-model
              ${maven.version}
          
          
              org.apache.maven
              maven-core
              2.0.9
          
      
      
      

      Here is the pom.xml of the plugin for these who need it. Should compile wihtout a problem now. ( something wrong with the header, so ignore it )

提交回复
热议问题