maven - 如何排除exec-war目标的extraDependency META-INF文件?

标签 maven maven-tomcat-plugin

我的问题恰恰是该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();
        }
    }
}

编辑
  • 由于此答案中指出的2.2版错误,已恢复为2.1版插件https://stackoverflow.com/a/23436438/980454
  • 一种解决方法是对依赖项jar的create an unsigned version
  • 最佳答案

    您为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/

    相关文章:

    java - Maven设置问题

    maven - Tomcat maven 插件 - run-war 和 run-war-only 有什么区别

    java - 嵌入式tomcat运行后如何运行main

    maven - Tomcat - Maven 插件 : run webapps but not current project

    java - 运行不同版本 Tomcat 的 Tomcat 7 Maven 插件

    java - 如何使用Maven项目在JAR中添加测试类

    java - 存在依赖关系时 Spring No Class Found 异常

    java - Eclipse 中的设置是否会传播到 Maven?

    Maven:不可解析的父 POM 和 'parent.relativePath' 指向错误的本地 POM

    java - 重启嵌入式tomcat