exec-maven-plugin中的Filechange之后,M2E触发增量构建

时间:2019-02-25 15:30:40

标签: maven m2e maven-exec-plugin

我们有两个带有消息文件的资源目录

<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>

这在console-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的引用?

0 个答案:

没有答案