Skip to content

Commit

Permalink
AVRO-3889: [Java][Build] Maven IDL Generation Modification Check (#2561)
Browse files Browse the repository at this point in the history
Check if the IDL source file has changed before regenerating the Java
classes to prevent unnecessary recompilation when using maven.

Co-authored-by: Brian Cullen <brianc@kahoot.com>
  • Loading branch information
briancullen and briankahoot committed Jan 3, 2024
1 parent 25222c3 commit 0a2323f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec

URLClassLoader projPathLoader = new URLClassLoader(runtimeUrls.toArray(new URL[0]),
Thread.currentThread().getContextClassLoader());
try (Idl parser = new Idl(new File(sourceDirectory, filename), projPathLoader)) {
File sourceFile = new File(sourceDirectory, filename);
try (Idl parser = new Idl(sourceFile, projPathLoader)) {

Protocol p = parser.CompilationUnit();
for (String warning : parser.getWarningsAfterParsing()) {
Expand All @@ -104,7 +105,7 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
compiler.addCustomConversion(projPathLoader.loadClass(customConversion));
}
compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"));
compiler.compileToDestination(null, outputDirectory);
compiler.compileToDestination(sourceFile, outputDirectory);
}
} catch (ParseException | ClassNotFoundException | DependencyResolutionRequiredException e) {
throw new IOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
*/
package org.apache.avro.mojo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import org.codehaus.plexus.util.FileUtils;
import org.junit.Test;

Expand All @@ -37,14 +41,17 @@ public class TestIDLProtocolMojo extends AbstractAvroMojoTest {

@Test
public void testIdlProtocolMojo() throws Exception {
// Clear output directory to ensure files are recompiled.
final File outputDir = new File(getBasedir(), "target/test-harness/idl/test/");
FileUtils.deleteDirectory(outputDir);

final IDLProtocolMojo mojo = (IDLProtocolMojo) lookupMojo("idl-protocol", testPom);
final TestLog log = new TestLog();
mojo.setLog(log);

assertNotNull(mojo);
mojo.execute();

final File outputDir = new File(getBasedir(), "target/test-harness/idl/test/");
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java",
"IdlUserWrapper.java", "IdlClasspathImportTest.java"));
assertFilesExist(outputDir, generatedFiles);
Expand All @@ -60,14 +67,17 @@ public void testIdlProtocolMojo() throws Exception {

@Test
public void testSetCompilerVelocityAdditionalTools() throws Exception {
// Clear output directory to ensure files are recompiled.
final File outputDir = new File(getBasedir(), "target/test-harness/idl-inject/test");
FileUtils.deleteDirectory(outputDir);

final IDLProtocolMojo mojo = (IDLProtocolMojo) lookupMojo("idl-protocol", injectingVelocityToolsTestPom);
final TestLog log = new TestLog();
mojo.setLog(log);

assertNotNull(mojo);
mojo.execute();

final File outputDir = new File(getBasedir(), "target/test-harness/idl-inject/test");
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java",
"IdlUserWrapper.java", "IdlClasspathImportTest.java"));

Expand All @@ -79,4 +89,37 @@ public void testSetCompilerVelocityAdditionalTools() throws Exception {
// The previous test already verifies the warnings.
assertFalse(log.getLogEntries().isEmpty());
}

@Test
public void testIdlProtocolMojoDoesntReplaceUpToDateFiles() throws Exception {
// Ensure that the IDL files have already been compiled once.
final IDLProtocolMojo mojo = (IDLProtocolMojo) lookupMojo("idl-protocol", testPom);
final TestLog log = new TestLog();
mojo.setLog(log);

assertNotNull(mojo);
mojo.execute();

// Remove one file to ensure it is recreated and the others are not.
final Path outputDirPath = Paths.get(getBasedir(), "target/test-harness/idl/test/");
final File outputDir = outputDirPath.toFile();

final Path idlPrivacyFilePath = outputDirPath.resolve("IdlPrivacy.java");
final FileTime idpPrivacyModificationTime = Files.getLastModifiedTime(idlPrivacyFilePath);
Files.delete(idlPrivacyFilePath);

final Path idlUserFilePath = outputDirPath.resolve("IdlUser.java");
final FileTime idlUserModificationTime = Files.getLastModifiedTime(idlUserFilePath);

mojo.execute();

// Asserting contents is done in previous tests so just assert existence.
final Set<String> generatedFiles = new HashSet<>(Arrays.asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java",
"IdlUserWrapper.java", "IdlClasspathImportTest.java"));
assertFilesExist(outputDir, generatedFiles);

assertTrue(idlPrivacyFilePath.toFile().exists());
assertEquals(Files.getLastModifiedTime(idlUserFilePath), idlUserModificationTime);
assertTrue(Files.getLastModifiedTime(idlPrivacyFilePath).compareTo(idpPrivacyModificationTime) > 0);
}
}

0 comments on commit 0a2323f

Please sign in to comment.