【问题标题】:Maven - URI is non hierarchialMaven - URI 不是分层的
【发布时间】:2020-09-24 09:16:25
【问题描述】:

我正在尝试学习 Spring 和 Maven,但遇到了一些麻烦。

当我使用mvn clean install 从终端运行我的测试时,我收到了这个错误:java.lang.IllegalArgumentException: URI is not hierarchy。这是引发错误的代码块:

LocationWeatherRootResponseTest.class.getClassLoader().getResource("extent.xml")).toURI()

当我将上面的代码更改为以下时,我得到一个空指针异常。

LocationWeatherRootResponseTest.class.getClassLoader().getResourceAsStream("extent.xml")))

更新

当我将代码更改为以下时,我收到了一个新错误。

    File file = new File(WeatherTest.class.getClassLoader().getResource("extent.xml").getPath());        
Reporter.loadXMLConfig(file);

堆栈跟踪:

java.io.FileNotFoundException: file:/home/user/IdeaProjects/spring-cucumber-test-harness/common/target/common-1.0-SNAPSHOT.jar!/extent.xml (No such file or directory)

【问题讨论】:

  • 是的,我尝试过 .toExternalForm() 和 FileLocator.toFileURL 但这些方法都不起作用
  • 我运行了以下 sn-p 并通过了。 import org.junit.jupiter.api.Test; import java.net.URISyntaxException; import java.util.Objects; public class MyTest { @Test void test() throws URISyntaxException { Objects.requireNonNull(MyTest.class.getClassLoader().getResource("extent.xml")).toURI(); } }
  • 您是否尝试使用 mvn clean install 运行它 - 这对我来说是失败的,我可以右键单击并成功运行它。它只有在 Maven 中才会失败
  • 使用 mvn clean install 也可以。

标签: java spring maven


【解决方案1】:

我设法使用 maven-remote-resources-plugin 解决了这个问题。现在,当我在主服务器 POM 上运行 mvn clean install 时,框架从 e2e 运行。

在包含我要共享的资源的模块中,我在 POM 文件中添加了以下内容

<build>
    <plugins>
        <plugin>
            <artifactId>maven-remote-resources-plugin</artifactId>
            <version>1.7.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>bundle</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

在我要使用资源的模块中。我添加了以下内容

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-remote-resources-plugin</artifactId>
                    <version>1.7.0</version>
                    <configuration>
                        <resourceBundles>
                            <resourceBundle>{groupId}:{resource artifactId}:1.0-SNAPSHOT</resourceBundle>
                        </resourceBundles>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>process</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多