【问题标题】:How do I create a new packaging type for Maven?如何为 Maven 创建新的打包类型?
【发布时间】:2009-09-15 15:00:21
【问题描述】:

我需要使用 Maven 创建 jar 文件,但它们需要安装到具有“foobar”扩展名的存储库中,如果它们可以有自己的打包类型那就太好了,这样我们就可以通过以下方式识别这些工件包装。

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

【问题讨论】:

    标签: java maven-2 maven-plugin


    【解决方案1】:

    按照您的描述,创建一个带有包装 jar 的 Maven 项目(如 here 所述,因为不会有 mojo 定义)。在 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>
    

    然后在要进行自定义包装的 pom 中,在包装元素中声明所需的类型,并确保您已指定插件以便可以贡献自定义包装。声明 true 告诉 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>
    

    当项目被打包时,它会是一个 jar,扩展名为 .jar,但是当它被安装/部署时,Maven 会将文件传递到带有 components.xml 中指定的“.foobar”扩展名的存储库

    【讨论】:

    • 有没有办法在项目中创建带有 foobar 扩展名的文件?
    • 你是指MET-INF还是META-INF?
    • 我认为要做到这一点,您必须用自定义实现替换 jar 归档器,或者使用生命周期.xml 将程序集插件调用绑定到阶段(根据您的其他问题:stackoverflow.com/questions/1427740/…)
    • 好的,谢谢,我想这可能就足够了。我会检查并回复你
    • 如果您使用包maven-plugin,您将收到No mojo definitions were found for plugin 消息,如here 所述。我不得不改用jar 包。
    【解决方案2】:

    跟进 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.
    

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

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

    <packaging>maven-plugin</packaging>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

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

    maven-plugin 包装类型生命周期将plugin:descriptor 目标绑定到generate-resources 阶段。这是在Sonatype's official documentation 中指定的。

    【讨论】:

    • 在这种情况下使用jar包装是正确的。因为它没有 mojos,所以您创建的是扩展,而不是插件。因此,您需要通过 POM 的 &lt;extensions&gt; 部分而不是 &lt;plugins&gt; 部分来加载它。这样做可以防止“找不到插件描述符”错误。
    • @SamHanes 感谢您的更正。我已经从我的帖子中删除了相关的“..should use JAR..”。
    猜你喜欢
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2020-02-15
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多