Leem.fin
Leem.fin

Reputation: 42652

How to include only one dependency project classes in final jar

I use Maven to build my MyProject. There are several other projects which are used as library projects for 'MyProject'.

In pom.xml of MyProject, I defined those library projects as dependencies of MyProject. One of the Library project is named "OneLibProject":

<dependency>
    <groupId>com.xxx.OneLibProject</groupId>
    <artifactId>OneLibProject</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <type>jar</type>
</dependency>
<dependency>
  ...
</dependency>

Under MyProject root path, after I run : maven clean install a MyProject.jar is generated. But those classes defined in library projects(dependencies) are not included in this MyProject.jar.

Each library project also have its own pom & can generate its own jar.

Now, what I want to do is I want to have my pom.xml in "MyProject" to be configured so that the generated MyProject.jar file contains the classes of OneLibProject and classes of MyProject. Other library project classes are not included.

How to achieve this?

Upvotes: 1

Views: 3483

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136112

You can use maven-assembly-plugin and configure which dependencies to include in jar in assembly descriptor's dependencySet section.

Assuming you have project y and z. Add this plugin to y's pom

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptor>src/main/assembly/assembly.xml</descriptor>
            </configuration>
        </plugin>

place this assembly.xml in scr/main/assembly

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <includes>
                <include>test:y</include>
                <include>test:z</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

run mvn install in z

run mvn package assembly:assembly in y

if all correct you will get y-0.0.1-SNAPSHOT-jar-with-dependencies.jar in target folder. It will contain y and x classes only.

Upvotes: 1

Related Questions