Senthil RS
Senthil RS

Reputation: 321

how to create jar for a particular java package using maven pom.xml

I am new to maven and can you please help me to provide a sample pom file on how to create a jar file belonging to a specific package for my spring project.

Below are my two packages

com.net.foo

com.org.bar

And i want to create the jar file for the package com.org.bar alone with name as org.jar

Could you please help on how to achieve this?

Thanks

Upvotes: 2

Views: 2590

Answers (2)

Nazaret K.
Nazaret K.

Reputation: 3559

Since you are new to maven I should tell you that each maven module (each pom.xml) should produce a single jar. Producing two jars from a single pom.xml is an anti-pattern. You might want to create a multi module maven.

In this case you create a parent pom.xml with

<packaging>pom</packaging>

and have it include the two maven projects

<modules>
    <module>foo</module>
    <module>bar</module>
</modules>

Now you can build both jars with a single build but each is its own maven project, which is the correct pattern.

Having said that if you really want to separate by packages I guess you could use this:

https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html

But you cannot simply repeat the maven-jar-plugin twice. You would have to create 2 profiles or 2 executions to force maven to run the jar plugin twice. The simplest is to create 2 executions. See the code below.

Now, regarding how to name your produced jar, you need to set the finalName configuration on the maven-jar-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <finalName>myJar</finalName>                   
    </configuration>
</plugin>

Putting it all together:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>

                <execution>
                    <id>foo</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <finalName>foo</finalName>
                        <includes>
                            <include>**/foo/*</include>
                        </includes>
                    </configuration>
                </execution>

                <execution>
                    <id>bar</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <finalName>bar</finalName>
                        <includes>
                            <include>**/bar/*</include>
                        </includes>
                    </configuration>
                </execution>

            </executions>
        </plugin>
    </plugins>
</build>

Note that if you follow this method the produced jar files are not automatically treated as project artifacts so you need to do additional work to install/deploy them as maven artifacts. I strongly recommend you go with the multi-module approach.

Upvotes: 2

java_maniac
java_maniac

Reputation: 101

You would need to make use of maven-jar-plugin.

Example Usage :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/bar/**</exclude>
                </excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 0

Related Questions