我们有两个包含消息文件的资源目录
<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>
过滤后,我们要合并目录并连接重复文件:
<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);
}
}
}
}
}
之后,resourcesMerged 文件夹被添加到 WAR 文件中
<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>
这在控制台 maven 上完美运行。对于 m2e,我们添加了以下配置:
<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>
现在的问题是,当生成合并文件时,会触发另一个增量构建,这会导致无限循环。我知道我可以设置 runOnIncremental=false 但是在更改文件时我总是要清理整个项目。我可以忍受,但不知何故,lesscss-maven-plugin(https://github.com/marceloverdijk/lesscss-maven-plugin)也在增量构建的目标文件夹中生成文件,但随后eclipse不会触发另一个构建事件,我怎样才能实现它呢?
我已经看到他们称之为buildContext.refresh(output);
是否可以在 maven-exec-plugin 中获得对 buildContext 的引用?