Louisprrn
Louisprrn

Reputation: 17

How to generate a jar with maven containing specified classes from a project and specified classes from its dependencies?

I would like to generate a jar that contains only few classes from my project and one of its dependencies.

schema of what I would like to achieve

I currently managed to do that with a jardesc file containing the path of all the classes/packages I need since the dependency is also one of my projects.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jardesc>
    <jar path="C:/temp/myJar-4.0.0.jar"/>
    <options buildIfNeeded="true" compress="true" descriptionLocation="/ProjectB/src/main/resources/exportJar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="true" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
    <storedRefactorings deprecationInfo="true" structuralOnly="false"/>
    <selectedProjects/>
    <manifest generateManifest="true" manifestLocation="" manifestVersion="1.0" reuseManifest="false" saveManifest="false" usesManifest="true">
        <sealing sealJar="false">
            <packagesToSeal/>
            <packagesToUnSeal/>
        </sealing>
    </manifest>
    <selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
        <javaElement handleIdentifier="=projectA/src\/main\/java&lt;com.stckvrflw.parsingTools"/>
        <javaElement handleIdentifier="=projectA/src\/main\/java&lt;com.stckvrflw.utils{ClassA.java"/>
        <javaElement handleIdentifier="=projectB/src\/main\/java&lt;com.test.myPackage{ClassE.java"/>
        <javaElement handleIdentifier="=projectB/src\/main\/java&lt;com.test.myPackage{ClassF.java"/>
    </selectedElements>
</jardesc>

Is it possible to do that with Maven without duplicating classes ?

The jar needs to have it's own artifactId and version.

Upvotes: 0

Views: 262

Answers (1)

Sukhpal Singh
Sukhpal Singh

Reputation: 2288

You can use maven-shade-plugin and configure it like below:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
    <execution>
        <phase>package</phase>
        <goals>
        <goal>shade</goal>
        </goals>
        <configuration>
        <filters>
            <filter>
            <artifact>junit:junit</artifact>
            <includes>
                <include>junit/framework/**</include>
                <include>org/junit/**</include>
            </includes>
            <excludes>
                <exclude>org/junit/experimental/**</exclude>
                <exclude>org/junit/runners/**</exclude>
            </excludes>
            </filter>
            <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
            </filter>
        </filters>
        </configuration>
    </execution>
    </executions>
</plugin>

Source: maven-shade-plugin

Upvotes: 1

Related Questions