Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

maven - Creating a new phase

I am working on a project using Maven for which I need two new phases: 'analyze' and 'eval' (for different phases of data analysis). I've read all the docs I can find, and created versions of components.xml and lifecycle.xml that are as close as I can get to correct, but Maven refuses to run the new phases. (I should emphasize that I have no problem getting my plugins to work, and no problem binding them to the existing phases provided by the default lifecycle. My problem is that the new phases I create seem to be ignored by maven.) What do I need to do to get my new phases to work?

lifecycles.xml:

<lifecycles>
  <lifecycle>
    <id>lenskit</id>
    <phases>
      <phase>
        <id>analyze</id>
        <executions>
          <execution>
            <goals>
              <goal>greet</goal>
            </goals>
          </execution>
        </executions>
      </phase>
      <phase>
        <id>validate</id>
        <executions>
          <execution>
            <goals>
              <goal>greet</goal>
            </goals>
          </execution>
        </executions>
      </phase>
    </phases>
  </lifecycle>
</lifecycles>

components.xml:

<component-set>
  <components>
    <component>
      <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
      <role-hint>lenskit</role-hint>
      <implementation>
        org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
      </implementation>
      <configuration>
         <id>lenskit</id>
         <phases>
            <phase>get-data</phase>
            <phase>analyze</phase>
            <phase>eval</phase>
         </phases>
         <default-phases>
            <verify> org.apache.maven.plugins:maven-resources-plugin:resources </verify>
            <get-data> org.riedl:hello-lenskit-plugin:greet </get-data>
            <analyze> org.riedl:hello-lenskit-plugin:greet </analyze>
            <eval> org.riedl:hello-lenskit-plugin:greet </eval>
            <package> org.riedl:hello-lenskit-plugin:greet </package>
         </default-phases>
      </configuration>
    </component>
  </components>
</component-set>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One of the wizards on the Maven mailing list pointed me to a complete working example that does exactly what I wanted: it creates a custom lifecycle with phases named whatever you want, and lets you use that lifecycle from the maven command-line. The example is at:

https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-scm-publish-plugin

The key missing insight is that the components.xml file must have both a LifecycleMapping component and a Lifecycle component. The correct, working components.xml is below.

Note that you may only define an additional lifecycle (i.e. additional to the three default lifecycles: the build, clean and site lifecycles). The original lifecycles and their phases will always be present and you cannot include any phases with names that match an existing lifecycle in your new lifecycle, which is too bad, since it makes it more awkward to redefine a complete lifecycle for a project. Still, this is a nice step forward.

Also, remember that when you use the new lifecycle, you must mark it as an extension:

<extensions>true</extensions>

so the plugin is allowed to redefine the lifecycle. To indicate that your pom.xml should use the new lifecycle, include the lifecycle name as the "packaging" for your project.

<component-set>
  <components>
    <component>
      <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
      <role-hint>lenskit</role-hint>
      <implementation>
    org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
      </implementation>
    </component>
    <component>
      <role>org.apache.maven.lifecycle.Lifecycle</role>
      <implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
      <role-hint>lenskit</role-hint>
      <configuration>
     <id>lenskit</id>
     <phases>
        <phase>get-data</phase>
        <phase>analyze</phase>
        <phase>eval</phase>
     </phases>
     <default-phases>
        <get-data>org.riedl:hello-lenskit-plugin:greet</get-data>
        <analyze>org.riedl:hello-lenskit-plugin:greet</analyze>
        <eval>org.riedl:hello-lenskit-plugin:greet</eval>
     </default-phases>
      </configuration>
    </component>
  </components>
</component-set>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...