Adding Templates in Maven - maven

I have a folder src/resource/templates with .xlsx and .docx files. I am using maven and how is it possible to copy files from that folder into jar file?

I found an answer using maven copy.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/classes/report/resource/Templates</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/report/resource/Templates</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

You can add more resource directories to maven project. See http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html
For example:
<project>
...
<build>
...
<resources>
<resource>
<directory>src/resource/templates</directory>
</resource>
</resources>
...
</build>
...
</project>
EDITED added clarification:
If you now have file src/resource/templates/example.xlsx it should be on jar's root.
File src/resource/templates/report/resource/Templates/example.xlsx would be copied to jar as file report/resource/Templates/example.xlsx

Related

generated file in the wrong place

I'm using the maven resources plugin to generate a plain file via filtering within a war file. I have the template file with variables in a the folder src/main/webapp/app
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp/app</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
the problem is that the generated file is in the WEB-INF/classes folder and I need it to be at the app folder at the root of the war.
The war structure should be
*.jsf
app/<generated_file>
WEB-INF/
How can I do it?
You have to add a copy-resources goal if you want to copy something to another structure during your build, with that you can point to a custom output path:
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
This can be found here in the documentation.
With the comments I could finally get the solution. It was simply to set the outputDirectory parameter to src/main/webapp/app and put the template in another place outside src/main/webapp. In my case, I put it in a folder src/main/jnlp. So finally this was the solution.. hope it helps others
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp/app</outputDirectory>
<resources>
<resource>
<directory>src/main/jnlp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

maven override/replace file into WEB-INF/classes

I want to override/replace my spring config xml file with specified location file while package as war.
And I do not want to use filter plugin (filter plugin must to use dolloar placeholder, it will run with error while local deploy), is there any plugin or setting I can use to do this?
project structure as follow:
ROOT
----config
----prd
----spring-servlet.xml
----web.xml
----src
----main
----java
----resources
----spring-servlet.xml
----webapp
....
My pom.xml is like as follow:
...
<profiles>
<profile>
<id>prd</id>
<properties>
<filterDir>prd</filterDir>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webXml>${basedir}/config/${filterDir}/web.xml</webXml>
<webResources>
<resource>
<directory>config/${filterDir}</directory>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>mvc-dispatcher-servlet.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
run maven use :mvn clean package -P prd.
Will work fine if not set tartgetPath, file copied to web root.
You can use copy-resources of maven to do this.
Example maven code should be like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>your-target-directory</outputDirectory>
<resources>
<resource>
<directory>source-directory</directory>
<!--You can also mention files too-->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Read more about copy-resources for correct implementation.

Maven-resources-plugin won't copy .metadata folder

I'm trying to copy a folder or following structure with maven-resources-plugin:
root
|- .metadata
|- Project
\- .gitignore
Project directory and .gitignore files are copied, but .metadata directory is left out for some reason.
How do I copy all contents of root folder?
Here is configuration I tried:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/aut-ws</outputDirectory>
<useBuildFilters>false</useBuildFilters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>metadata</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<resources>
<resource>
<directory>H:\rcptt\workspaces\root</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
You're looking for the configuration parameter addDefaultExcludes. See the documentation page.
So your configuration section should look like the following:
<configuration>
<outputDirectory>${project.build.directory}/aut-ws</outputDirectory>
<addDefaultExcludes>false</addDefaultExcludes>
...
<resources>
<resource>
<directory>H:\rcptt\workspaces\root</directory>
</resource>
</resources>
</configuration>
If this was relating to the maven-assembly-plugin then I had this problem, and had to use the useDefaultExcludes property (recent versions of the plugin only); by default it is true and it needs to be set to false to include directories like .metadata. This doesn't seem to be applicable to maven-resources-plugin though, or it might just not be a documented property.
My first fix attempt would be trying modifying the resources element.
<resources>
<resource>
<directory>H:\rcptt\workspaces\root</directory>
<includes>
<include>**/*</include>
<include>**/.*</include>
</includes>
</resource>
</resources>
Also, if you comment out the <nonFilteredFileExtensions> element, does it work?
Edit to show full plugin configuration that works with Maven 3.2.2, Resources plugin 2.7, on both Windows 7 and RedHat Linux. Command for testing is mvn validate.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/testing123</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
<include>**/.*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Just add addDefaultExcludes as #Matthew Wise said.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
By default files like .gitignore, .cvsignore etc. are excluded which
means they will not being copied. If you need them for a particular
reason you can do that by settings this to false.
Documentation: https://maven.apache.org/plugins/maven-resources-plugin/copy-resources-mojo.html#addDefaultExcludes

Maven Resource Plugin Copying Files

I currently have some Maven projects which when I install the project I need to copy all files from the conf folder to the target folder.
|-Project
|--src
|--conf <--FROM HERE -->
|--lib
|--target <--TO HERE-->
I have attempted this in the pom.xml to no avail. What am I doing wrong? My plugin part of the pom.xml is below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>${basedir}/conf</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
Your problem is that you copy the resources in the phase install. In that phase your target archive is already built and copied to your local repository. See the Maven lifecycle. You'll propably want to do it in the process-resources phase.

Maven: how to get a war package with resources copied in WEB-INF?

when I create a war package with maven, files and directories under the directory "src/main/resources" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get them copied in /WEB-INF?
thanks,
rand
UPDATE:
in my pom now I use this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>war</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>myapp/target/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and I launch mvn with:
mvn -Dmaven.test.skip=true clean package resources:copy-resources
but I got:
[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'
[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<outputDirectory>VALUE</outputDirectory>
</configuration>.
[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<resources>VALUE</resources>
</configuration>.
I'm using maven 2.2 and the snippet basically is the same of the documentation
any idea?
either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory.
resource plugin
EDIT:
This configuration is working for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.
Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml
This configuration is working add plugin pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<!--copy resource file location-->
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
</webResources>
<!--location for add file-->
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
</configuration>
</plugin>

Resources