My situation is:
I have a Maven project, I have my java classes in /app/src/main/java, my resources in /app/src/main/resources and my webapp files in /app/src/main/webapp
I have a javascript file in /common/script.js
Now what I want is to include (copy) the javascript file to the war file during the build phase of maven. To be precise, I want the script.js to land in /js/ directory of the war archive, just as it was placed in /app/src/main/webapp/js before starting the build.
I need this to share one version of resource files among many web-apps.
Kind regards,
Q.
You could do something like this, as documented here.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>../common</directory>
<targetPath>/js</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element.
Check
"maven-resources-plugin"
You can use maven-resources plugin to copy a file to the desired location. Before or after a war has been built
Related
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>
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
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.
I have inherited a project which uses a maven pom file to build the .war file and we have a request to generate a checksum of the .war file, so my question is, can this be done in the maven pom file? and what plugin if any is needed and how to configure it?
we use the maven-war-plugin for building
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webResources>
<resource>
<directory>target/minifiedOutput</directory>
</resource>
</webResources>
</configuration>
</plugin>
is it possible to use the maven-install-plugin to generate a checksum of the output war file, searching around the subject it seems to provide checksum functionality but it isn't clear to me how to integrate it with the existing pom file?
Use the Maven install plugin :https://maven.apache.org/plugins/maven-install-plugin/plugin-info.html , set the property in the configuration: createChecksum to true. This is a common practice.
There's also other plugins :
http://nicoulaj.github.io/checksum-maven-plugin/
I've started a new Maven project in NetBeans, accepting all the defaults. The POM, with all the JAR dependencies stripped out, is cut-n-pasted at the bottom of this question.
The application reads in various properties files (e.g. logging and config). It also reads in external resources such as fonts, images, and sounds. I do NOT want all these resources to be bundled up into the JAR file. Instead, I plan to deploy them in subdirectories beneath the directory where the JAR is deployed.
A simplified view of the project's directory structure looks like this:
-src
|---main
|---java
|---com.mypackage, etc
|---resources
|---conf
|---fonts
|---images
|---sounds
+target
What I would like to have after a clean build would look like this:
+src
-target
|---myproject-1.0.0.jar (compiled contents of "src/main/java" ONLY)
|---conf
|---fonts
|---images
|---sounds
However, when I do a "clean-and-build" or an "exec" through NetBeans (or the command-line for that matter)... what I'm actually getting looks like this:
+src
-target
|---classes
|---("src/main/java" and "src/main/resources" slammed together)
|---myproject-1.0.0.jar (the "classes" subdirectory JAR'ed up)
Can someone point me in the right direction for getting that first result rather than the second? I apologize if this is a silly question (I'm a Maven rookie), or if I overlooked a previously-asked duplicate. However, from the searching I've done on Stack Overflow... it looks like all the duplicate questions try to go the other way! (i.e. get resources into a JAR rather than keep them out)
pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>steveperkins</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>My Project</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
Although the proposed solutions would work they basically work around the maven conventions. A better alternative would be to filter out the resources so they are not included in the jar but still available as resources while working in the IDE. In the pom it should look like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>/conf/**</exclude>
<exclude>/fonts/**</exclude>
<exclude>/images/**</exclude>
<exclude>/sounds/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This would effectively exclude them from the jar without any workarounds.
Here is the doc page for the jar plugin.
Though the above will answer your question may I suggest some additional possibility that could help you in your endeavour. As a second step, to still make these resources available you could package your project using the assembly plugin. this would allow you to create a zip file and place all the files, resources and jar, in an appropriate location so that when the zip is unpacked everything just falls into place.
If this project is part of a larger work you can still use the assembly plugin for each where you would have this situation and in the main project you could extract and reassemble them in a larger zip including all the necessary artifacts.
Lastly I suggest you leave the directory structure under target as-is. If you customize it it would be preferable to do it through the Maven variables so that the changes percolate to the other plugins. If you manually remove and rename stuff once Maven has gone through you may run into problems later. Normally the Maven jar plugin should be able to just get it right if you configure it the way you want so you have no needs to worry about what comes under target. Personally I use Eclipse and the pusign is pretty good at getting the IDE and Maven config in sync. For NetBeans I would suspect this would also be the case. If not the best approach would be to configure your project in NetBeans to use target/classes as a target folder for built artifacts and target/test-classes for stuff built from src/test/java.
Personally, I would not use the default location of resources but an "extra" location and configure the resources plugin to copy them where you want from there:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
If you insist with using the default location (src/main/resources), I think you'll have to configure some exclusions (see below) to avoid resources getting copied by default and then use the same approach as above.
Another option would be to use the AntRun maven plugin and Ant to move files but this is not really the maven way so I won't detail it.
Resources
Copy Resources
Including and excluding files and directories
You can sonfigure a special execution of resources:copy-resources goal.
Eugene is on the right track but there's a better way to make this work.
It should look something like this:
<build>
<outputDirectory>target/${artifactId}-${version}</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<outputDirector>target</outputDirectory>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources/conf</directory>
<targetPath>../conf</targetPath>
</resource>
<resource>
<directory>src/main/resources/ANOTHER_PATH</directory>
<targetPath>../ANOTHER_PATH</targetPath>
</resource>
</resources>
</build>
You won't be able to get rid of the 'classes' directory, but you'll be able to give it a different name that shouldn't interfere with NetBeans.
You can find more about the <outputDirectory> element here.
You can find more about the jar plugin here.
You can find more about the <resource> element here.
As a side note, you may want to consider running Maven under a 1.6 JDK and fork the maven-compiler-plugin to use your 1.4 JDK for compiling. You can find out more about this here. That should give you a boost to your compile time. You can also tell surefire when running test cases to use the 1.4 JDK for execution as well.