Maven and maven-resources-plugin [closed] - maven

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I would like to use maven-resources-plugin to copy resource to another directory.
My resources directory have this structure :
log4j.xml
xml
|_ resource-1
|_ resource-n
I would like to copy only log4.xml to output directory. Here is my plugin code :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${glassfish.conf}</outputDirectory>
<resources>
<resource>
<directory>${conf.location}</directory>
<includes>
<include>**/log4j.xml</include>
</includes>
<excludes>
<exclude>**/xml/*</exclude>
</excludes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-log4j-to-glassfish</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
But all is copied to output directory (log4j.xml and xml directory).
I tried
<resource>
<directory>${conf.location}</directory>
<excludes>
<exclude>**/xml/*</exclude>
</excludes>
</resource>
Or
<resource>
<directory>${conf.location}</directory>
<includes>
<include>**/log4j.xml</include>
</includes>
</resource>
Even
<resource>
<directory>${conf.location}</directory>
<excludes>
<exclude>**/*</exclude>
</excludes>
</resource>
But all the content of directory is included...What is the problem ?
Thank you

To answer Andrew Logvinov :
With a plugin like that :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-log4j-to-glassfish</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${glassfish.conf}</outputDirectory>
<resources>
<resource>
<directory>${conf.location}</directory>
<includes>
<include>log4j.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
it's work well, only log4j.xml is copied.
With this plugin configuration now :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-log4j-to-glassfish</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${glassfish.conf}</outputDirectory>
<resources>
<resource>
<directory>${conf.location}</directory>
<includes>
<include>log4j.xml</include>
</includes>
</resource>
</resources>
</configuration>
</plugin>
all my files are copied.
I check xsd here are configuration block can be inside plugin or inside execution tag so I don't know if this is a plugin bug or if this misunderstanding of me.

Related

Maven: Filtering files from unpacked-dependencies

I have a script that is unpacked from a dependency that contains an undefined variable. My goal is to set a property in my maven project that defines the variable and filter the file so that the variable in the script is replaced by the property.
I have tried this:
<build>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</build>
However, this is called before the file is unpacked. So there is nothing to filter yet at the time this runs.
I also tried:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter script</id>
<phase>generate-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, its looking inside of src/main/resources instead of the target directory that I provided.
The only thing I can get KINDA working is this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter script</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dir2</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This DOES filter the file, but now the filtered file exists in a different directory than I want it to. I've tried making the outputDirectory the same as the directory it's copying from and just overwrite the original, but it doesn't work. I feel like there is a simple way of doing this that I'm not noticing.
I'm also constrained from modifying the artifact that the dependency is coming from.
I found solution, although it is a bit messy and not my favorite.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter script</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/filtered_files</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</configuration>
</execution>
</executions>
</plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>Create filter dir</id>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<mkdir dir="${project.build.directory}/filtered_files" />
</target>
</configuration>
</execution>
<execution>
<id>copy filtered files to original path</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<move todir="${project.build.directory}/dir1" >
<fileset dir="${project.build.directory}/filtered_files"/>
<include name="fileToBeFiltered.sh" />
</fileset>
</move>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This essentially creates a temp directory, uses the 3rd approach described in the original question, then moves the filtered files from the temp directory back to the original path, overwriting the unfiltered file.
Again, not my favorite approach, but it works.

Maven avoid certain files from being copied to test-classes

I'm trying to avoid that certain file types don't end copied in target/test-classes. The code I have right now in my pom file but it's not working is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/test-classes
</outputDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.nc</exclude>
</excludes>
</testResource>
</testResources>
</configuration>
</execution>
</executions>
</plugin>
I was finally able to do what I wanted with:
<execution>
<id>default-testResources</id>
<phase>test-compile</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/test/resources </directory>
<excludes>
<exclude>**/*.nc</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
With this code, which is very similar to the one I posted in the question, and by changing the maven version in my eclipse settings from 3.3.3 to 3.3.9 I'm able to avoid the files that I wanted to be copied to target/test-classes

How to include particular file from a folder in maven install target directory?

Directory structure is
conf
axis2.aar
com
lib
META-INF
axis2.xml
build.xml
common-logging.properties
log4j.properties
*.class
I want that only axis2.xml should be included in axis2.aar in target folder when doining maven install. I have tried many include and exclude combinations but nothing is
working. After doing maven install, It is taking whole axis2.aar contents. I also tried filtering by setting it to true.Please suggest some solution.
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CUDB_HSS</groupId>
<artifactId>CUDB_HSS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>wsdl</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>conf/axis2.aar</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>never</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-aar-maven-plugin</artifactId>
<version>1.6.4</version>
<extensions>true</extensions>
<executions>
<execution>
<id>cudb-hss-aar</id>
<phase>package</phase>
<goals>
<goal>aar</goal>
</goals>
<configuration>
<aarDirectory>conf/axis2.aar</aarDirectory>
<aarName>axis2</aarName>
<filesets>
<fileset>
<directory>conf/axis2.aar</directory>
<outputDirectory>target</outputDirectory>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>cudb-hss</id>
<phase>package</phase>
<configuration>
<classifier>client1</classifier>
<finalName>cudb-hss</finalName>
<includes>
<include>com/accenture/**/*.class</include>
<include>macro_CUDB_Accenture_HSS.class</include>
<include>**/com/accenture/il/interfaces/cudb/hss/conf/jaxb/**/*.properties</include>
<include>**/com/accenture/il/interfaces/cudb/hss/error/bean/jaxb/**/*.properties</include>
<include>**/*.properties</include>
</includes>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>cudb-hss-wsdl</id>
<phase>package</phase>
<configuration>
<classifier>client2</classifier>
<finalName>cudb-hss-wsdl</finalName>
<includes>
<include>com/ericsson/**/*.class</include>
<include>**/com/ericsson/**/jaxb.properties</include>
</includes>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The answer to your question can be:
<fileset>
<directory>conf/axis2.aar</directory>
<outputDirectory>target</outputDirectory>
<includes>
<include>axis2.xml</include>
</includes>
</fileset>

Maven resource plugin expose an image from a different path

I have an immage accessible from /resources/gfx/loading.gif
I would like to have it accessible from /img/immage.gif
I tried with maven-resources-plugin with the following config, but actualy nothing happens.
I'm curretnly building the app from eclipse.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-loading-status</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/src/main/webapp/img</outputDirectory>
<resources>
<resource>
<directory>/src/main/resources/gfx</directory>
<includes>
<include>loading.gif</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Assuming you are building a WAR, you could configure the maven-war-plugin to include additional web resources:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>/src/main/resources/gfx</directory>
<includes>
<include>loading.gif</include>
</includes>
<targetPath>img</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

How to loop the files in a directory, and do some tasks based on each filename?

There is a properties directory in my project:
properties
- project_dev.properties
- project_test.properties
- project_prod.properties
- project_other.properties
Which defines some different values for different enviroments. And there is also a template directory, which contains some configuration file templates with placeholders.
What I want to do is to loop all the files in properties, and combine each one to the template directory to generate the final configuration files, which are under different directories based the file name.
So it will be:
target
- configurations
- dev
- ... some files
- test
- ... some files
- prod
- ... some files
- other
- ... some files
What I'm doing now is to use the maven-resources-plugin, and define several executions for each of the files, and hard-coded the names.
It looks like:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<!--- for dev ---->
<execution>
<id>dev-filter</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${basedir}/target/conf/dev</outputDirectory>
<useDefaultDelimiters>false</useDefaultDelimiters>
<resources>
<resource>
<directory>${basedir}/conf/template</directory>
<filtering>true</filtering>
<includes>
<include>channel/*</include>
<include>rule-config/*</include>
<include>server/*.properties</include>
</includes>
</resource>
</resources>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
<filters>
<filter>${basedir}/properties/project_dev.properties</filter>
</filters>
</configuration>
</execution>
<!--- for prod ---->
<execution>
<id>prod-filter</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${basedir}/target/conf/prod</outputDirectory>
<useDefaultDelimiters>false</useDefaultDelimiters>
<resources>
<resource>
<directory>${basedir}/conf/template</directory>
<filtering>true</filtering>
<includes>
<include>channel/*</include>
<include>rule-config/*</include>
<include>server/*.properties</include>
</includes>
</resource>
</resources>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
<filters>
<filter>${basedir}/properties/project_prod.properties</filter>
</filters>
</configuration>
</execution>
<!--- for test --->
...
You can see I'm copying the execution block for each file, so there will be many duplicated code with small differences.
I wonder is there any solution to make things simpler? I tried to find a way to loop the files under properties and do the filter task for each of them, but not sure how to do it.
A simple solution for such purposes is the Iterator-maven-plugin
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>executor</goal>
</goals>
<configuration>
<items>
<item>dev</item>
<item>test</item>
<item>prod</item>
</items>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<goal>copy-resources</goal>
<configuration>
<outputDirectory>${basedir}/target/conf/#item#</outputDirectory>
<useDefaultDelimiters>false</useDefaultDelimiters>
<resources>
<resource>
<directory>${basedir}/conf/template</directory>
<filtering>true</filtering>
<includes>
<include>channel/*</include>
<include>rule-config/*</include>
<include>server/*.properties</include>
</includes>
</resource>
</resources>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
<filters>
<filter>${basedir}/properties/project_#item#.properties</filter>
</filters>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
The documentation contains also an example which is very similar to your needs.
Apart from that i would suggest to use the following properties in your pom file:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
than you don't need to define the encoding separately for the maven-resources-plugin.

Resources