0

Im trying learn Spring and Maven but im having some trouble.

When I go to run my tests from the terminal using mvn clean install I'm getting this error: java.lang.IllegalArgumentException: URI is not hierarchical . This is the block of code that throws the error :

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

When I change the above code to the following Im getting a null pointer exception.

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

Update

When I change the code to the below Im getting a new error.

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

stacktrace :

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)
KahunaDub
  • 89
  • 1
  • 8
  • https://stackoverflow.com/questions/18055189/why-is-my-uri-not-hierarchical https://stackoverflow.com/questions/10144210/java-jar-file-use-resource-errors-uri-is-not-hierarchical Did you look at these? – gtiwari333 Sep 23 '20 at 01:06
  • yeah ive tried .toExternalForm() and FileLocator.toFileURL but neither of these approaches worked – KahunaDub Sep 23 '20 at 01:12
  • I ran the following snippet and it passed. ``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(); } }`` – gtiwari333 Sep 23 '20 at 01:39
  • did you try to run it with mvn clean install - thats where its failing for me, I can right click and run it successfully. its only with maven it fails – KahunaDub Sep 23 '20 at 01:51
  • its fine with mvn clean install as well. – gtiwari333 Sep 23 '20 at 02:04
  • Which version of maven you have? – gtiwari333 Sep 23 '20 at 02:05
  • Apache Maven 3.6.3 Maven home: /usr/share/maven Java version: 11.0.8, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64 – KahunaDub Sep 23 '20 at 14:25
  • I just noticed maven is using the Open JDK and not the same JDK as the IDE. Could this be part of the problem? – KahunaDub Sep 23 '20 at 14:27
  • I fixed the open JDK issue, the URI error is still present tho – KahunaDub Sep 23 '20 at 15:42
  • I did not anticipate issues due to JDK. It might be Maven or the surefire plugin that runs the tests. Can you try updating the Maven and use the latest version of the surefire plugin? – gtiwari333 Sep 23 '20 at 17:30
  • I appreciate all your help. I added the surefire plugin to the framework. Maven is already on the latest version. When I run the code with the old implementation Im getting the URI error. When I run the code with the updated version im getting a file not found – KahunaDub Sep 23 '20 at 17:55

1 Answers1

0

I managed to solve this issue using maven-remote-resources-plugin . Now when I run mvn clean install on the master POM the framework runs from e2e.

In the module containing the resources I wanted to share, I added the following to the POM file

<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>

In the module where I want to use the resource. I added the following

<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>
KahunaDub
  • 89
  • 1
  • 8