I have a Maven web project and I have some CSS and Javascript files under the src\main\webapp\ folder. I constantly make changes to those files and would like to see my changes quickly. If I run maven install, it takes ages due to project dependencies. Sometimes all I want to change is one line of code in my CSS file and do not want to recompile everything else. I have a maven plugin that publishes my output war file to my JBoss instance. Ideally, I would like to run a maven execution script that will quickly copy my web resources to the output folder and reploy the changed war file without recompiling everything else.
I tried invoking the generate-resources goal but that doesn't seem to look in the src\main\webapp directory as it is expecting my resources to be under the src\main\resources folder. What am I missing here?
Thanks
If you want to add more resources to be copied during the generate-resources plugin, you can change the resources folders used by your build. The project.build.resources property controls which folders are searched for resources. You could add:
<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webApp</directory>
<includes>
<include>*.css</include>
<include>*.js</include>
You would then run mvn resources to copy the files.
This approach is that these files will always be copied during the resources phase of any build. You can get around this by using the copy-resources goal instead of resources. In this case you would use the following configuration:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-web-resources</id>
<!-- here the phase you need -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/webApp</outputDirectory>
<resources>
<resource>
<directory>src/main/webApp</directory>
<includes>
<include>*.css</include>
<include>*.js</include>
You could then run mvn resources:copy-resources to copy the files.
I think you could accomplish this by using the war:war goal. This should generate a war file in the output folder for you without re-compiling the source.
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 am trying to add a license file to all of my jars when executing a maven build. I have the license on each class file, but I am looking to add License.txt to each META-INF folder within each jar
My project has a master pom, which has half dozen modules, those modules then have modules of their own, and eventually get to a project that generates a /target/<.jar-file>. The build and the class level licenses are working, I am just looking to add a physical License.txt into the META-INF folder.
My file is stored (relative to the master POM) in /src/resources/src-license.txt. I really need the automated method to ensure that if/when the license changes, I dotn have to update 50 files, I can just update the one, which is then copied out to the other locations.
I have tried using
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/resources</directory>
<targetPath>/META-INF</targetPath>
<includes>
<include>src-license.txt</include>
</includes>
</resource>
</resources>
....
</build>
But that doesnt seem to do the trick. I have also tries some alternatives to the output path, such as ${project.build.outputDirectory}/META-INF, or */META_INF, also to no avail. Does anyone have some experience on how to accomplish this? Thanks
Also, I use the maven-license-plugin to ensure that each class file has the license info pasted into it, and that functions as intended. But again, that inside the class files, I am looking for an external .txt file in each <*.jar>/META-INF/
Using resources from parent relativePath is at least difficult (what if you have a complex directory structure?).
A simple and clean way can be to create a separate module containing your src-license.txt file. Then make it a dependency of your modules and unzip it (dependency:unpack-dependencies) # generate-resources phase, inside target/classes.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-license</id>
<phase>generate-resources</phase>
<goals><goal>unpack</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.acme</groupId>
<artifactId>com.acme.license</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
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