有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

我可以设置一种新的包装类型来实现这一点吗


共 (2) 个答案

  1. # 1 楼答案

    如您所述,创建一个带有打包jar的Maven项目(如上所述here,因为没有mojo定义)。在src/main/resources/META-INF/plexus子文件夹中创建一个组件。包含以下内容的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中,在packaging元素中声明所需的类型,并确保已指定插件,以便可以提供定制打包。声明<;扩展>;真的</扩展>;告诉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扩展时,Maven将使用组件中指定的“.foobar”扩展名将文件交付到存储库。xml

  2. # 2 楼答案

    跟进富商的原始答案:

    如果按照他的建议,你使用一种包装类型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目标绑定到generate-resources阶段。这是在Sonatype's official documentation中指定的