Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MCOMPILER-561] - Add new rebuild detection for bytecode changes #219

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/it/MCOMPILER-561/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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
#
# http://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.

invoker.debug = false
invoker.java.version = 11+
invoker.goals = compile -Dmaven.compiler.release=8 -Dmaven.compiler.showCompilationChanges=true -ntp
invoker.goals.2 = compile -Dmaven.compiler.release=11 -Dmaven.compiler.showCompilationChanges=true -ntp
invoker.goals.3 = compile -Dmaven.compiler.release=9 -Dmaven.compiler.showCompilationChanges=true -ntp
42 changes: 42 additions & 0 deletions src/it/MCOMPILER-561/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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

http://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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>MCOMPILER-561</artifactId>
<name>MCOMPILER-561</name>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</build>
</project>
34 changes: 34 additions & 0 deletions src/it/MCOMPILER-561/src/main/java/myproject/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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
*
* http://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.
*/
package myproject;

/**
* The classic Hello World App.
*/
public class HelloWorld {

/**
* Main method.
*
* @param args Not used
*/
public static void main(String[] args) {
System.out.println("Hi!");
}
}
27 changes: 27 additions & 0 deletions src/it/MCOMPILER-561/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/*
* 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
*
* http://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.
*/

def log = new File(basedir, 'build.log').text

assert log.count('[INFO] Recompiling the module because of changed source code.') == 1
assert log.count('[INFO] Recompiling the module because of bytecode version changed.') == 2

assert log.count('from 8 to 11') == 1
assert log.count('from 11 to 9') == 1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
Expand All @@ -43,6 +44,8 @@
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -80,6 +83,7 @@
import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
import org.codehaus.plexus.languages.java.version.JavaClassfileVersion;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.RepositorySystem;
Expand Down Expand Up @@ -906,12 +910,17 @@ public void execute() throws MojoExecutionException, CompilationFailureException
String inputFileTreeChanged = hasInputFileTreeChanged(incrementalBuildHelper, sources)
? "added or removed source files"
: null;
Supplier<String> bytecodeChanged = () -> hasBytecodeChanged() ? "bytecode version changed" : null;

// Get the first cause for the rebuild compilation detection.
String cause = Stream.of(immutableOutputFile, dependencyChanged, sourceChanged, inputFileTreeChanged)
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
.orElseGet(() -> Stream.of(bytecodeChanged)
.map(Supplier::get)
.filter(Objects::nonNull)
.findFirst()
.orElse(null));

if (cause != null) {
getLog().info("Recompiling the module because of "
Expand Down Expand Up @@ -1842,6 +1851,48 @@ private boolean hasInputFileTreeChanged(IncrementalBuildHelper ibh, Set<File> in
return inputTreeChanges.hasChanged();
}

private static final int MAX_FILE_WALK_LIMIT = 20;

/**
* Performs a check on compiled class files to ensure that the bytecode version
* hasn't changed between runs.
*
* <p>This is limited to check a maximum of {@link #MAX_FILE_WALK_LIMIT}.
*
* @return true if a bytecode version differs from the actual release version.
*/
private boolean hasBytecodeChanged() {
String currentVersion = getVersionRelease();
JavaVersion javaVersion = JavaVersion.parse(currentVersion).asMajor();

try (Stream<Path> walk = Files.walk(getOutputDirectory().toPath())) {
Map<Path, JavaClassfileVersion> pathVersionMap = walk.filter(file -> "class"
.equals(FileUtils.extension(file.getFileName().toString())))
.limit(MAX_FILE_WALK_LIMIT)
.collect(Collectors.toMap(Function.identity(), JavaClassfileVersion::of));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of checking bytecode of classes we can store current JDK version used to compile and next time verify if is the same.

We also should add an IT for multi-releases compilation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a really good approach, I'm making complex bytecode checks when simply storing the current JDK, and verifying next time would do the trick.

Closing this in favor of that approach (will open a new PR later).

for (Map.Entry<Path, JavaClassfileVersion> entry : pathVersionMap.entrySet()) {
Path path = entry.getKey();
JavaClassfileVersion classFileVersion = entry.getValue();
JavaVersion javaFileVersion = classFileVersion.javaVersion().asMajor();
if (enablePreview != classFileVersion.isPreview() || javaFileVersion.compareTo(javaVersion) != 0) {
if (getLog().isDebugEnabled() || showCompilationChanges) {
getLog().info(String.format(
"\tBytecode file change: %s from %s to %s", path, javaFileVersion, javaVersion));
}
return true;
}
}
return false;
} catch (UncheckedIOException | IOException e) {
getLog().warn("Error reading bytecode version", e);
return false;
}
}

private String getVersionRelease() {
return getRelease() != null ? getRelease() : getTarget() != null ? getTarget() : DEFAULT_TARGET;
}

public void setTarget(String target) {
this.target = target;
targetOrReleaseSet = true;
Expand Down