简体   繁体   中英

How to use maven assembly plugin to exclude a package from dependency jar?

I'm using maven assembly plugin to package a distribution of my project that contains a lib folder with dependency jars, a config folder with resources and the jar file containing the project class files. I need to exclude a package from one of the dependency jars in the lib folder.

The assembly plugin has an option to unpack dependency jars, and if this is used then you can exclude a package with assembly.xml like so:

<assembly>
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>./${project.build.finalName}/lib</outputDirectory>
            <scope>runtime</scope>
            <unpackOptions>
                <excludes>
                    <exclude>**/excludedpackage/**<exclude>
                </excludes>
            </unpackOptions>
        </dependencySet>
    </dependencySets>
</assembly>

My question is, how can I exclude a package from a dependency jar without using unpack (ie keeping all dependencies packed as jars)? Ideally I'd like a solution that can be done using assembly plugin - if that's not possible what is the simplest way to achieve what I want to do?

I don't think you can repack the JAR after it has been unpacked and filtered. You could submit an enhancement-request at the Maven Assembly Plugin JIRA .


A (complicated) workaround would be to use the maven-dependency-plugin to unpack the project's dependency that you want to exclude something from, then use the maven-jar-plugin to jar the classes again excluding a package in a new JAR and finally declare a <files> element for the maven-assembly-plugin for that particular dependency.

A sample configuration would be

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>unpack</id>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
                <includeArtifactIds><!-- include here the dependency you want to exclude something from --></includeArtifactIds>
                <outputDirectory>${project.build.directory}/unpack/temp</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>repack</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
                <classesDirectory>${project.build.directory}/unpack/temp</classesDirectory>
                <excludes>
                    <exclude>**/excludedpackage/**</exclude>
                </excludes>
                <outputDirectory>${project.build.directory}/unpack</outputDirectory>
                <finalName>wonderful-library-repackaged</finalName> <!-- give a proper name here -->
            </configuration>
        </execution>
    </executions>
</plugin>

Then in your assembly configuration, you would have:

<files>
    <file>
        <source>${project.build.directory}/unpack/wonderful-library-repackaged.jar</source>
        <outputDirectory>/${project.build.finalName}/lib</outputDirectory>
    </file>
</files>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM