intellij is copying src to target - java

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>

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.

Loading an excel(.xlsx/.xls) file from maven project resources folder (src/main/resources)

Here i am trying to load an excel file from resources folder(src/main/resources) of a maven project.
Folder Structure
MyWebApp
|______src/main/java
| |____Test.java
|
|______src/main/resources
| |______test
| |___hello.properties
| |___template.xlsx
|______target
|___MyWebApp
|____WEB_INF
|___classes
|__test
|__hello.properties
|__template.xlsx
My Approach
//loading excel file
String resource = "/test/template.xlsx";
System.out.println(this.getClass().getResource(resource) == null); // prints true
//loading properties file
String resource = "/test/hello.properties";
System.out.println(this.getClass().getResource(resource) == null); //prints false
//I have also tried below methods
this.getClass().getClassLoader().getResourceAsStream(resource); //null
new ClassPathResource(resource).getInputStream(); //null
After doing some googling i came to know, maven filters binary contents. To over come that i modified my pom.xml to allow .xlsx,.xls file extenstions not to be filtered with this help.
pom.xml
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*.xlsx</include>
<include>**/*.xls</include>
</includes>
</resource>
</resources>
</configuration>
I could able to load the properties file, but i could not able to load the excel file by using above approach. From my side i referred the below two links (Rererence-1,
Reference-2) but no success. Please help me if you have some thoughts/ideas on this issue.
In the maven documentation page which you've linked in your there is said:
If you have both text files and binary files as resources it is
recommended to have two separated folders. One folder
src/main/resources (default) for the resources which are not filtered
and another folder src/main/resources-filtered for the resources which
are filtered.
So you should hold the properties and xlsx files in separate directories.
There is also an information about excluding binary files from filtering:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<configuration>
...
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
...
</configuration>
</plugin>

File not found exception, only when including the code as a maven dependency

I have written this project which has a bunch of config files in config/ folder, which are necessary for running:
https://github.com/danyaljj/jwnl-prime/
The code is working fine under mvn test in my computer and CI.
But after packaging it and adding it as a maven dependency to another project, the function calls give me error
java.io.FileNotFoundException: config/file_properties.xml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at net.didion.jwnl.TestDefaults.getInputStream(TestDefaults.java:63)
at net.didion.jwnl.JWNL.initialize(JWNL.java:92)
Any idea why I am getting this weird behavior?
Update:
after extracting the jar file, it turns out that the config files are not included in the jar:
Update2: After adding the suggestion by ? the config files get included in the jar file:
But still getting the same error: config/file_properties.xml (No such file or directory)
If you have project structure other than standard format you need to update pom.xml as well. If you need to include the config for build you need to add that in build section like
Refer link enter link description here
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.4</version>
</extension>
</extensions>
<resources>
<resource>
<directory>config</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
First, verify that the configuration files are actually being packaged into the jar. I would view it in an archive viewer or unpack it and see what's in it.
Secondly, you can't directly load a file from a jar using standard file opening methods. You'll have to use something like
Class.getResourceAsStream()
See here for some more suggestions:
How do I access a config file inside the jar?

Maven resources/ in sibling modules

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>

Categories

Resources