如何为Maven创建新的包装类型? [英] How do I create a new packaging type for Maven?

查看:123
本文介绍了如何为Maven创建新的包装类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用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?

推荐答案

按照你的描述做,用包装 jar 创建一个Maven项目(如所述此处,因为不会有mojo定义)。在src / main / resources / META-INF / plexus子文件夹中创建一个包含以下内容的components.xml(假设您希望打包类型为my-custom-type,如果您将其更改为foobar希望)。

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>

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

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>

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

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

这篇关于如何为Maven创建新的包装类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆