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

发布于 2024-08-04 23:57:11 字数 122 浏览 3 评论 0原文

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

我可以设置新的包装类型来执行此操作吗?

I have a requirement to create jar files with Maven, but they need to be installed to the repository with a "foobar" extension , and it would be nice if they could have their own packaging type so we can identify those artifacts by the packaging.

Can I set up a new packaging type to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一江春梦 2024-08-11 23:57:11

要按照您所描述的方式进行操作,请创建一个带有打包 jar 的 Maven 项目(如所述 此处,因为不会有 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 会将文件传递到具有“.foobar”扩展名的存储库,如在 elements.xml 中指定的那样

To do as you described, create a Maven project with packaging jar (as stated here, as there won't be mojo definitions). In the src/main/resources/META-INF/plexus sub-folder create a components.xml with the following contents (assuming you want the packaging type to be "my-custom-type", change it to "foobar" if you wish).

<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>

Then in a pom that is to have the custom packaging, declare the required type in the packaging element, and ensure you have specified the plugin so the custom packaging can be contributed. Declaring <extensions>true</extensions> tells Maven that the plugin contributes packaging and/or type handlers to 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>

When the project is packaged, it will be a jar, with a .jar extension, however when it is installed/deployed, Maven will deliver the file to the repository with the ".foobar" extension as specified in components.xml

避讳 2024-08-11 23:57:11

跟进 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 mojoDefinitions.. 错误:

<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>

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

maven-plugin 打包类型生命周期将 plugin:descriptor 目标绑定到 generate-resources 阶段。这是在Sonatype 官方文档。

Following up on Rich Seller's original answer:

If as he recommends you use a packaging type jar then most likely in the project where you reference your plugin you'll receive:

[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.

This is because no plugin descriptor exists in the JAR you generated.

You can use the following to bypass the No mojo definitions.. error he mentions:

<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>

This configuration was found in plugin docs example here.

The maven-plugin packaging type lifecycle has the plugin:descriptor goal bound to generate-resources phase. This is specified in Sonatype's official documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文