I can't seem to build a 'solution' that works with the resources in my project. When I run my test during build, it can naturally access the resources, but after build the test can't find the file. How do I adjust my code so it works in both cases, or adjust my project. I'm interested to know the fundamentals on Java project setup with respect to this, in addition to my particular situation using the maven-shade plugin, time allowing.
I have tried various path variations with no luck
I have tried getClass().ClassLoader this and that with no luck.
My pom.xml
<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>company.myproj</groupId>
<artifactId>myproj</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-beta3</version>
<!-- <scope>provided</scope> -->
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
<!-- <scope>provided</scope> -->
</dependency>
</dependencies>
<build>
<!-- Source directory configuration -->
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- // Following plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- // Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testtest.xml</suiteXmlFile>
</suiteXmlFiles>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes><exclude>META-INF/versions/**</exclude></excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.testng.TestNG</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
My java
package myproj;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.testng.annotations.Test;
public class TestTest{
@Test(groups = { "basic1" })
public void Test05BasicPASS() throws URISyntaxException, IOException {
Path filePath = Paths.get(getClass().getResource("/images/hydrant.jpg").toURI());
System.out.println("My Path is: "+filePath.toString());
System.out.println("This is test 5, Basic Pass");
}
}
My testNg TestTest.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteB1" parallel="false">
<test name="TestTest">
<groups>
<run>
<include name="basic1"/>
</run>
</groups>
<classes>
<class name="myproj.TestTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
My directory structure:
src
-myproj
TestTest.java
-resources
-images
hydrant.jpg
TestTest.xml
pom.xml
when I run: mvn package -DskipTests=false the test runs and can access the image file.
after build, when I run on command line(builds to 'target' dir: java -jar C:\Dev\testproj\target\myproj-0.0.1.jar TestTest.xml
I get in test results FileSystemNotFoundException on the line where the file is being accessed.