How to Create a New Packaging Type for Maven

How do I create a new packaging type for Maven?

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>

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>

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

How to do custom packaging in Maven?

Had found an answer on how to create .zip files. It seems that tweaking with the descriptor.xml file will give me the desired result. Following is my descriptor.xml file.

<assembly>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}/src/main/resources/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}/src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
</fileSets>
</assembly>

Using custom packaging type as dependency in maven

It seems that this sort of thing cannot be done in Maven.

I am solving my problem by adjusting the format of my custom package type to be a jar file itself, with all of the classes that I want on the classpath directly inside it. The other sub-resources that I don't require on the classpath (at least by default) are bundled inside the META-INF directory.

This is not ideal, but it is an acceptable solution for now.

Adding two packaging types in pom .xml

No a maven project will only support one type, as each project is specific and tailor made to what it needs to be or do. Typically a pom type project will not be a child project.

The reason for this is that pom projects describe the layout, dependencies and plugins of a project, in other words they are descriptor projects that layout the specifics of what is below - the children. They are normally the parents or some support project linking dependencies.

Then the children will be jars, wars, and bundles according to their specification.

I can imagine IntelliJ is getting confused over this and I think it will be necessary to see a cut down version of your poms to answer your question further. I'm not sure I've had a child project that was a POM project either and that doesn't seem right to me.

Does that help you?

How can i make packages in a maven project?

There is a confusion in your question. You need to make submodules, not packages.

On the screenshot:

  • multimod is a parent module.
  • dao and web are sub-modules.

pom.xml in the parent module will contain list of submodules:

<modules>
<module>dao</module>
<module>web</module>
<!-- other submodules -->
</modules>

Probably you will also need ear submodule to package dao and web.

Check this article: https://www.baeldung.com/maven-multi-module

Each of you modules should have different packaging type:

  • multimod - pom
  • dao - jar
  • web - war
  • ejb - ear

Each maven module can contain java code in src/main/java. There you can place the packages. E.g. directory src/main/java/a/b/c is the package a.b.c

How to specify packaging in a Maven profile?

You can try to include the following in your pom.xml

        <packaging>${packaging.type}</packaging> 

<profiles>
<profile>
<id>lambda</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>jar</packaging.type>
</properties>
</profile>
<profile>
<id>war</id>
<properties>
<packaging.type>war</packaging.type>
</properties>
</profile>
</profiles>

What is pom packaging in maven?

pom is basically a container of submodules, each submodule is represented by a subdirectory in the same directory as pom.xml with pom packaging.

Somewhere, nested within the project structure you will find artifacts (modules) with war packaging. Maven generally builds everything into /target subdirectories of each module. So after mvn install look into target subdirectory in a module with war packaging.

Of course:

$ find . -iname "*.war"

works equally well ;-).



Related Topics



Leave a reply



Submit