5

I am using the maven shade plugin to package my application into a jar file. One of my dependencies is to 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 supply its JAR files. However, I do need to add a few single classes from this dependency to my JAR file. I tried adding a filter and specifying the name of the class that is to be added, but it seems that provided dependencies are ignored by the shade 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 how what I need can be achieved?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Dimitar
  • 134
  • 1
  • 6

4 Answers4

1

The configuration setting you need is <useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>. This will create a file named dependency-reduced-pom.xml with the references to the shaded jar(s) removed. You can also use <promoteTransitiveDependencies>true</promoteTransitiveDependencies> if the shaded jars have their own transitive dependencies to add them to the modified pom.

Jarett Millard
  • 5,802
  • 4
  • 41
  • 48
0

I had the same issue and If I can suggest a solution it would be to use the maven-assembly-plugin which will address 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 of the assembly.xml which will allow you include "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'am just having classpath issues in the generated artifact in the Manifest file!

-1

I'm not very familiar with this plugin, but the best place for finding solutions to these kinds of problems is to have a look at the plugin/ goal documentation.

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

Puce
  • 37,247
  • 13
  • 80
  • 152
  • Thanks for the hint. I did check the documentation, and I tried quite a few different settings and cases. None of them seemed to work, and there's even a bug report for that parameter stating it does not work. – Dimitar Apr 29 '15 at 11:46
-1

I had the same issue, but I found we misunderstood how this plug-in works. If some dependencies are bundled into the shaded jar, they will be removed from the dependencies in the pom.xml in the shaded jar. So you should change nothing, just left their scope runtime.

vipcxj
  • 840
  • 5
  • 10