Maven resources/ in sibling modules - java

I have a issue with some tests in sibling modules.
I have the following folder in module 1 in resources/: sql/statements
all the files in that folder is loaded with the following code:
URL url = this.getClass().getClassLoader().getResource("sql/statements");
assert url != null;
Collection<File> files = FileUtils.listFiles(new File(url.getPath()),
FileFilterUtils.suffixFileFilter("sql.xml"), TrueFileFilter.INSTANCE);
This works fine:
inside intellij for the sibling module
inside intellij in the module with the xml files
with maven clean install in the module with the xml files
but for some reason when I reference this class in a test in the sibling module it is unable to find the xml files when I run clean install from command line.
how can I solve this?
edit: when it fails it print the follwing url path, which seem ok (?):
url: jar:file:/home/<user>/.m2/repository/<path>/1.0.0/<jar-name>1.0.0.jar!/sql/statements

Check your POM files : you are probably only copying the resources in the build of your "sql/xml" module. Better move it up to the parent POM if you want to use it in sibling modules.

As for this maven doc, test resources should be stored in
src/test/resources
But you can set parameter testResources to the same directory as main resources
<build>
<testResources>
<testResource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</testResource>
<testResources>
<resources> <!-- this is default setting -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

Related

Java / Mvn Local Resource not found in .jar Package

I'm tying to add a local Resource (A simple JSON File) to my Maven Project. But when I execute the code, I get the Error:
java.io.FileNotFoundException: ./resources/test.json (No such file or directory)
This is the Structure of my Project:
-Router
--src
---main
----java
----resources
-----test.json
--pom.xml
In my pom file I add the json:
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.json</include>
</includes>
</resource>
</resources>
it seems it can't find the resource because in the code when instead of the path I look for
this.getClass().getResources("test.json")
it returns null. Any suggestions how I can resolve this issue?
From Class, the path is relative to the package of the class unless you include a leading slash, so if you don't want to use the current package, include a slash like this:
this.getClass().getResource("/test.json")

How can I get the resource directory in maven to work?

I have a Java Maven web app project that I'm trying to clean up. In the <build> section of my pom.xml, I have the following:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<filters>
<filter>profiles/${build.profile.id}.profile.properties</filter>
</filters>
[...other properties...]
</build>
In my project, which on my Mac is /Users/anthony/workspace/my-project/, i src/main/resources/profiles/I have local.profile.properties and qa.profile.properties.
Then, in my maven profiles in my pom, I define ${build.profile.id} as the following:
<profile>
<id>local</id>
[...stuff...]
<properties>
<build.profile.id>local</build.profile.id> <!-- or qa -->
[...stuff...]
</properties>
</properties>
When I am in my console and run $ mvn clean install -Plocal, I get the following error:
Failed to execute goal ... on project my-project: Error loading property file '/Users/anthony/workspace/my-project/profiles/local.profile.properties'.
It seems like Maven is not recognizing the resource directory for my filtering profile properties file. This only works when I explicitly put the entire path of my properties file, like so:
<filters>
<filter>src/main/resources/profiles/${build.profile.id}.profile.properties</filter>
</filters>
Is there a way for me to not have to explicitly state src/main/resources? I thought that the point of me declaring a resources directory was that I could use it, especially for declaring filtering.
The resource directory only has a meaning as "resources" for the Java artifact being built, but not for Maven itself. For Maven, the "resources" directory has a special meaning in the sense that Maven knows where to copy those files to in the resulting jar-file. But for Maven working with files or filtering files, you have to tell Maven the exact path, as Maven does not know if the filtered file is a resource, a source file, or something else. Also, you could have multiple source or resource directory defined, and Maven would not know, in which one to filter. Thus you always need to specify the full path for Maven.
So, in short:
Is there a way for me to not have to explicitly state src/main/resources?
No.

intellij is copying src to target

I imported the following project into intellij idea: https://github.com/brendano/ark-tweet-nlp/
I managed to get a working jar built, but for some reason it's copying all the source files from /src/ into /target/, so the resulting jar also includes all the source files. How do I prevent this? If it helps I've included a screenshot of the directories below:
Open the Settings window CTRL + ALT + S
Click on the Compiler option. You should see a list of Resource Patterns which determines which types of file IntelliJ will add to the target folder.
Add !*.java; to the list of resource patterns to exclude .java source files from being added to your target directory.
If this doesn't work, you can try adding a <build> tag to your Maven POM containing the following:
<build>
...
<resources>
<resource>
<directory>src/com</directory>
<targetPath>com</targetPath>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
...
</build>

Reading Properties file in Maven Java Project created by maven-archetype-quickstart

I'm trying to read a properties file using the below code on a Java Project created using Maven ArchetypeId=maven-archetype-quickstart
(properties = new Properties()).load(DbCopy.class.getClassLoader()
.getResourceAsStream("config.properties"));
And it throws exception:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties.load(Properties.java:284)
at benz.bnp.db.DbCopy.main(DbCopy.java:77)
Thanks for helping !
Have a look at the target folder, if it contains your properties file. Hopefully it will not contain. To include your property file edit your pom:
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
Add it to your POM.xml and configure the same for your properties file.
Another solution:
Right Click on Project in Eclipse
Select Build Path -> Configure Build Path
In Source tab : Add **/*.properties in "Included" for "src/main/java".
The problem was: maven-archetype-quickstart only sets **/*.java in the "Included" list.
Assuming your config.properties is under src/main/resources try something like this :
Properties props = new Properties();
try(InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties")) {
props.load(resourceStream);
}

surefire not picking up properties file

Hi I have module inside a project which i'm trying to run from the modules directory as such:
mvn integration-test -PmyProps
in pom
<properties>
<user>admin</user>
</properties
in src/test/resources/test.properties
user=${user}
When i execute the test i get user={user} instead of user=admin
If i run it via intellij or eclipse i have no problems with it and properties get picked up?
is it because the property values are getting assigned to the target directory?
This is the class loader:
InputStream testPropertiesInput = WebDriverConfiguration.class.getClassLoader().getResourceAsStream("smoke.properties");
i've tried making it "target/classes/smoke.properties" but no luck.
First if you have unit tests which seemed to be in your case use simply:
mvn test
furthermore to get resource files beeing filtered you need to active filtering
<build>
<resources>
<resource>
<directory>...</directory>
<filtering>true</filtering>
</resource>
...
</build>

Categories

Resources