Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven war plugin copy arbitrary files

I apologize that this is surely basic maven/war plugin stuff, but I'm totally not a maven user. I just have to hack this one thing into a project that is maven based.

What I need to do is to copy an (essentially arbitrary) directory and files into the root of my war. I need them to appear as resources available via HTTP when the war deploys.

I cannot simply put them in the "right place" in the source tree. This is because the files in question are actually the source files of my project. I realize this is a bit odd, but this is a documentation project, and I need to show the source and the effect all in the same war.

So, in the general case, how would I configure the maven war plugin to copy a given directory, along with it's contents, to the root of my war? (BTW, I have tried to make sense of the documentation of this tool, but it seems to be predicated on so much understanding of maven that it feels like I'll never understand it without learning maven first, and I'm a bit too pressed for time to do that just now!)

Many TIA Toby.

like image 603
Toby Eggitt Avatar asked Nov 15 '13 00:11

Toby Eggitt


People also ask

How do I copy files from one directory to another in Maven?

Using the Copy Rename Maven Plugin The copy-rename-maven-plugin helps copying files or renaming files/directories during the Maven build lifecycle. Upon building the project, we'll see foo. txt in the target/destination-folder.

What is packagingExcludes?

packagingExcludes: The comma separated list of tokens to exclude from the WAR before packaging. With packagingExcludes, the tokens are completely excluded from the final war file. With warSourceExcludes, the tokens are just ignored when copying the war directory into the war file.

How do I create a war file in Maven?

Using the mvn:war:exploded command, we can generate the exploded WAR as a directory inside the target directory. This is a normal directory, and all the files inside the WAR file are contained inside the exploded WAR directory.

What is war packaging in Maven?

Goals Overview war:war is the default goal invoked during the package phase for projects with a packaging type of war . It builds a WAR file. war:exploded is generally used to speed up testing during the developement phase by creating an exploded webapp in a specified directory.


2 Answers

You could try:

  1. use the copy-resources plugin. Use the plugin to copy your source files under target prior to when the war is packaged.

  2. or, configure the maven-war-plugin to include additional resources.

like image 115
Drew MacInnis Avatar answered Oct 01 '22 00:10

Drew MacInnis


Thanks Drew, that got me where I needed to go. Bottom line, I added the sample pom fragment from the link you gave for copy-resources to my pom.xml with the following changes:

<outputDirectory>target/${project.name}-${project.version}/sources ...

<directory>src/main/java ...

I copied this from the link you gave, then edited the element to point at src/main/java, which picked up my files, and the outputDirectory to the aggregate target/${project.name}-${project.version}/sources . I found that ${project.name} mapped to the project, and the version came from ${project.version}, which seemed to resolve the last little bits of issue.

Thanks again. Toby

like image 34
Toby Eggitt Avatar answered Oct 01 '22 01:10

Toby Eggitt