Pete
Pete

Reputation: 187

Two Separate JARs with Maven

I want to use Maven to create two separate jars for my project.

One that will include everything under the **/client package and one that will include everything under the **/server package. The client package should also contain the sources.

Question 1: How can I setup my pom to produce these two separate jars?

Question 2: Is it possible to create both with a single maven command?

Any help is appreciated.

Thank you very much!

Upvotes: 2

Views: 4580

Answers (3)

Pete
Pete

Reputation: 187

After hours of searching, researching, banging my head against the desk, bleeding, and crying, I have solved this by using Maven's assembly plug-in.

Here is how it goes:

I created two separate assembly descriptors. Firstly, ${basedir}/src/main/assembly/client.xml (src/main/assembly is apparently the standard place for Maven assembly descriptors)

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>client</id>

    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/client/**</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/main/java</directory>
            <outputDirectory>/</outputDirectory>
            <useDefaultExcludes>false</useDefaultExcludes>
            <includes>
                <include>**/client/**</include>
            </includes>
        </fileSet>
    </fileSets>

</assembly>

then ${basedir}/src/main/assembly/server.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>server</id>

    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/server/**</include>
            </includes>
        </fileSet>
    </fileSets>

</assembly>

and added the following to my pom.xml:

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.2</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/client.xml</descriptor>
                    <descriptor>src/main/assembly/server.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

run mvn package... aaand voila!!! One command... two JARs!... so easy that I want to cry for all the time I spent trying to find the solution.

[INFO] --- maven-assembly-plugin:2.2.2:single (default) @ [my project] ---
[INFO] Reading assembly descriptor: src/main/assembly/client.xml
[INFO] Reading assembly descriptor: src/main/assembly/server.xml
[INFO] Building jar: [my project]-1.0-SNAPSHOT-client.jar
[INFO] Building jar: [my project]-1.0-SNAPSHOT-server.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.202s
[INFO] Finished at: Mon Dec 19 23:43:52 EET 2011
[INFO] Final Memory: 26M/245M
[INFO] ------------------------------------------------------------------------

Upvotes: 0

matsev
matsev

Reputation: 33789

My advice would be to refactor the projects into a separate client and a separate server project respectively. By adding a multi mudule (scroll down) project that contains both projects, it is possible to build the client and the server in one command.

Upvotes: 8

smp7d
smp7d

Reputation: 5032

You would want to use profiles to achieve this. Please see http://maven.apache.org/guides/mini/guide-building-for-different-environments.html

I am not sure if you can package both with a single command easily.

Upvotes: 0

Related Questions