如何为Maven创建新的包装类型?

tal*_*ank 31 java maven-2 maven-plugin

我需要使用Maven创建jar文件,但是需要使用"foobar"扩展名将它们安装到存储库中,如果它们可以拥有自己的包装类型,那么我们就可以通过打包来识别这些工件.

我可以设置新的包装类型吗?

Ric*_*ler 44

要做到像你描述,创建包装Maven项目罐子(如说在这里,因为不会有魔力的定义).在src/main/resources/META-INF/plexus子文件夹中创建一个包含以下内容的components.xml(假设您希望打包类型为"my-custom-type",如果您将其更改为"foobar"希望).

<component-set>
  <components>
    <component>
      <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
      <role-hint>my-custom-type</role-hint>
      <implementation>
        org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
      </implementation>
      <configuration>
    <phases>
      <!--use the basic jar lifecycle bindings, add additional 
          executions in here if you want anything extra to be run-->          
      <process-resources>
        org.apache.maven.plugins:maven-resources-plugin:resources
      </process-resources>
      <package>
        org.apache.maven.plugins:maven-jar-plugin:jar
      </package>
      <install>
        org.apache.maven.plugins:maven-install-plugin:install
      </install>
      <deploy>
        org.apache.maven.plugins:maven-deploy-plugin:deploy
      </deploy>
    </phases>
      </configuration>
    </component>
    <component>
      <role>org.apache.maven.artifact.handler.ArtifactHandler</role>
      <role-hint>my-custom-type</role-hint>
      <implementation>
        org.apache.maven.artifact.handler.DefaultArtifactHandler
      </implementation>
      <configuration>
        <!--the extension used by Maven in the repository-->
        <extension>foobar</extension>
        <!--the type used when specifying dependencies etc.-->
        <type>my-custom-type</type>
        <!--the packaging used when declaring an implementation of 
          the packaging-->
        <packaging>my-custom-type</packaging>
      </configuration>
    </component>
  </components>
</component-set>
Run Code Online (Sandbox Code Playgroud)

然后在要使用自定义包装的pom中,在包装元素中声明所需的类型,并确保已指定插件,以便可以提供自定义包装.声明<extensions> true </ extensions>告诉Maven该插件为Maven提供打包和/或类型处理程序.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>name.seller.rich</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>my-custom-type</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>name.seller.rich.maven.plugins</groupId>
        <artifactId>maven-foobar-plugin</artifactId>
        <version>0.0.1</version>
        <!--declare that this plugin contributes the component extensions-->
        <extensions>true</extensions>
      </plugin>
    </plugins>
  </build> 
</project>
Run Code Online (Sandbox Code Playgroud)

打包项目时,它将是一个扩展名为.jar的jar,但是在安装/部署时,Maven会将文件传递到存储库,并使用components.xml中指定的".foobar"扩展名.

  • 如果使用包`maven-plugin`,你会得到`没有找到插件`消息的mojo定义,如[here](http://jira.codehaus.org/browse/MPLUGIN-109)所述.我不得不使用`jar`包. (2认同)

Kom*_*ave 5

跟进 Rich Seller 的原始回答:

如果他建议您使用打包类型,jar那么很可能在您引用插件的项目中,您将收到:

[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] The plugin descriptor for the plugin Plugin [com.ocado.mvn.packaging:Jar-Gem] was not found. Please verify that the plugin JAR /home/ndb/.m2/repository/com/ocado/mvn/packaging/Jar-Gem/1.0.0/Jar-Gem-1.0.0.jar is intact.
Run Code Online (Sandbox Code Playgroud)

这是因为您生成的 JAR 中不存在插件描述符。

您可以使用以下内容绕过No mojo definitions..他提到的错误:

[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] The plugin descriptor for the plugin Plugin [com.ocado.mvn.packaging:Jar-Gem] was not found. Please verify that the plugin JAR /home/ndb/.m2/repository/com/ocado/mvn/packaging/Jar-Gem/1.0.0/Jar-Gem-1.0.0.jar is intact.
Run Code Online (Sandbox Code Playgroud)

此配置在插件文档示例中找到

maven-plugin包装类型的生命周期有plugin:descriptor必然的目标generate-resources阶段。这在Sonatype 的官方文档中有详细说明

  • 在这种情况下使用 `jar` 包装是正确的。因为它没有魔力,所以您创建的是扩展程序,而不是插件。因此,您需要通过 POM 的 [`&lt;extensions&gt;`](http://maven.apache.org/pom.html#Extensions) 部分而不是 [`&lt;plugins&gt;`](http:/ /maven.apache.org/pom.html#Plugins) 部分。这样做将防止“找不到插件描述符”错误。 (2认同)