I am having an issue running my cucumber project with device farm.
I am getting this error:
[TestNG] Caused by: java.lang.IllegalArgumentException: Not a file or directory: /tmp/scratchheDEgq.scratch/test-packagex5ZhYf/src/test/java/cucumber/features
I understand from this message that there is an issue with the path of the features directory in my project but locally it works.
This is how I put it in my code:
#CucumberOptions(features = "src/test/java/cucumber/features", glue = "cucumber.steps")
Should I provide a different path to device farm? What am I missing here?
Thanks
By default the src/test/java/cucmber directory(if it exists in the project) is not included the *-test.jar file. Try putting the feature files in the src/test/resources directory as shown in the Device Farm sample project.
https://github.com/aws-samples/aws-device-farm-appium-cucumber-tests-for-sample-app/tree/master/src/test/resources/LoginTest
Alternatively, you can implement the testResources tags in the pom.xml to explicitly reference another directory besides src/test/resources assuming that we're using maven:
<testResources>
<testResource>
<directory>${project.basedir}/src/test/YOUR_DIRECTORY_HERE</directory>
</testResource>
</testResources>
Check out this link for more info on the testResources tag
https://maven.apache.org/pom.html#Resources
testResources: The testResources element block contains testResource elements. Their definitions are similar to resource elements, but are naturally used during test phases. The one difference is that the default (Super POM defined) test resource directory for a project is ${basedir}/src/test/resources. Test resources are not deployed.
HTH
-James
Related
I'm trying to load a file using the classpath loader from within a Spring Boot application.
I'm just running it using Intellij.
It won't load, even though the file is in the resources folder.
This was due to the maven project configuration in the pom.xml file.
The fix was to include the file I wanted to reference in the build output.
Example pom.xml change:
<build>
<resources>
<includes>
<include>**/filename.ext</include>
</includes>
</resources>
</build>
The file then appears at: ./target/classes/filename.ext.
This then means I could reference the file as expected in my application configuration .yaml file as follows:
filename: classpath:filename.ext
Note: this answer was changed from the original.
I have 2 application.properties file in my small Spring Boot project. One is saved in /src/main/resources/application.properties and the other is in /src/test/resources/application.properties.
Some values in main doesn't necessarily have to be present in test.
Problem : When I do maven clean -> update project -> maven install in Spring Tool Suite IDE, I get multiple
Could not resolve placeholder 'JMS_URL' in value "${JMS_URL}"... and so on
But if I delete the application.properties file in test /src/test/resources/application.properties the error goes away and I get a successful maven install build.
I searched online and found that you need to specify the resources directory in pom.xml if you get that error.
Like so :
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
In my case, adding that <resource> block still did not resolve my problem because it's only specifying the directory of application.properties in src/main/resources but not for test
What is the correct way of handling this kind of situation where you have 2 application.properties file in main and test resource folder?
I'd appreciate any comment.
Thank you.
The correct way of handling this kind of situation is naming the test properties file differently, like application-test.properties. Then you either run your tests with the test profile active or explicitly specify
#TestPropertySource("classpath:application-test.properties")
on your #SpringBootTest.
Both ways you'll have all your properties from the main application.properties and application-test.properties active with properties from the latter having a higher priority.
Leaving the test properties file with the default name (application.properties) completely overrides the main properties file as you might have noticed. This way the test properties file has to have all the same required properties as the main one for the tests to work.
I have an application in a Java simple project. However, I need to paste this project to a Maven project. So, I basically made a simple Maven project, and I copied and pasted all my classes into it. I need a war to run in a server, and I need to run a Main like a Java application, because this application configures the war application. However, when I run Main, I get some errors that I wasn't having before:
java.io.FileNotFoundException: resources\config.properties (The system cannot find the path specified)
when in the code is:
input = new FileInputStream("resources/config.properties");
This didn't work either:
faceDetector = new CascadeClassifierDetector("D:/retinoblastoma/workspace/Resources/CascadeClassifiers/FaceDetection/haarcascade_frontalface_alt.xml");
How can I fix this?
In a simple Mavne project, all the resources should be located in src/main/resources.You can get the properties file then by (for a non-static method) :
Properties prop = new Properties();
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
For a static method, use :
<Class name>.class.getClassLoader().getResourceAsStream("config.properties");
Refer this link for more information :
Reading properties file
If you're using a "simple maven project", the maven way is for there to be a src/main/resources folder. Did you configure your pom.xml to do something different than that?
If you've done the jar creation portion correctly, the right way to get a file that's on the classpath is:
getClass().getResourceAsStream("/path/to/resource.ext");
The leading forward slash is important!
If the file is NOT on your classpath (in other words, this will be the case if the above doesn't work), you probably need to configure maven to use a different resources directory.
You do that like this (change the arguments as appropriate):
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
...
</build>
Then, your file will be on your classpath and the above code will work. Read the linked documentation above for more information.
I'm using Maven for desktop build application. I read about Maven standard directory layout and I have this project structure for now:
App
|-- pom.xml
`-- src
|-- main
|-- java
| `-- java classes
|-- resources
| `-- images
| `-- app images
`--config
`--config.xml
I want to find a way to load my resources and config files. I read some articles and topics and found this (simplified example from my code):
//class for loading config
public class Preferences {
public Preferences() {
InputStream image = Preferences.class.getResourceAsStream("images/image.png");
InputStream config = Preferences.class.getResourceAsStream("config.xml");
}
}
But image and config variables contains null. I was trying different variants of loading (from root folder, with this.getClass() instead of Preferences.class, and others), but it's always null. I really don't understand this resource loading system and I didn't find any good documentation about it. It would be nice, if somebody gives a good explanation about this mechanism (or just give a link on docs).
So, the main question is: How can I load my resources and config files?
You can use getResourceAsStream() method of java.lang.Class as you have done, but you have to add / before the path.
This question is tricky.
1. Two methods with same name
First of all, exist two methods of same name and same signature in these two classes:
java.lang.Class
java.lang.ClassLoader
They have the same name: getResource(String) (and getResourceAsStream(String) is alike).
2. They accept params of different format
Then, the param of them has different format:
The method java.lang.Class.getResouce<asStream>() accepts path with and without the leading /, resulting in different resources searching strategies. If a path has no /, Java will search the resource in the package/folder where the .class file resides. If it has /, Java will begin the searching from classpath root.
The method java.lang.ClassLoader.getResource<asStream>() accepts only path without /, because it always search from classpath. In a classpath based path, / is not a valid character. *
*: As this answer states: this.getClass().getClassLoader().getResource("...") and NullPointerException
How to add a folder to classpath? In Eclipse we resolve to the context menu of a project: "Build path" - "Configure build path..." and add some folder to build path.
3. When it comes to Maven
At last, if a project is a Maven project, by default src/main/resources is in the classpath, so we can use
Class.getResource("/path-to-your-res");
or,
ClassLoader.getResource("path-to-your-res");
, to load anything under src/main/resources.
If we want to add another resources folder, as you have mentioned, it is done in pom.xml. And they are added into classpath as well, done by Maven. No extra config is needed.
4. Example
For example, if your config.ini is under src/main/resources/settings, myAvatar.gif under src/main/images, you can do:
In pom.xml:
<build>
<resources>
<resource>
<directory>src/main/images</directory>
</resource>
</resources>
</build>
In code:
URL urlConfig = MyClass.class.getResource("/settings/config.ini"); //by default "src/main/resources/" is in classpath and no config needs to be changed.
InputStream inputAvatar = MyClass.class.getResourceAsStream("/myAvatar.gif"); //with changes in pom.xml now "src/main/images" is counted as resource folder, and added to classpath. So we use it directly.
We must use / above.
Or, with ClassLoader:
URL urlConfig = MyClass.class.getClassLoader().getResource("settings/config.ini"); //no leading "/"!!!
InputStream inputAvatar = MyClass.class.getClassLoader().getResourceAsStream("myAvatar.gif"); //no leading "/"!!!
I think I found the solution. As Juned Ahsan and mR_fr0g write, I need to use ClassLoader class, instead of this.getClass().getResource(). But, it works only for resource folder. But maven allows to add other folders as resource folders. I was just needed to add this section to pom.xml:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
</build>
And working java code is:
InputStream image = this.getClass().getClassLoader().getResourceAsStream("images/image.png");
InputStream config = ClassLoader.getSystemResourceAsStream("config.xml");
public Preferences() {
InputStream image = this.getClass().getClassLoader().getResourceAsStream("image.png");
InputStream config = this.getClass().getClassLoader().getResourceAsStream("config.xml")
}
How about using this appraoch:
InputStream file = ClassLoader.getSystemResourceAsStream("res.txt");
or
InputStream file = Thread.currentThread().getContextClassLoader().getResourceAsStream("MyProperty.properties");
As you currently have it there, that will look for the MyProperty.properties file at the top of your classpath. The could be in your src/main/resources directory or other src folder -- it would depend on how your application (jar/war) is built.
If you are building a jar then you should be able to unpack it and see your properties file at the top level of the jar. If you are building a war, maybe it should be in the WEB-INF/classes directory. Again, it depends on how it was built.
Try to build the project before.
If you just put the files in the resources folder then you need to build the project in order to copy the new resource files into target folder
It's an old answered question, but I found that you need to use the include tag set or it won't find the resource when you call.
ClassLoader.getSystemResource("config.xml").
I'm using the latest maven (Oct 2020) on JDK14
Here's the working xml. Hope it helps as I tried it as stated in the solution here and it would not work until my xml looked as follows.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<!-- or whatever other file extensions you want to include -->
</includes>
</resource>
</resources>
</build>
I have two different projects, A and B. Both projects load their own properties with:
new PropertiesConfiguration("application.properties")
Each application.properties is located in the Maven resource root directory.
When I execute Project B alone, all properties are loaded correctly.
Project A depends on Project B. When I execute Project A, the property class of Project B cannot load the correct properties, because the properties of Project A are loaded.
How can each Project load the correct properties even if they are included as a dependency. I have to specify to load the resources in different contexts, but I don't know how.
My maven settings for resources:
<resource>
<directory>${project.build.sourceDirectory}</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
You probably have some specific packages for Project B like
com.company.packageB
So put your application.properties in
src/main/resources/com/company/packageB/application.properties
And same for the Project A
You can iterate over all application.properties files like this
for (Enumeration<URL> urls = ClassLoader.getSystemResources("application.properties"); urls.hasMoreElements();) {
URL url = urls.nextElement();
}
Depending on your application you have to make a choice how to proceed if multiple property files are found. I hope this helps.