|
19 | 19 | import org.apache.maven.plugin.*; |
20 | 20 | import org.apache.maven.plugins.annotations.*; |
21 | 21 | import org.apache.maven.plugins.annotations.Mojo; |
| 22 | +import org.apache.maven.shared.utils.io.DirectoryScanner; |
22 | 23 |
|
23 | 24 | import java.io.File; |
24 | 25 | import java.util.*; |
@@ -49,8 +50,55 @@ public void execute () throws MojoExecutionException, MojoFailureException { |
49 | 50 | System.out.println (id); |
50 | 51 |
|
51 | 52 | try { |
52 | | - new ProcessorRunner (id, createProperties ()) |
53 | | - .run (); |
| 53 | + Map<String, Object> properties = createProperties (); |
| 54 | + |
| 55 | + DirectoryScanner sourceScanner = new DirectoryScanner (); |
| 56 | + File sourceRoot = apiPath.getParentFile (); |
| 57 | + sourceScanner.setBasedir (sourceRoot); |
| 58 | + sourceScanner.setIncludes ("**/*.yaml", "**/*.yml"); |
| 59 | + sourceScanner.scan (); |
| 60 | + String[] sourceFiles = sourceScanner.getIncludedFiles (); |
| 61 | + |
| 62 | + long lastModified = 0; |
| 63 | + for (String source : sourceFiles) { |
| 64 | + File current = new File (sourceRoot, source); |
| 65 | + |
| 66 | + if (current.exists () && current.lastModified () > lastModified) { |
| 67 | + lastModified = current.lastModified (); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + DirectoryScanner targetScanner = new DirectoryScanner (); |
| 73 | + File targetDir = new File((String) properties.get (TARGET_DIR)); |
| 74 | + targetScanner.setBasedir (targetDir); |
| 75 | + targetScanner.setIncludes ("**/*", "**/*"); |
| 76 | + targetScanner.scan (); |
| 77 | + String[] targetFiles = targetScanner.getIncludedFiles (); |
| 78 | + |
| 79 | + boolean outdated = false; |
| 80 | + if (targetFiles.length == 0) { |
| 81 | + outdated = true; |
| 82 | + } |
| 83 | + |
| 84 | + for (String target : targetFiles) { |
| 85 | + File current = new File (targetDir, target); |
| 86 | + |
| 87 | + if (current.exists () && current.lastModified () < lastModified) { |
| 88 | + outdated = true; |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (outdated) { |
| 94 | + getLog().info( "Changes detected - generating target files!" ); |
| 95 | + |
| 96 | + new ProcessorRunner (id, properties) |
| 97 | + .run (); |
| 98 | + |
| 99 | + } else { |
| 100 | + getLog().info( "Nothing to process - all generated target files are up to date." ); |
| 101 | + } |
54 | 102 |
|
55 | 103 | } catch (Exception e) { |
56 | 104 | throw new MojoExecutionException ("openapi-processor-" + id + " execution failed!"); |
|
0 commit comments