M2E запускает инкрементную сборку после обмена файлами в exec-maven-plugin - PullRequest
0 голосов
/ 25 февраля 2019

У нас есть два каталога ресурсов с файлами сообщений

<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); возможно ли получить ссылку на buildContext в maven-exec-plugin?

...