Maven: Filtering files from unpacked-dependencies - maven

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.

Related

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

Maven does not copy resource files

I have a maven web application project that follows the maven standard directory layout so I have my resource files placed in src/main/resources/.
As this follows the standard I was expecting maven to automatically add the resource files to /WEB-INF/classes of the web application during the build but no files are copied.
I have to add the following lines to the build section of the pom file to get maven to copy the files:
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
My question is: is this supposed to be necessary?
I was wondering if any of the build section plugins I use could somehow interfere with the copying of the resources. Are there any plugins that are known to do this?
The build section of my pom file is here:
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>${tests.to.include}</include>
</includes>
<excludes>
<exclude>${tests.to.exclude}</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/ITSelenium*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add extra source directories</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add extra test directories</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add integration test sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>res</warName>
<webResources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
</plugins>
</build>
Any help or information will be much appreciated.

reading properties file into maven pom.xml not working

I need to use values from properties file in maven pom.xml, so i used properties-maven-plugin to read my properties file as follows
pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
qura.properties file contains something like this..
config.file.path = resources/python/config/test.py
I need use this config.file.path variable in resource element of pom.xml
pom.xml
<resources>
<resource>
<directory>${basedir}/multilang/</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
<resources>
But the value for ${config.file.path} is not taking up from qura.properties file and I couldn't find test.py file in jar.
what I'm doing wrong in this code?
Thanks in Advance
try using version 1.0.0 and removing spaces around equal sign in properties file.
eg:
key=value
IMO, it does not matter whether you put the spaces or not around equal sign in properties file. You may need to check if the ${config.file.path} exists in the directory specified by ${basedir}/multilang
The below snippet works for me.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
</resources>
</configuration>
<inherited></inherited>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

How to unpack a war file copied from local directory using Maven Resources Plugin?

I am trying integrating CAS server with a Spring application. (I have configured CAS locally, in my Tomcat server and it is working properly). Using Maven Resources Plugin I copied the cas.war file to my Web app's target folder. But I need to unpack that cas.war file in order to overwrite some files that I have modified.
How can I unpack this war file which I have copied using Maven Resources Plugin`?
Here is my pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>cas</warName>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${maven-jetty-plugin.version}</version>
<configuration>
<webApp>
<contextPath>/cas</contextPath>
</webApp>
</configuration>
</plugin>
<!--Copy plugin-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<!--copy cas.war file-->
<execution>
<id>copy-initial-cas.war</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas</outputDirectory>
<resources>
<resource>
<directory>${CAS.LOCATION}/target</directory>
<includes>
<include>cas.war</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying deployerConfigContext file-->
<execution>
<id>copy-deployerConfigContext</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>deployerConfigContext.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying web.xml file-->
<execution>
<id>copy-web.xml</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>web.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying casdatabase.properties file-->
<execution>
<id>copy-casdatabase.properties</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>casdatabase.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--unpacking plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>unzip-war</id>
<phase>install</phase>
<configuration>
<tasks>
<echo message="unpack cas.war" />
<unzip src="${basedir}/target/cas/cas.war" dest="${basedir}/target/cas" overwrite="true"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
If you wish to override resources inside a war, you should be using the maven overlay method.

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