我的问题恰恰是该maven-shade插件用户面临的问题:
How to exclude META-INF files from bundle?
但是我正在使用tomcat7-maven-plugin来构建一个自运行的webapp。我最近将数据库驱动程序切换为使用Microsoft自己的实现JDBC4的驱动程序。现在我遇到了麻烦,将它作为我的exec-war目标中的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();
}
}
}
编辑
最佳答案
您为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();
}
}
}
要在您的项目中解决此问题,您可以从源代码中 checkout /修改/构建tomcat7-maven-plugin。如果这样做,并且您成功进行了测试,那么贡献补丁就好了。我已经为此提交了issue。
关于maven - 如何排除exec-war目标的extraDependency META-INF文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29686124/