How do you use the maven shade plugin to only include certain classes from a "provided" dependency?

I am using the maven shade plugin to package my application into a jar file. One of my dependencies is for Tomcat:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>7.0.59</version>
    <scope>provided</scope>
</dependency>

      

The scope of this dependency is provided as the container itself will ship its JAR files. However, I need to add a few separate classes from this dependency to my JAR file. I tried adding a filter and specifying the name of the class to add, but it seems that the provided dependencies are being ignored by the shadow plugin.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>org.apache.tomcat:tomcat-catalina</artifact>
                                <includes>
                                    <include>org/apache/catalina/deploy/LoginConfig.class</include>
                                </includes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

      

Any ideas on how I need this can be achieved?

+3


source to share


2 answers


I had the same problem and if I can suggest a solution then the maven-assembly-plugin should be used , which will solve your problem:

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <executions>
                <execution>
                    <id>jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                        <archive>
                            <manifest>
                                <mainClass>com.mypackage.MainClass</mainClass>
                            </manifest>                             
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>

      

And here is a sample .xml collector that will allow you to include the "provided" scope artifacts:



<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>jar-with-dependencies</id>
<formats>
    <format>jar</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
    </dependencySet>
    <dependencySet>
        <outputDirectory>/lib</outputDirectory>
        <useProjectArtifact>false</useProjectArtifact>
        <unpack>false</unpack>
        <scope>provided</scope>
    </dependencySet>
</dependencySets>

      

I only have classpath problems in the generated artifact in the manifest file!

0


source


I'm not very familiar with this plugin, but the best place to look for solutions to these problems is to look at the plugin / target documentation .



I think the keepDependenciesWithProvidedScope parameter is what you are looking for.

-1


source







All Articles