我需要使用 Maven 创建 jar 文件,但它们需要安装到带有“foobar”扩展名的存储库中,如果它们可以有自己的打包类型那就太好了,这样我们就可以通过以下方式识别这些工件包装。
我可以设置新的包装类型来执行此操作吗?
最佳答案
按照您的描述,创建一个带有包装 jar 的 Maven 项目(如 here 所述,因为不会有 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 中,在包装元素中声明所需的类型,并确保您已指定插件以便可以贡献自定义包装。声明
<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”扩展名的存储库
关于java - 如何为 Maven 创建新的打包类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1427722/