Summary
My JUnit tests are not finding the files they require during execution.
I'm using Maven for dependency management and compilation.
Details
All files required by the test cases are located in: src/test/resources.
For example, src/test/resources/resourceFile.txt.
To access a resource I use the following code:
URL url = getClass().getResource("/resourceFile.txt").getFile();
File file = new File(url);
But then file.exists() returns false. And the error I get is:
Tests in error:
myJUnitTestCase(tests.MyJUnitTestClass): /home/me/workspace/Project%20Name/target/test-classes/resourceFile.txt (No such file or directory)
Note, the following gives the same error (notice the removed / prefix):
URL url = getClass().getClassLoader().getResource("resourceFile.txt").getFile();
File file = new File(url);
It seems as though the files from src/test/resources are not getting copied into target/test-classes.
Any ideas?
The following questions did not help
Why Can't I access src/test/resources in Junit test run with Maven?
Loading Properties File In JUnit #BeforeClass
How to deal with the test data in Junit?
Software Versions
Ubuntu 12.04
Apache Maven 2.2.1
Java 1.7.0
Eclipse (Java EE IDE for Web Developers) Indigo Service Release 2
(truncated) Maven POM
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.groupId</groupId>
<artifactId>artifactId</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>name</name>
<build>
<finalName>name</finalName>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
My mistake, the resource files WERE actually copied to target/test-classes. The problem seemed to be due to spaces in my project name, e.g. Project%20Name.
I'm now loading the file as follows and it works:
org.apache.commons.io.FileUtils.toFile(myClass().getResource("resourceFile.txt"));
Or, (taken from Java: how to get a File from an escaped URL?) this may be better (no dependency on Apache Commons):
myClass().getResource("resourceFile.txt").toURI();
You know that Maven is based on the Convention over Configuration pardigm? so you shouldn't configure things which are the defaults.
All that stuff represents the default in Maven. So best practice is don't define it it's already done.
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
This is actually redundant except in cases where you want to override the defaults. All of these settings are implied defaults.
You can verify that by checking your effective POM using this command
mvn help:effective-pom
<finalName>name</finalName>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
For example, if i want to point to a different test resource path or resource path you should use this otherwise you don't.
<resources>
<resource>
<directory>/home/josh/desktop/app_resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>/home/josh/desktop/test_resources</directory>
</testResource>
</testResources>
You may have defined:
<packaging>pom</packaging>
If you did this, the resources won't be present in the target directory when you will launch your tests. And mvn package won't create it either.
At the contrary, if you define:
<packaging>jar</packaging>
Or nothing as the default value is jar. As Maven is based on Convention over Configuration. You will end up with:
src/main/resources => target/classes
src/test/resources => target/test-classes
Main classes should be under src/main/java
and
test classes should be under src/test/java
If all in the correct places and still main classes are not accessible then
Right click project => Maven => Update Project
Hope so this will resolve the issue
The test Resource files(src/test/resources) are loaded to target/test-classes sub folder. So we can use the below code to load the test resource files.
String resource = "sample.txt";
File file = new File(getClass().getClassLoader().getResource(resource).getFile());
System.out.println(file.getAbsolutePath());
Note : Here the sample.txt file should be placed under src/test/resources folder.
For more details refer options_to_load_test_resources
Make 'maven.test.skip' as false in pom file, while building project test reource will come under test-classes.
<maven.test.skip>false</maven.test.skip>
Related
I'm using maven-resources-plugin to copy a file from resources dir to output dir and inject a variable;
there is the pom of the project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</plugin>
the resource file is something like this:
operation=${var}
and in he pom there is:
<properties>
<operation>true</operation>
</properties>
When I build the project (mvn -U clean package ) locally everything works fine: the file .properties is correctly inside the jar and it contains "operation=true".
The problem appears when I deploy the jar to my artifactory. If I download and open the jar from artifactory I can still find the .properties file, but in this case it contains: "operation=${var}" (the plugin doesn't inject the value of the variable). The command for the build inside the .yml file is the same that I run locally (mvn -U clena package). Any suggestions? The only difference that I can see is that on my computer I have maven 3.6.1 and on the server where I build the project for artifactory there is maven 3.3.3.
This is my project structure:
This is my code, trying to read the file in the resources folder:
package passgen;
public class Application {
public static void main(String[] args) {
System.out.println(Application.class.getResourceAsStream("/configuration.properties"));
System.out.println(Application.class.getResource("/configuration.properties"));
System.out.println(Application.class.getClassLoader().getResourceAsStream("/configuration.properties"));
System.out.println(Application.class.getClassLoader().getResource("/configuration.properties"));
System.out.println(new Application().getClass().getResourceAsStream("/configuration.properties"));
System.out.println(new Application().getClass().getResource("/configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResourceAsStream("/configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResource("/configuration.properties"));
System.out.println(Application.class.getResourceAsStream("configuration.properties"));
System.out.println(Application.class.getResource("configuration.properties"));
System.out.println(Application.class.getClassLoader().getResourceAsStream("configuration.properties"));
System.out.println(Application.class.getClassLoader().getResource("configuration.properties"));
System.out.println(new Application().getClass().getResourceAsStream("configuration.properties"));
System.out.println(new Application().getClass().getResource("configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResourceAsStream("configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResource("configuration.properties"));
}
The results are all null:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
Replacing "configuration.properties" with "src/main/resources/configuration.properties" (both with slash and without slash) doesn't make any difference.
Other answers, like this, tell to use .getClass().getClassLoader().getResource(fileName) but this is already one of the lines. Why are they all null and how do I get the resource?
POM:
<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>passgen</groupId>
<artifactId>passgen</artifactId>
<version>1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>passgen</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/dist</targetPath>
<includes>
<include>**/*<!-- all resources that go to folder, rest will go into the jar --></include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*<!-- all resources that go to folder, rest will go into the jar --></exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<!-- <addClasspath>true</addClasspath> -->
<mainClass>
passgen.Application
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
You have to provide the full path to the class loader in case you want to load it from the project directories.
System.out.println(Application.class.getResourceAsStream("/main/resources/configuration.properties"));
Hope this helps.
Found the answer. For some reason Eclipse puts an exclusion pattern of "**" on the main/java/resources folder. If someone else has the same problem: right click on the project -> Build Path -> Configure Build Path -> Source tab. For all entries check the "Excluded" voice, it should be "(None)". If you have an exlcusion pattern that excludes your files from the classpath (like "**") click on Remove to remove it.
EDIT: for some reason Eclipse adds an exclusion pattern of ** to the src/main/resource folder when you run Maven -> Update project
EDIT 2: I found that the exclusion pattern in Eclipse on src/main/resource folder is normal (see this answer). The exclusion means that it's not Eclipse handling the src/main/resources folder compilation but it's Maven (the Maven plugin of Eclipse to be precise, M2Eclipse). The fact that those resources weren't found in the classpath was due to the exclusion present in the pom.xml:
<resource>
<directory>src/main/resources</directory>
<excludes>
<!-- these resources will be excluded from the classpath; they will not go in to the target/classes folder and will not be packaged into the artifact -->
<exclude>**/*</exclude>
</excludes>
</resource>
which I removed to get the output listed below.
Now the output of the code above is this:
java.io.BufferedInputStream#7852e922
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties
null
null
java.io.BufferedInputStream#4e25154f
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties
null
null
null
null
java.io.BufferedInputStream#70dea4e
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties
null
null
java.io.BufferedInputStream#5c647e05
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties
If you want to exclude a resource from the jar exclude it in the maven-jar-plugin section. If you want to produce in output the resources that you excluded from the jar, configure the maven-resources-plugin with the goal copy-resources specifing the destination folder (by default resources are packaged into the artifact, if you just exclude them from the artifact you will have resources nowhere).
I'm trying to configure my application to have 2 build profiles: development and production. In order to do that, I created two subdirectories under the src/main/resources folder: src/main/resources/development and src/main/resources/production. Each subdirectory has its own .properties files.
<profiles>
<profile>
<id>development</id>
<build>
<resources>
<resource>
<directory>src/main/resources/development</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>production</id>
<build>
<resources>
<resource>
<directory>src/main/resource/production</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
I build the app with the command mvn install -P ${profile_here}
Maven copies the content of the folder related to the chosen profile to the WEB-INF/classes output directory, however the development and production folders are copied as well.
WEB-INF/classes
WEB-INF/classes/development
WEB-INF/classes/production
How can I solve this problem?
Thanks in advance.
The maven-war-plugin is rather limited when it comes to resources. However, you could use the maven-resources-plugin to include/exclude resources like described here: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
I have a test.properties file located in: $PROJECT_HOME/src/test/resources with the following content:
hostname=${host}
I also have a main.properties file located in: $PROJECT_HOME/src/main/resources with the same content.
Then I specified the followinging lines in the pom-file of my project.
<properties>
<host>localhost</host>
</properties>
<build>
<resources>
<!-- Filter resource files -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
....
</build>
After executing a mvn clean install I see that the main.properties in my target folder is replaced with the localhost value. However the property in my test.properties is not...
My first idea was to adapt the resources as this:
<properties>
<host>localhost</host>
</properties>
<build>
<resources>
<!-- Filter resource files -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
....
</build>
When building now, the test.properties file is replaced with the localhost value, but is placed into the classes folder in the target. In test-classes, there is still the test.properties file without the replaced value...
Is there a way to also replace the value in the test-classes folder?
My idea is to work on my local server with the localhost value without specifying it as a parameter and to overwrite this value with an alternate host when performing integration tests against a test server. In this case I specify the value -Dhost=<> on our continuous integration system.
This should work (although test.properties will not end up in your classes folder and test-classes would have both test.properties and main.properties with the replaced value which I think was what you wanted but wasn't sure).
<resources>
<!-- Filter resource files -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<!-- Filter test resource files -->
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
Also, you may want to have two properties, ${host} (referenced in host.properties) and ${test.host} (referenced in test.properties), otherwise if you specify -Dhost during an integration build it will replace the value in both the host.properties and the test.properties. This way you can specify -Dtest.host to change which host you are using for integration tests but not the host that is set when you deploy.
I am using JNDI to read database configuration from my application's context.xml. The way I currently have this setup is to have [appname].xml in conf/Catalina/localhost. However, when I redeploy the app, this file gets overridden with an empty context file, and I have to copy the custom one back to the conf/Catalina/localhost directory. I have different database settings, etc. for my test and production servers, and so don't want to put the context file in META-INF in the WAR file, but would like to just keep it in the conf/Catalina/localhost directory. Is this possible?
Is there somewhere better to put the database configuration?
I'd also like to avoid putting the configuration in the server.xml file, although I know this is possible.
Thanks!
I would say look into using maven profiles (one for prod, one for test), and having different resource definitions for each profile. You can keep your common files in src/main/resources and then have a folder for each profile type to keep specific config files in:
src/test/resources
src/prod/resources
Then you can amend your pom to define each profile and its associated resources:
<project>
<profiles>
<profile>
<id>prod</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
<resource>
<directory>src/prod/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
</resources>
</build>
</profile>
<profile>
<id>test</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
<resource>
<directory>src/test/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
</resources>
</build>
</profile>
</profiles>
</project>
finally you can build the war using the -Pprod or -Ptest profile argument to mvn
mvn -Pprod package
Problem is that undeploy removed the webapp specific context.xml file that was installed in Catalina/localhost/.xml
If you don't want to have the file removed, you'll have to just redeploy it, not undeploy/deploy