如何为exec-war目标排除extraDependency META-INF文件



我的问题正是这个maven shade插件用户所面临的问题:

如何从捆绑包中排除META-INF文件?

但我正在使用tomcat7-maven插件来构建一个自运行的网络应用程序。我最近将数据库驱动程序切换为使用微软自己的驱动程序,该驱动程序实现了JDBC4。现在我遇到了一些问题,包括在我的高管战争目标中,它是一个extraDependency。以下是pom.xml的相关部分。

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/oases</path>
<contextFile>applicationContext.xml</contextFile>
<extraDependencies>
<extraDependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</extraDependency>
</extraDependencies>
<excludes>
<exclude>META-INF/MSFTSIG.RSA</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

该项目构建良好,只是maven不遵守exclude指令,因此sqljdbc4 RSA文件包含在META-INF目录中。这意味着当我试图运行exec-war-jar文件时,我会收到这个错误。

Exception in thread "main" java.lang.SecurityException: Invalid 
signature file digest for Manifest main attributes

我已经阅读了代码,据我所知,插件已正确配置为排除sqljdbc4 META-INF文件。这是2.2版本的插件代码,这就是我正在使用的。看起来这应该是我想要的。然而,exec-war jar仍然包含META-INF/MSFTSIG.RSA

org.apache.tomcat.maven.plugin.tomcat7.run.AbstractExecWarMojo.java

protected void extractJarToArchive( JarFile file, ArchiveOutputStream os, String[] excludes )
throws IOException
{
Enumeration<? extends JarEntry> entries = file.entries();
while ( entries.hasMoreElements() )
{
JarEntry j = entries.nextElement();
if ( excludes != null && excludes.length > 0 )
{
for ( String exclude : excludes )
{
if ( SelectorUtils.match( exclude, j.getName() ) )
{
continue;
}
}
}
if ( StringUtils.equalsIgnoreCase( j.getName(), "META-INF/MANIFEST.MF" ) )
{
continue;
}
os.putArchiveEntry( new JarArchiveEntry( j.getName() ) );
IOUtils.copy( file.getInputStream( j ), os );
os.closeArchiveEntry();
}
if ( file != null )
{
file.close();
}
}
}

编辑

  1. 由于此答案中指出的2.2版本错误,已恢复到插件2.1版本https://stackoverflow.com/a/23436438/980454
  2. 一种解决方法是创建依赖项jar的未签名版本

您为AbstractExecWarMojo发布的代码有一个错误:内部for循环中的continue没有任何影响。相反,它应该在外部while循环上继续,以便在exclude匹配时跳过放置归档条目,如下所示:

protected void extractJarToArchive( JarFile file, ArchiveOutputStream os, String[] excludes )
throws IOException
{
Enumeration<? extends JarEntry> entries = file.entries();
outer:
while ( entries.hasMoreElements() )
{
JarEntry j = entries.nextElement();
if ( excludes != null && excludes.length > 0 )
{
for ( String exclude : excludes )
{
if ( SelectorUtils.match( exclude, j.getName() ) )
{
continue outer;
}
}
}
if ( StringUtils.equalsIgnoreCase( j.getName(), "META-INF/MANIFEST.MF" ) )
{
continue;
}
os.putArchiveEntry( new JarArchiveEntry( j.getName() ) );
IOUtils.copy( file.getInputStream( j ), os );
os.closeArchiveEntry();
}
if ( file != null )
{
file.close();
}
}
}

为了解决项目中的这个问题,您可以从源代码签出/修改/构建tomcat7-maven插件。如果你这样做了,并且你成功地测试了它,如果你贡献了一个补丁,那就太好了。我已经为它提交了一个问题。

最新更新