如何为Maven创build新的包装types?

我有一个要求用Maven创buildjar文件,但是他们需要用一个“foobar”扩展名来安装到版本库中,如果他们有自己的包装types,那么我们可以通过包装来识别这些工件。

我可以设置一个新的包装types来做到这一点?

按照你所描述的,用包装jar创build一个Maven项目(如这里所述 ,因为不会有mojo定义)。 在src / main / resources / META-INF / plexus子文件夹中创build一个components.xml,其中包含以下内容(假设您希望打包types为“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中,在包装元素中声明所需的types,并确保已经指定了插件,以便自定义包装可以被贡献。 声明<extensions> true </ extensions>告诉Maven该插件为Maven提供了打包和/或types处理程序。

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

跟进Rich Seller的原始答案:

如果按照他的build议,使用包装types的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> 

这个configuration在这里的插件文档示例中find 。

maven-plugin包装types生命周期具有plugin:descriptor目标,绑定到generate-resources阶段。 这是在Sonatype的官方文档中指定的 。