Issue
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.
<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.
Solution
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.
Answered By - Kyle Seaman
Answer Checked By - Candace Johnson (JavaFixing Volunteer)