How can I write maven build to add resources to classpath? - java

I am building a jar using maven with simple maven install.
If I add a file to src/main/resources it can be found on the classpath but it has a config folder where I want that file to go but moving it inside the config folder makes it disappear from the classpath.

A cleaner alternative of putting your config file into a subfolder of src/main/resources would be to enhance your classpath locations. This is extremely easy to do with Maven.
For instance, place your property file in a new folder src/main/config, and add the following to your pom:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
</build>
From now, every files files under src/main/config is considered as part of your classpath (note that you can exclude some of them from the final jar if needed: just add in the build section:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>my-config.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
so that my-config.properties can be found in your classpath when you run your app from your IDE, but will remain external from your jar in your final distribution).

If you place anything in src/main/resources directory, then by default it will end up in your final *.jar. If you are referencing it from some other project and it cannot be found on a classpath, then you did one of those two mistakes:
*.jar is not correctly loaded (maybe typo in the path?)
you are not addressing the resource correctly, for instance: /src/main/resources/conf/settings.properties is seen on classpath as classpath:conf/settings.properties

By default maven does not include any files from "src/main/java".
You have two possible way to that.
put all your resource files (different than java files) to "src/main/resources" - this is highly recommended
Add to your pom (resource plugin):
​
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

Related

Maven filtering - replacement of variables

I have a maven project, which generates a jar file as a web project. Based on Maven I include a standalone Tomcat. Inside of the jar file, there is actually the war-file, which contains my application.
This application contains a "version.txt" in src/main/config (or any similar path), that is finally included in the war-file.
This version.txt looks like:
version: ${project.version}
I would like, that maven should replace the variable with the correct version from pom.xml.
In my pom.xml I have included:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
<filtering>true</filtering>
<includes>
<include>**/version.txt</include>
</includes>
</resource>
<resource>
<resources>
</build>
So is there any way to include this version.txt and a working replacement in a war-file, which is in a (Tomcat)jar-file?
Addendum:
My File hierarchy looks like:
jar-file
-- ...
--war-file
---- ...
----version.txt
I suggest you use the maven-war-plugin.
https://maven.apache.org/plugins/maven-war-plugin/usage.html
It will expect a certain directory layout, and in the examples it clearly shows how to filter (replace maven variables into the web resources)
https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
If this solution falls short, then a more specific question based on this should should be asked later.
Thanks for the advice. No, there was no maven-war-plugin, but I have included it and based on the instructions, it works.
Short solution:
Added a resourceDirectory on same level like pom.xml and included version.txt
Added in pom.xml and activate filtering:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>externalresources</directory>
</resource>
</webResources>
</configuration>
</plugin>

Maven delete resources after testing

I recently read you can have a logback.xml and a logback-test.xml in your classpath, where the test file has higher priority.
Logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, logback tries to find a file called logback.groovy in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath..
Source
So I thought it would be a great idea letting logging happen in the console while testing and log to a file after buildung with maven (without having to change the output manually).
I found the maven-resources-plugin, which can <exclude> some resources. I specified test files (like logback-test.xml) like this in the plugin:
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>test/**</exclude>
<exclude>*test*</exclude>
</excludes>
</resource>
</resources>
</configuration>
</plugin>
Which works great, but has one problem. I definitely need access to the *test* files (yes, also logback-test.xml, so I cannot just exclude only it instead of the wildcard *test*) and the test/** directory during tests. I only want to exclude/delete them after testing is complete. With this configuration the excluded resources are never copied, but I want them to the copied first (to make them accessible by tests) and then (after tests run successfully), delete them.
How can I achieve this? I've been lookung for a "maven delete plugin" but couldn't find any.
Things are much simpler.
Maven separate sources/resources for the packaged application and sources/resources for the execution tests.
Simply move logback-test.xml in the src/test/resources folder.
And place logback.xml in src/main/resources.
In this way, logback-test.xml will be available during the tests of your build.
And as the file is located in src/test/resources, it will never be included it in your application.
While the packaged application will contain and use only logback.xml as defined in src/main/resources.
I found my own solution.
Delete the maven-resource-plugin. Instead, based on your packaging, use maven-jar-plugin or maven-war-plugin which is responsible for building the jar or war file (and runs later, after the tests).
jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>test/**</exclude>
<exclude>*test*</exclude>
</excludes>
</configuration>
</plugin>
war
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<packagingExcludes>${webdir}/test/**,${webdir}/*test*</packagingExcludes>
</configuration>
</plugin>
where webdir is the path to your classpath root from the built war file. You can add it to your <properties> tag (directly under the <project> tag). In my case it is WEB-INF/classes.
pom.xml
<properties>
<webdir>WEB-INF/classes</webdir>
</properties>

JavaFX Maven Plugin: com.zenjava - How to copy external resources

I'm relatively new to Maven + JavaFX and I'm trying to produce a JavaFX executable jar file with the com.zenjava maven plugin.
I was following this guide for reference:
https://www.youtube.com/watch?v=wbjW8rYlook
I have the following folder structure for my project:
Now I'm trying to run the config jfx:jar during maven build and was able to produce a jar file but the resources I need are not copied under the target/jfx/app folder.
Basically, I want to copy the entire src/main/resources folder to target/jfx/app/resources. How do I accomplish this?
Some information:
The src/main/resources/ folder will contain different kinds of files that I will need during runtime, (excel files, pdf, htmls...) and not just property files.
Thanks in advance.
========================================================================
UPDATE:
Yuri-M-Dias' answer helped.
Without changing any other setting, I managed to do this by just updating my pom file with:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>../jfx/app/resources</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.7.0</version>
<configuration>
<mainClass>me.iamkenos.bayonetta.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
This is definitely working but I'm not sure whether this is the best way, given I had to cheat it a bit by using "../" in <targetPath>../jfx/app/resources</targetPath> will wait for other possible answers for the meantime.
You can control Maven's output folders to specific folders using the resources keyword. For example, on my project:
<resources>
<resource>
<directory>src/main/java/view</directory>
<targetPath>view</targetPath>
</resource>
</resources>
I am forcing the contents of the java/view folder to output to the target/classes/view in this case, since it's where my JavaFX images and fxmls are. You can probably do the same for the jfx/app/resources folder.
As for copying the folder, you can take a look at the official maven recommendation.
When you run the command jfx:jar you will get executable jar file with resources folder inside because you added resources folder to the build path.
If you just copy the entire src/main/resources folder to target/jfx/app/resources folder you will have copies of the same resource files (inside and outside of generated jar file) and if you need to allow a user to edit some of resource files (e.g. *.properties files) your code I guess will rely on the inside files so user changes have no any effect in this case.
That is why you need to split project resources into:
Internal (the part of generated jar file e.g. raster graphics and read-only configs)
External (located outside the jar file e.g. config files that could be edited by user)
I would suggest to create 3 folders
\src\main\java (source code) - part of a build path
\src\main\resources (internal) - part of the build path
\src\main\config (external)- excluded from the build path
use maven to copy external config folder and build executable jfx jar
<build>
<resources>
<resource>
<directory>src/main/config</directory>
<targetPath>../jfx/app/config</targetPath>
</resource>
</resources>
</build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<vendor>${vendor}</vendor>
<mainClass>${mainClass}</mainClass>
<allPermissions>true</allPermissions>
</configuration>
</plugin>
</plugins>
The finishing touch is configuring symlink path to allow eclipse work in debug mode properly with external resources. You can use Link Shell Extension to do it.
for Windows it might look like
mklink /J C:\...\target\classes C:\...\target\jfx\app\config
LinkToFolder OriginalFolder
LinkToFolder is eclipse project folder with compiled classes

Maven WAR plugin - change front-end resources location

After minification I have content of webapp like this:
WEB-INF
assets
favicon
i18n
scripts
dist
index.html
//other things
Where inside dist I have compressed styles, scripts etc... But Maven WAR plugin copies everything to WAR, which causes WAR contains unminificated sources. I tried to change directory for webResources:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp/dist</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
But nothing changed. Can anyone help me with this? Thank you in advance for every answer.
packagingExcludes accepts a comma separated list of resources not to include. Add all the directories to it, that you want excluded, e.g.
<packagingExcludes>
WEB-INF/lib/tomcat-*.jar,
scripts
</packagingExcludes>
You also need to make sure that resources are not included by the maven resources plugin.
Instead of manually dealing with all the exclusions, I recommend to move all the files, that you don't want to end up in your war file, out of the directories, maven expects to contain the resources by default. You could move them to e.g. src/main/uncompressedResources. That way they'd still be in the project, but maven would not include them by default.

How to add an extra source directory for maven to use only when running tests or when debugging?

I have a set of configurable plugins that are being debugged.
They would not exist in a build of the main program but are part of the debugging regimen.
How to add them as resources that can be used in tests and debugging but not included in the final build?
The tests and debugging depends on the program locating the proper plugins and configuration directory/folders, compiling sources on the fly and then testing them with also stepping through them.
As a result, the source files used during the test must reside in the normal folder used during run-time.
I have this:
<build>
<resources>
<resource>
<directory>conf</directory>
</resource>
</resources>
</build>
The problem is that the resources are now being added to the final compile, which is not what is desired.
You'll have to keep the ressources in a different folder. as mentioned in the comments, you could use the standard archiecture (src/main/resources and src/test/resources) or as you tried you can define a test resources folder:
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>testresources</directory>
</testResource>
</testResources>
Helpfull links you'll find here: access the ressources in junit and the maven guide

Categories

Resources