Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

Maven plugin-development - How to detect modified files?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...