Skip to content

Commit

Permalink
AVRO-3889: [Java][Build] Maven IDL Generation Modification Check (#2564)
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 <[email protected]>
  • Loading branch information
briancullen and briankahoot authored Jan 3, 2024
1 parent c5523dc commit 1dbfef0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -84,7 +85,8 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
Thread.currentThread().setContextClassLoader(projPathLoader);
try {
IdlReader parser = new IdlReader();
IdlFile idlFile = parser.parse(sourceDirectory.toPath().resolve(filename));
Path sourceFilePath = sourceDirectory.toPath().resolve(filename);
IdlFile idlFile = parser.parse(sourceFilePath);
for (String warning : idlFile.getWarnings()) {
getLog().warn(warning);
}
Expand All @@ -109,7 +111,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(sourceFilePath.toFile(), outputDirectory);
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package org.apache.avro.mojo;

import java.io.File;
import java.util.Collections;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -37,16 +40,22 @@ public class TestIDLMojo extends AbstractAvroMojoTest {
private File injectingVelocityToolsTestPom = new File(getBasedir(),
"src/test/resources/unit/idl/pom-injecting-velocity-tools.xml");

private File incrementalCompilationTestPom = new File(getBasedir(),
"src/test/resources/unit/idl/pom-incremental-compilation.xml");

@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 IDLMojo mojo = (IDLMojo) lookupMojo("idl", 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<>(
asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java", "IdlUserWrapper.java"));
assertFilesExist(outputDir, generatedFiles);
Expand All @@ -60,14 +69,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<>(
asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java", "IdlUserWrapper.java"));

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

@Test
public void testIDLProtocolMojoSupportsIncrementalCompilation() throws Exception {
// Ensure that the IDL files have already been compiled once.
final IDLMojo mojo = (IDLMojo) lookupMojo("idl", incrementalCompilationTestPom);
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-incremental/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<>(
asList("IdlPrivacy.java", "IdlTest.java", "IdlUser.java", "IdlUserWrapper.java"));
assertFilesExist(outputDir, generatedFiles);

assertTrue(idlPrivacyFilePath.toFile().exists());
assertEquals(Files.getLastModifiedTime(idlUserFilePath), idlUserModificationTime);
assertTrue(Files.getLastModifiedTime(idlPrivacyFilePath).compareTo(idpPrivacyModificationTime) > 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>avro-parent</artifactId>
<groupId>org.apache.avro</groupId>
<version>1.12.0-SNAPSHOT</version>
<relativePath>../../../../../../../../../pom.xml</relativePath>
</parent>

<artifactId>avro-maven-plugin-test</artifactId>
<packaging>jar</packaging>

<name>testproject</name>

<build>
<plugins>
<plugin>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>idl</id>
<goals>
<goal>idl-protocol</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>${basedir}/src/test</sourceDirectory>
<outputDirectory>${basedir}/target/test-harness/idl-incremental</outputDirectory>
<stringType>String</stringType>
<project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub"/>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>${parent.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</project>

0 comments on commit 1dbfef0

Please sign in to comment.