We have two resource directories with message files
<resource>
<directory>${basedir}/resources</directory>
<filtering>true</filtering>
<targetPath>${project.build.directory}/resourcesDefault</targetPath>
</resource>
<resource>
<directory>${basedir}/profiles/${additionalWebcontent}/java</directory>
<filtering>true</filtering>
<targetPath>${project.build.directory}/resourcesProfile</targetPath>
</resource>
After filtering we want to merge the directories and concat duplicate files:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>resourceMerge</id>
<goals>
<goal>java</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<mainClass>mavenProcessor.Resourcesmerger</mainClass>
<arguments>
<argument>${project.build.directory}/resourcesDefault</argument>
<argument>${project.build.directory}/resourcesProfile</argument>
<argument>${project.build.directory}/resourcesMerged</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
public class Resourcesmerger {
public static void main(String[] args) throws Throwable {
Path inputDir1 = new File(args[0]).toPath();
Path inputDir2 = new File(args[1]).toPath();
Path outputDir = new File(args[2]).toPath();
copyAppending(inputDir1, outputDir, false);
if (Files.exists(inputDir2)) {
copyAppending(inputDir2, outputDir, true);
}
}
private static void copyAppending(Path inputDir1, Path outputDir, boolean append) throws IOException, FileNotFoundException {
List<Path> defaultResources = Files.walk(inputDir1).collect(Collectors.toList());
for (Path path : defaultResources) {
if (Files.isRegularFile(path)) {
Path relativePath = inputDir1.relativize(path);
Path targetPath = outputDir.resolve(relativePath);
targetPath.getParent().toFile().mkdirs();
try (FileOutputStream fos = new FileOutputStream(targetPath.toFile(), append)) {
System.out.println("Merge " + path + " to " + targetPath);
System.out.flush();
Files.copy(path, fos);
}
}
}
}
}
After that the resourcesMerged folder is added to the WAR file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/resourcesMerged</directory>
</resource>
</webResources>
</configuration>
</plugin>
This works perfecly on console-maven. For m2e we added this configuration:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<versionRange>[1,)</versionRange>
<goals>
<goal>java</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
The problem is now that, when the merged files are generated another incremental build is triggered which leeds to a infinite loop. I know that i could set runOnIncremental=false but then i have always to clean the whole project when changing the file. I could live with that, but somehow the lesscss-maven-plugin (https://github.com/marceloverdijk/lesscss-maven-plugin) also generates files in the target folder on incremental build, but then eclipse does not trigger another build event, how can i achieve it, too?
I have seen that they call something like buildContext.refresh(output);
is it possible to get an reference to buildContext within maven-exec-plugin?