35

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

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

4

2 回答 2

47

按照您的描述,创建一个带有打包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 中,在包装元素中声明所需的类型,并确保您已指定插件以便可以贡献自定义包装。声明 <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>

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

于 2009-09-15T15:04:20.553 回答
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.

这是因为您生成的 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>

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

maven-plugin包装类型生命周期的目标plugin:descriptorgenerate-resources阶段性的。这是在Sonatype 的官方文档中指定的。

于 2012-12-12T10:53:41.857 回答