1

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

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.