Multiple Sources directories using maven-remote-resources-plugin - maven

I am trying to use maven-remote-resources-plugin to bundle resources from one of my projects. The problem I have is that i wish to bundle resources from two different locations:
src/main/java/es/ws/handler.xml
src/main/resources/myResources/*
I could do something like this
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>${maven-remote-resource.version}</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<resourcesDirectory>${basedir}/src</resourcesDirectory>
<includes>
<include>**/myResources/**</include>
<include>**/handler.xml</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
but this would export me the resources in the following manner
main/java/es/ws/handler.xml
main/resources/myResources/image.png
main/resources/myResources/novel.txt
and I would like them
handler.xml
myResources/image.png
myResources/novel.txt
So my question is: Is there a way to define multiple resourcesDirectory in order to select each folder separately and avoid having the full path

Use 2 execution block, each with a unique id. Use one block for the handler, the other for the myResources.
However, since you use ${basedir}, I think you actually want to use the maven-resources-plugin
See also http://maven.apache.org/pom.html#Resources

if you need to add more than one resource then you have to add another block of resources <execution>. Here is a complete sample.
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/static
</outputDirectory>
<resources>
<resource>
<directory>${basedir}/../frontend/build</directory>
</resource>
</resources>
</configuration>
</execution>
Then if you need to add a specific file than add like the following.
<execution>
<id>copy-index-page</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/resources/templates
</outputDirectory>
<resources>
<resource>
<directory>${basedir}/../frontend/build</directory>
<includes>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>

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.

can you provide overwrite on a per resource basis using the maven-resources-plugin?

So I'm having a problem with sharing a resources root because of IntelliJ, We have some resources we already add with the resource plugin, some of these resources I do not want to overwrite for performance reasons. In other cases I would like them overwritten So we don't forget we have to delete them if we change them. Can I just specify (mostly copy-pasta-ed from the official documentation
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<overwrite>true</overwrite> <!-- will this work -->
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
in the resources I want to overwrite? really this question is simple Can I put overwrite directly on the resource? Will it work?

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>

Maven store MD5 checksum of file and file size in properties then filter another file

I am wondering if there is a way in Maven to calculate the MD5 checksum and size of a file, put them into properties then use those properties to filter (text replace) parameters in another file. I am trying to generate a config file for Advanced Installer before I run it.
After spending a while googling for ways to do it with Maven I decided to look into using the antrun plugin. I googled for both features and the first link of both solved the problem. Seems like antrun is a great way to script most things in Maven.
My antrun configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<configuration>
<exportAntProperties>true</exportAntProperties>
</configuration>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="my_path" value="some path"/>
<length file="${my_path}" property="file.size"/>
<checksum file="${my_path}" property="file.md5"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
Maven resources plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>file1</include>
<include>file2</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
Which works great from the command line but for some reason the properties are not resolved in Intellij. I've posted another question for that.

maven run test for each classifier

I have a maven war project that configure to build for multiple envs using multiple classifier. Here is how i configure the plugin for dev and test envs:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>package-dev</id>
<phase>package</phase>
<configuration>
<classifier>dev</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory>
<webResources>
<resource>
<directory>src/env/dev</directory>
</resource>
</webResources>
</configuration>
<goals>
<goal>war</goal>
</goals>
</execution>
<execution>
<id>package-test</id>
<phase>package</phase>
<configuration>
<classifier>test</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-test</webappDirectory>
<webResources>
<resource>
<directory>src/env/test</directory>
</resource>
</webResources>
</configuration>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
However, I want my test to execute on each of the classifier based on the property files that are overridden for each classifier to make sure all the test will pass in each environment. How can I do that?
Thanks,
Sean

Resources