Maven plugin-development - How to detect modified files?

266 Views Asked by At

I'm writing new maven plugins, I've successfully used StaleSourceScanner in a combination with SuffixMapping, but it is just not working with SingleTargetSourceMapping. Always all files are detected as modified.

To be more specific this will be a tool, that analyzes class files, so my source files are in ${project.build.outputDirectory}. I have created a timestamp file for the last run, to compare changes with.

Unfortunately StaleSourceScanner is not documented at all.

(Maven phase is: process-classes; staleMillis = 0 show in the debug)

Code is:

    private Set<File> findChangedFilesSimple() throws MojoExecutionException
    {
        HashSet<String> sourceIncludes =
            new HashSet<>(Arrays.asList("**/*.class"));
        Set sourceExcludes = Collections.EMPTY_SET;

        StaleSourceScanner sourceInclusionScanner =
            new StaleSourceScanner(
                staleMillis, sourceIncludes, sourceExcludes);

        sourceInclusionScanner.addSourceMapping(
            new SingleTargetSourceMapping(
                ".class", timeStampFile.getPath()));

        Set<File> effectedFiles;

        try
        {
            effectedFiles =
                sourceInclusionScanner.getIncludedSources(
                    classesDirectory, timestampDirectory);
        }
        catch (InclusionScanException e)
        {
            throw new MojoExecutionException(
                "Error scanning source directory: " + classesDirectory, e);
        }

        return effectedFiles;
    }
1

There are 1 best solutions below

0
On

Okay, already found the problem by analyzing SingleTargetSourceMapping source code:

  1. For the getIncludedSources, the targetDir should be a directory, where the timestampFile located in. (So far so good.)
  2. The timestampFile provided for the SingleTargetSourceMapping is the relative file name, relative to targetDir mentioned above. Thus, instead of timeStampFile.getPath(), timeStampFile.getName() should be used.

Fixed source code:

    private Set<File> findChangedFilesSimple() throws MojoExecutionException
    {
        HashSet<String> sourceIncludes =
            new HashSet<>(Arrays.asList("**/*.class"));
        Set sourceExcludes = Collections.EMPTY_SET;

        StaleSourceScanner sourceInclusionScanner =
            new StaleSourceScanner(
                staleMillis, sourceIncludes, sourceExcludes);

        sourceInclusionScanner.addSourceMapping(
            new SingleTargetSourceMapping(
                ".class", timeStampFile.getName()));

        Set<File> effectedFiles;

        try
        {
            effectedFiles =
                sourceInclusionScanner.getIncludedSources(
                    classesDirectory, timeStampFile.getParentFile());
        }
        catch (InclusionScanException e)
        {
            throw new MojoExecutionException(
                "Error scanning source directory: " + classesDirectory, e);
        }

        return effectedFiles;
    }