简体   繁体   English

使用Maven排除文件不起作用

[英]Excluding files with Maven doesn't work

I run into problems at Maven compile when using JMockit together with JUnit and Android: 将JMockit与JUnit和Android一起使用时,在Maven编译时遇到问题:

[INFO] UNEXPECTED TOP-LEVEL EXCEPTION: [INFO] java.lang.IllegalArgumentException: already added: Ljunit/framework/TestResult$1; [INFO]超出预期的最高级别:[INFO] java.lang.IllegalArgumentException:已添加:Ljunit / framework / TestResult $ 1;

At least JUnit and JMockit contain TestResult, so I thought about using the shade plugin to exclude these files: 至少JUnit和JMockit包含TestResult,所以我考虑使用shade插件排除这些文件:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>com.googlecode.jmockit:jmockit</artifact>
                            <excludes>
                                <exclude>junit/framework/**</exclude>
                            </excludes>
                        </filter>
                        <filter>
                            <artifact>junit:junit</artifact>
                            <excludes>
                                <exclude>junit/framework/**</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <minimizeJar>true</minimizeJar>
                </configuration>
            </execution>
        </executions>
    </plugin>

Unfortunately I still get these errors. 不幸的是我仍然遇到这些错误。 Any ideas what's wrong here or did I misunderstand what this plugin is about? 任何想法在这里出什么问题还是我误解了该插件的含义? Or maybe you know a better solution to get rid of multiple package clashes with Maven? 或者,也许您知道一个更好的解决方案来摆脱与Maven的多个程序包冲突?

UPDATE 更新

(Meanwhile I unpacked and removed the conflicting dependencies manually from the jars and repacked all packages into an uber jar and installed it into the local repository. This works. But I still would be interested in a more professional solution) (与此同时,我手动解压缩了相互冲突的依赖关系,并将其从jar中删除,并将所有程序包重新打包到uber jar中,然后将其安装到本地存储库中。这可行。但是我仍然会对更专业的解决方案感兴趣)

As far as I know there is no good way to solve your issue. 据我所知,没有解决您问题的好方法。 Normally in Maven you would exclude the problematic transitive dependency on Junit from the JMockit framework. 通常,在Maven中,您将从JMockit框架中排除对Junit的问题传递依赖。 However JMockit has the same problem some maven thirdparty jars have. 但是,JMockit在某些maven第三方jar中也存在相同的问题。 They have included the Junit dependency in the JMockit jar instead of declaring a dependency in the pom of JMockit. 他们在JMockit jar中包含Junit依赖关系,而不是在JMockit的pom中声明依赖关系。 See exclude dependencies how you could solve this with maven when JMockit would be packaged more maven friendly. 请参阅排除依赖项 ,当将JMockit打包为对maven更友好时,如何使用maven解决此问题。

But there is no way to specify that you want to remove some classes from the JMockit dependency with the maven dependency mechanism. 但是没有办法指定您要使用maven依赖机制从JMockit依赖中删除某些类。

So you already choose the correct way of creating a correct jar file and putting it in your repository. 因此,您已经选择了创建正确的jar文件并将其放入存储库的正确方法。 The only thing could be better would be to not do it manually. 唯一可以做的更好的办法是不手动执行此操作。 Create a new maven module creating this for you and use it as dependecy in your android project. 创建一个新的maven模块,为您创建该模块,并在您的android项目中将其用作依赖项。

The new module would only depend on Junit and JMockit and no source code and would have the following shade plugin conf: 新模块将仅依赖于Junit和JMockit,并且没有源代码,并且具有以下阴影插件conf:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Since this module would be jar module and not compiled with Android it did not have any problems of the duplicate class and the shade plugin will only package one TestResult class. 由于此模块将是jar模块,并且未使用Android进行编译,因此它没有重复类的任何问题,shadow插件将仅打包一个TestResult类。 If you want to ensure the correct one is packaged you can still use filtering to exclude the JMockit classes and rely on the JUnit once. 如果您想确保打包了正确的类,则仍然可以使用过滤来排除JMockit类,并仅依赖JUnit。

With an extra module you ensure the process is documented as pom file and you could easily switch to a newer JUnit or JMockit version. 使用额外的模块,可以确保将过程记录为pom文件,并且可以轻松切换至更新的JUnit或JMockit版本。 Also you would not rely on a file installed in your local repository which could be deleted by accident or is missing on an other pc. 同样,您不会依赖于本地存储库中安装的文件,该文件可能会被意外删除或在另一台PC上丢失。

Not sure what you mean, that you are getting an error during maven compile? 不确定您的意思是说,您在进行Maven编译时遇到错误吗?

Anyways, I believe that you should not at all package your JUnit dependencies to the "uber-jar", and you should only use them in maven test scope: 无论如何,我相信您根本不应该将JUnit依赖项打包到“ uber-jar”中,而只能在maven测试范围中使用它们:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>${junit.vesrion}</version>
  <scope>test</scope>
<dependency>
<dependency>
  <groupId>com.googlecode.jmockit</groupId>
  <artifactId>jmockit</artifactId>
  <version>${jmockit.vesrion}</version>
  <scope>test</scope>
<dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM