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.incremental.IncrementalBuildHelperRequest;
26  
27  /**
28   * Prevents the following scenario:
29   *
30   * 1. Contract is added
31   *
32   * 2. Derived file is generated
33   *
34   * 3. Contract is deleted
35   *
36   * 4. Derived file still exists
37   */
38  class LeftOverPrevention {
39  
40  	private final IncrementalBuildHelper incrementalBuildHelper;
41  
42  	private final File generatedDirectory;
43  
44  	LeftOverPrevention(File generatedDirectory, MojoExecution mojoExecution, MavenSession session)
45  			throws MojoExecutionException {
46  		this.generatedDirectory = generatedDirectory;
47  		this.incrementalBuildHelper = new IncrementalBuildHelper(mojoExecution, session);
48  		this.incrementalBuildHelper
49  				.beforeRebuildExecution(new IncrementalBuildHelperRequest().outputDirectory(generatedDirectory));
50  	}
51  
52  	void deleteLeftOvers() throws MojoExecutionException {
53  		if (generatedDirectory.exists()) {
54  			incrementalBuildHelper
55  					.afterRebuildExecution(new IncrementalBuildHelperRequest().outputDirectory(generatedDirectory));
56  		}
57  	}
58  
59  }