How to copy directory using maven copy resoures plugin - maven

I have referred the maven documentation here https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html to understand about including whatever the files we need. But it has mentioned only about including files. So how can I copy all the directories and files inside a specific directory using maven resources plug-in?

Try without specifying any includes and excludes so that it picks all directories and files inside the resources directory.
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>

Related

Maven exclude resources not working as expected

I am trying to exclude from a build all YAML resource files, but the ones with a prod clause within the filename.
For example, given that my resource directory contains application-dev.yaml, application-test.yaml and application-prod.yaml, I would like application-dev.yaml and application-test.yaml to be excluded and application-prod.yaml to be kept.
The portion of my POM that deals with the resources is below:
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*-!(prod).yaml</exclude>
</excludes>
</resource>
<resource>
<directory>${project.basedir}/web/WEB-INF</directory>
</resource>
</resources>
However, exclusion does not work and all YAML files are copied, including application-dev.yaml and application-test.yaml.
I tested the exclusion pattern in Bash shell by ls *-!(prod).yaml and it worked as expected.
At this point I am lost and am looking for the community assistance.
I thank you all in advance for your thoughts and comments.
In order to solve that I would go with maven profiles and resource plugin maven resource plugin
You can have variables to the resource file name according to what you need (prod, dev, etc)

Excluding Resource while packaging in maven

I have a project with sample structure as :
jcr_root
|_apps
|_A
|_B
|_etc
|_A
|_B
What I need to do is while creating a package, I need to include either "apps/A & etc/A" or "apps/B & etc/B"
In my pom.xml, I tried something like :
<resources>
<resource>
<directory>src/main/content/jcr_root</directory>
<excludes>
<exclude>apps/A/**</exclude>
<exclude>etc/A/**</exclude>
</excludes>
</resource>
</resources>
But still both 'A' and 'B' under apps and etc get included while packaging. I'm using content-package-maven-plugin to build a package that would be deployed on CQ.
I tried putting entries in filter.xml but then it is used while deployment and not while packaging.
It seems, the include/exclude tags are not at all working. For testing, I tried:
<resources>
<resource>
<directory>src/main/content/jcr_root</directory>
<excludes>
<exclude>**/*.otf</exclude>
</excludes>
</resource>
</resources>
But still fonts.otf file was getting included in the packaged zip.
Some help or hints please. Let me know if any more info is required.
Many Thanks in Advance.
So finally I was able to create a package with the excluded resources.
The issue was not with the include/exclude tag(they always worked fine.)
The files after excluding some resource were copied to "target/classes" directory
Issue was that the maven-content-package plugin took the resource to package from original source directory rather than the "target/classes" directory created.Thus it always included everything. This is the default behaviour of maven-content-package plugin.
Thus I had to explicitly tell the plugin that you need to pick the resource to package from "target/classes".
<builtContentDirectory>${basedir}/target/classes</builtContentDirectory>
Please let me know if anyone needs more detail. Thanks for all your answers:)
Since you want to include jcr_root/app/A and jcr_root/etc/A
Please you try using this in POM.xml:
<project>
...
<resources>
<resource>
<directory>jcr_root</directory>
<includes>
<include>**/*A*</include>
</includes>
<excludes>
<exclude>**/*B*</exclude>
</excludes>
</resource>
<resources>
</project>
Similarly you can do it for getting jcr_root/app/B and jcr_root/etc/B.
Regards
Jyotsna

How to edit the directory structure in Maven?

I am using Maven project, when i create the Maven module of jar packaging, maven auto generates directory structue as src/main/java, src/main/resources, src/test/java and src/test/resources. Can I edit the above names as per my wish? Can I add new folders to the same parent? Also when i googled, I came to know abt super POM, can anybody suggest how to edit the same with the custom directory structure. I have configured sonatype maven to my eclipse from the link http://m2eclipse.sonatype.org/sites/m2e
Assuming you have a good reason to do this, you can rename the folders and indicate to maven what is the edited one by specifying the appropriate properties/sections in pom.xml of your project. I suppose m2e will pick up the changes once made to the pom.
The relevant section in your case would be (from the superpom)
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
If you want to add additional source folders or resources (not subfolders), then you can use build helper maven plugin. Again, not sure what m2e will do.

Maven project.build.directory

In Maven, what does the project.build.directory refer to? I am a bit confused, does it reference the source code directory or the target directory in the Maven project?
You can find those maven properties in the super pom.
You find the jar here:
${M2_HOME}/lib/maven-model-builder-3.0.3.jar
Open the jar with 7-zip or some other archiver (or use the jar tool).
Navigate to
org/apache/maven/model
There you'll find the pom-4.0.0.xml.
It contains all those "short cuts":
<project>
...
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
...
</build>
...
</project>
Update
After some lobbying I am adding a link to the pom-4.0.0.xml. This allows you to see the properties without opening up the local jar file.
It points to your top level output directory (which by default is target):
https://web.archive.org/web/20150527103929/http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide
EDIT: As has been pointed out, Codehaus is now sadly defunct. You can find details about these properties from Sonatype here:
http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html#resource-filtering-sect-project-properties
If you are ever trying to reference output directories in Maven, you should never use a literal value like target/classes. Instead you should use property references to refer to these directories.
project.build.sourceDirectory
project.build.scriptSourceDirectory
project.build.testSourceDirectory
project.build.outputDirectory
project.build.testOutputDirectory
project.build.directory
sourceDirectory, scriptSourceDirectory, and testSourceDirectory provide access to the source directories for the project. outputDirectory and testOutputDirectory provide access to the directories where Maven is going to put bytecode or other build output. directory refers to the directory which contains all of these output directories.
You can find the most up to date answer for the value in your project just execute the
mvn3 help:effective-pom
command and find the <build> ... <directory> tag's value in the result aka in the effective-pom. It will show the value of the Super POM unless you have overwritten.
Aside from #Verhás István answer (which I like), I was expecting a one-liner for the question:
${project.reporting.outputDirectory} resolves to target/site in your project.

Maven Script for include files in in jars

How do i include config folder in jar file.
let say i have one services project
services/src/config/adp/flt/get.flt
there is one flt file in config folder.
So i want to include that folder in services.jar using maven script
I have tried different solution copy-resources but it is copying that folder on target folder not in services.jar
Try the Maven Resource Plugin:
<project>
...
<build>
...
<resources>
<resource>
<directory> [your folder here] </directory>
</resource>
</resources>
...
</build>
...
</project>
Alternatively, you could move your files to a sub directory of Maven's default resource folder, src/main/resources. In that way, they get included automatically.

Resources