View Javadoc
1   /*
2    * Copyright 2013-2020 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.cloud.contract.maven.verifier;
18  
19  import java.io.File;
20  
21  import org.apache.maven.execution.MavenSession;
22  import org.apache.maven.plugin.MojoExecution;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.shared.incremental.IncrementalBuildHelper;
25  import org.apache.maven.shared.utils.io.DirectoryScanner;
26  
27  final class ChangeDetector {
28  
29  	private ChangeDetector() {
30  	}
31  
32  	static boolean inputFilesChangeDetected(File contractsDirectory, MojoExecution mojoExecution, MavenSession session)
33  			throws MojoExecutionException {
34  		if (session.getGoals().contains("clean")) {
35  			return true;
36  		}
37  		IncrementalBuildHelper incrementalBuildHelper = new IncrementalBuildHelper(mojoExecution, session);
38  
39  		DirectoryScanner scanner = incrementalBuildHelper.getDirectoryScanner();
40  		scanner.setBasedir(contractsDirectory);
41  		scanner.scan();
42  		boolean changeDetected = incrementalBuildHelper.inputFileTreeChanged(scanner);
43  		if (scanner.getIncludedFiles().length == 0) {
44  			// at least one input file must exist to consider changed/unchanged
45  			// return true to skip incremental build and make visible no input file at all
46  			return true;
47  		}
48  		return changeDetected;
49  	}
50  
51  }