How can I configure maven resources plugin with a few copy tasks? - java

I have two copy tasks that I want to do with maven-resources-plugin. For example I need to copy config.yml from src/main/resources to root folder and to copy all folder contents from /src/main/resources/examples to src/examples.
root
/src
/main
/resources --> config.yml (to root)
/examples --> all folder contents (to /src/examples)
The only one solution I've found is this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/config.yml</include>
</includes>
</resource>
<!-- <resource>
<directory>src/main/resources/examples</directory>
</resource> -->
</resources>
</configuration>
</execution>
</executions>
</plugin>
but I can add only one destination folder.

You can create another execution with different ID and configuration:
<execution>
<id>copy-resources-2</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
...
</configuration>
</execution>

Related

Bind a file inside a maven built EAR

Below is my project structure.
I require to copy a file from the location \MainProject\src\non-packaged-resources to the \MainProject\sub-project2\sub-project2-web\src\main\resources location on building the MainProject.
Below is the build section of the pom file of my sub-project2-web but the file is not copied while building.
<build>
<finalName>sub-project2-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>**/.copyarea.db</warSourceExcludes>
<packagingExcludes>**/.copyarea.db</packagingExcludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When observing the build log, it skips the file being copied with the below error
skip non existing resourceDirectory
But the maven build gets successful.
Goal is to bind the file which exists inside the \MainProject\src\non-packaged-resources to all the ears (sub-project1.ear, sub-project2.ear) on building the maven project. First I am trying to test this in the sub-project2-web only.
Please advice on how to provide correct paths based on this requirement.
I am using maven 2.2.1 version (project is built on this.)
Define the paths with reference to your project base directory as follows.
<outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>
<directory>${project.basedir}/src/non-packaged-resources</directory>
Then add it in the pom.xml file in the maven-resources-plugin.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Based on the above answer I found the correct way to get the locations of the parent and the base directories. Below worked for me.
<build>
<finalName>sub-project2-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>**/.copyarea.db</warSourceExcludes>
<packagingExcludes>**/.copyarea.db</packagingExcludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.parent.parent.basedir}/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Maven resources plugin copy file

I'd like to copy eclipse.properties.ref as eclipse.properties if eclipse.properties does not exist. I'm using maven 3.2 and Java 7.
I tried the following, but not sure how to map the old file to new.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>test</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/src/test/resources</outputDirectory>
<resources>
<resource>
<directory>/src/test/resources</directory>
<includes>
<include>eclipse.properties.ref</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
You can achieve your goal using maven-antrun-plugin. It seems like copy does exactly is what you need.
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property
name="resources"
value="${project.basedir}/src/test/resources" />
<copy
overwrite="false"
file="${resources}/eclipse.properties.ref"
toFile="${resources}/eclipse.properties" />
</target>
</configuration>
</execution>
</executions>
NOTE
Also I'd suggest you to copy to ${project.build.testOutputDirectory} instead of ${project.basedir}/src/test/resources so it's will not hurt your version control system.

Maven - How to place a single resource file on a separate location from the other resource files?

So I created a windows batch file that i would like to include on my build using Maven. I placed it in the src/main/resources folder. When I do a mvn clean install, my resources end up on a folder named conf in the same directory as the jar file.
Basically I would like to have the windows batch file on the same directory as my jar, with my other resource files in the conf folder.
+ conf/
... my resource files
- myjar.jar
- myBatchFile.bat
here is a snippet of the maven plugin that does it:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.csv</include>
<!-- include my batch file -->
<include>**/*.bat</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
AS EXPECTED, my batch file went inside the conf folder.
Question: Is there a way to have the batch file sit beside my jar, while all other resource files inside conf?
Try to change your configuration to the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.csv</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-batch</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<!-- include my batch file -->
<include>**/*.bat</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
We are basically adding a further execution of the same plugin for the same phase which will take care of specific batch file and its copy while removing the batch inclusion from the previous execution.

Copy empty directory from test resources in Maven

Based on this comment I am trying to copy an empty directory from test resources folder in Maven based project to the test build output but with no luck. I have already been successfully using maven-resource-plugin for copying basic resources so I tried to add another execution part for test resources like this to my pom.xml:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resource</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-test-resource</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
<outputDirectory>${project.build.testSourceDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I also tried to define it in build section like this:
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
but it also didn't help.
All files and non-empty directories gets correctly copied but the single empty directory doesn't.
Thanks for any help or advice.
Finally, I solved it!
The problem is that element <includeEmptyDirs> was in the wrong place in the plugin section. it should be a part of plugin configuration, NOT part of execution configuration.
Also I changed goal of copy-test-resource to testResources and outputDirectory to ${project.build.testOutputDirectory}
So the correct plugin section is following:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
<executions>
<execution>
<id>copy-resource</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-test-resource</id>
<phase>package</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Maven-resources-plugin how to execute the same phase and goal with different configuration

I have many subfolder in my src/test/resources and what I want to do is filter each subfolder with a specific filter file which resides in src/test/filters:
I tried something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<resources>
<resource>
<outputDirectory>${basedir}/target/annonce</outputDirectory>
<directory>${basedir}/src/test/resources/annonce</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/src/test/filters/annonce/filter.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/profil</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${basedir}/src/test/resources/profil</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/src/test/filters/profil/filter.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
But I get the error that said that the id should be unique. I tried to use for the second execution copy-resources id and goal but it's not working, so I wonder if there is any ideas?
Simply change one of the ids
org.apache.maven.plugins
maven-resources-plugin
2.6
<execution>
<id>default-testAnnonce</id>
...
</execution>
<execution>
<id>default-testProfil</id>
...
</execution>
</executions>

Categories