How to copy directories into WAR using Maven? - maven

My goal is this: I have 2 directories (not their contents but the directories themselves) which I need to get copied into both the exploded WAR directory and the packaged WAR.
Specifically, I need the following directories inside the final WAR:
${project.basedir}/web/WEB-INF/templates needs to get copied into
target/${finalName}/WEB-INF/
${project.basedir}/web/WEB-INF/classes/dao needs to get copied to
target/${finalName}/WEB-INF/classes/
So far, I haven't found any way of doing it. I've tried the resources plugin, the war plugin's webResources feature, antrun, exec-maven-plugin, etc., and each have limitations that prevent the aforementioned directories from ending up in the final WAR. In some cases the resultant directory structure ended up with too much nesting (e.g.: target/${finalName}/WEB-INF/classes/WEB-INF/classes/); in other cases nothing got copied at all, presumably because the war plugin has its own beliefs about what should and should not get copied.
As such, I'm at my wit's end. Is there any way to copy actual directories without having the war plugin question my motives? I wish I could tell Maven, "Do as I say!"

Related

In Maven, how to compile a class outside the source directory into an arbitrary target directory?

I have a legacy app that I'm porting from Ant to Maven. My Maven build works fine for the main project, which I've moved into the standard Maven directory layout (*.java files in /src/main/java/) and it outputs the compiled classes into /target/classes/ as neat as you could wish. These are packaged in a .war file.
However, the project also has a class outside of the folder hierarchy, indeed outside of the web application, that contains scripts that run via cron job. Let's say it's /cronjobs/MyClass.java. I need that class to be compiled and output to /target/cronjobs/MyClass.class and zipped up as part of the resulting .war file, in its /cronjobs/ folder.
Can Maven do this? I know it's possible to change the default "src" directory and "target" directory, but I don't know if (or how) it's possible to run a separate, parallel compile step for just one class.
I can move the source file, of course, if it's easier to compile it with the other classes and then move it later (maybe with the WAR plugin?) but I definitely need the compiled MyClass.class file in the /cronjobs/ directory of the .war.
I'd split the project in 2 parts, webapp as war and cronjobs as jar. Maven knows about multi-module format and it is somewhat the best way to go forward and decouple the webapp from non-webapp code.

Maven: how to copy a directory cleanly before creating the JAR?

I want to store some additional files in the JAR that gets created. Those files are in a directory that is a subdirectory of a repository which is pulled in via a git submodule.
I want to copy that submodule to my src resources directory before compiling, but I also want to make sure that any old files at that location are removed first.
How can that be achieved best with Maven plugins? I did not find any option to remove any destination files with the copy-resources goal of the maven-resources-plugin and I could not get the maven-clean-plugin to run right before the copy-resources either. So how does one accomplish such a trivial task with Maven?
UPDATE: as mentioned above, the reason why I want to do this is because what is copied should become part of what gets added to the resulting jar (and could potentially be part of what gets compiled). So I need to copy these files into the src directory and NOT the target directory. What should get copied before each build is the input to the build, not an additional output.
There is one flaw in your approach, and it probably explains most of the obstructions you encountered.
During a build, the only directory in which you may write is target. Copying files to src or changing them is strictly discouraged.
The target folder is erased by clean, so no need to tidy up yourself or to manage old files.

How do I add a directory in /target to the resulting JAR with Spring Boot?

I'm using Enunciate to generate REST documentation upon building a REST application. Enunicate outputs documentation to /target/docs. I would like to add the /docs directory to the resulting JAR file (and rename it) to be able to serve docs as static content.
How do I do this? I can't figure out how to get these static files (which are generated upon build) into the JAR.
I guess you can solve this by configuring the Maven plugin for enunciate and wiring it up to be run in the 'generate-resources' lifecycle phase.
Also, make sure you set the output-dir to a subdirectory of src/main/resources/static, as commented by Rob above.
I added this to my enunciate.xml to force the docs directory to be generated in a custom location which will be packaged with the .war file
<docs docsDir="target/<app_name>/docs"/>
and then maven will put the entire contents of target/ into the resulting war file package

Merge files from multiple war file

I have a main war file with maven dependencies to various war files. With the maven-war-plugin I am able to merge them into one, but my problem is, if files with the same name exist they will be overridden by the last one.
What I want is to merge the files with the same name.
This is not something done commonly with Maven. But have a look at the discussion of this SO question: How can I merge resource files in a Maven assembly?

Maven Assembly: Setting up external svn for in-program-use

I have a very specific question. The project I am on is a maven3 driven multi-module Project. The Project uses the SVN-Api to synchronize data from a repository and process it. More specifically:
A Manager, upon recieving an update call, will contact a remote svn repository and sync the files to the harddrive, where it will afterwards process them.
I want to create an appropriate folder Structure, with the assembly plugin. When I run a package command, ideally the output in the target folder would be
target
- data <- this is the base folder for the external repository
--- .svn <-- repository-information
--- group1 <-- subfolders on repository
--- group2
--- group3
- program.jar <-- contains java classes, executable
- config.xml <--- config file, editable by user
- other miscalleanous files
So frankly, this poses a number of problems and on top of this, I don't have too much experience with the assembly plugin. What I am not quite getting is how I am able to differentiate between files that should be put into the jar and files that should be kept outside the jar.
I have read about the assembly descriptors, I have however only seen examples that either include or exclude files, none that provide information about the relative positions the files should later have in the target folder.
Thanks for your time.
Regards,
Keno
So after some research I decided to go the following (simple) way:
At startup, The program fetches the location it's jar file is at, then checks for an resource folder outside the char. If none is found, it checks out the base directory of the svn server and starts crunching numbers. This way didn't require any fiddeling with the maven pom, and it works for testing as well.

Resources