Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Maven Shade插件在运行集成测试时会导致类路径上出现重复的JAR_Java_Amazon Web Services_Maven_Maven Shade Plugin_Maven Failsafe Plugin - Fatal编程技术网

Java Maven Shade插件在运行集成测试时会导致类路径上出现重复的JAR

Java Maven Shade插件在运行集成测试时会导致类路径上出现重复的JAR,java,amazon-web-services,maven,maven-shade-plugin,maven-failsafe-plugin,Java,Amazon Web Services,Maven,Maven Shade Plugin,Maven Failsafe Plugin,我有一个项目,其中包括来自的S3依赖项,并构建了一个着色jar。在终端上运行集成测试时,我正在点击。问题是拦截器被添加了两次,因为类路径包含两个带有S3 jar的jar:一个在我的着色jar中,一个来自本地.m2存储库。不幸的是,我无法控制包含此问题的代码,因此我需要找到一个解决方法,直到问题得到解决 我已经用以下pom和测试类复制了该问题: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:

我有一个项目,其中包括来自的S3依赖项,并构建了一个着色jar。在终端上运行集成测试时,我正在点击。问题是拦截器被添加了两次,因为类路径包含两个带有S3 jar的jar:一个在我的着色jar中,一个来自本地.m2存储库。不幸的是,我无法控制包含此问题的代码,因此我需要找到一个解决方法,直到问题得到解决

我已经用以下pom和测试类复制了该问题:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stu</groupId>
    <artifactId>duplicate-jars-classpath</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <aws.sdk.version>2.5.62</aws.sdk.version>
        <junit.version>5.4.2</junit.version>
        <maven.failsafe.plugin.version>2.22.0</maven.failsafe.plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>${aws.sdk.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.failsafe.plugin.version}</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>run-tests</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>
如果我在IDE中运行测试,则这是输出:

********** AWS execution interceptors:
jar:file:/Users/<name>/.m2/repository/software/amazon/awssdk/s3/2.5.48/s3-2.5.48.jar!/software/amazon/awssdk/services/s3/execution.interceptors
如您所见,终端已找到两个与路径匹配的资源


我能做些什么来避免这种情况吗?我的配置有问题吗?

这是由于您的maven配置,
shade
插件将所有依赖项打包到一个jar文件中

您的
failsafe
插件使用项目依赖项(
.m2/…
)和单个jar文件中的类路径运行测试,因此使用重复的资源

不过,这似乎只有在命令行中使用故障保护时才会发生。而且这很容易解决,您可以简单地告诉
故障保护
不要加载该依赖项。(无论如何,它都可以在单个jar文件中使用)


org.apache.maven.plugins
maven故障保护插件
${maven.failsafe.plugin.version}
**/*Test.java
software.amazon.awssdk:s3
运行测试
集成测试
集成测试
验证

这是由于您的maven配置,
shade
插件将所有依赖项打包到一个jar文件中

您的
failsafe
插件使用项目依赖项(
.m2/…
)和单个jar文件中的类路径运行测试,因此使用重复的资源

不过,这似乎只有在命令行中使用故障保护时才会发生。而且这很容易解决,您可以简单地告诉
故障保护
不要加载该依赖项。(无论如何,它都可以在单个jar文件中使用)


org.apache.maven.plugins
maven故障保护插件
${maven.failsafe.plugin.version}
**/*Test.java
software.amazon.awssdk:s3
运行测试
集成测试
集成测试
验证

谢谢!我更进一步,添加了
runtime
的范围排除,以排除所有编译和运行时依赖项。因为我有一个带有所有依赖项的着色罐子,这是完全可以接受的。这是配置
运行时
,请注意,如果使用此配置,IntelliJ还将排除运行时范围的依赖项。但是,IntelliJ不会在着色JAR上运行测试,因此现在您将错过所有运行时依赖项,IntelliJ将因NoClassDefFoundError而失败。谢谢!我更进一步,添加了
runtime
的范围排除,以排除所有编译和运行时依赖项。因为我有一个带有所有依赖项的着色罐子,这是完全可以接受的。这是配置
运行时
,请注意,如果使用此配置,IntelliJ还将排除运行时范围的依赖项。但是,IntelliJ不会在着色JAR上运行测试,因此现在您将错过所有运行时依赖项,IntelliJ将因NoClassDefFoundError而失败。
********** AWS execution interceptors:
jar:file:/Users/<name>/.m2/repository/software/amazon/awssdk/s3/2.5.48/s3-2.5.48.jar!/software/amazon/awssdk/services/s3/execution.interceptors
[INFO] Running com.stu.S3DuplicateJarTest
********** AWS execution interceptors:
jar:file:/Users/<name>/Development/duplicate-jars-classpath/target/duplicate-jars-classpath-1.0-SNAPSHOT.jar!/software/amazon/awssdk/services/s3/execution.interceptors
jar:file:/Users/<name>/.m2/repository/software/amazon/awssdk/s3/2.5.62/s3-2.5.62.jar!/software/amazon/awssdk/services/s3/execution.interceptors