我正在使用 Maven Shade 插件将我的应用程序打包成 jar 文件。我的依赖之一是 Tomcat:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.59</version>
<scope>provided</scope>
</dependency>
此依赖项的范围是 provided,因为容器本身将提供其 JAR 文件。但是,我确实需要将此依赖项中的一些单个类添加到我的 JAR 文件中。我尝试添加一个过滤器并指定要添加的类的名称,但似乎 provided 依赖项被阴影插件忽略了。
<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>
有什么想法可以实现我所需要的吗?
我遇到了同样的问题,如果我可以建议一个解决方案,那就是使用 maven-assemble-plugin 这将解决您的问题:
<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>
这是 assembly.xml 的示例,它将允许您包含“提供的”范围工件:
<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>
我只是在清单文件中生成的工件中遇到类路径问题!
您需要的配置设置是
<useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
。这将创建一个名为 dependency-reduced-pom.xml
的文件,并删除对阴影 jar 的引用。如果阴影 jar 有自己的传递依赖项,您还可以使用 <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
将它们添加到修改后的 pom 中。
使用单独的模块解决了相同的问题
...
<artifactId>subset-of-tomcat-catalina-classes</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.59</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
...
然后在主模块中:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.59</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId></groupId>
<artifactId>subset-of-tomcat-catalina-classes</artifactId>
<version>${project.version}</version>
<!-- classes relocated => compile scope -->
</dependency>
我对这个插件不是很熟悉,但是寻找此类问题的解决方案的最佳位置是查看插件/目标文档。
我猜参数 keepDependencyWithProvidedScope 就是你要找的。
我也遇到了同样的问题,但我发现我们误解了这个插件的工作原理。如果某些依赖项被捆绑到阴影 jar 中,它们将从阴影 jar 中的 pom.xml 的依赖项中删除。所以你不应该改变任何东西,只需离开他们的范围运行时间。