Maven / Gradle List of compiled classes

Maven and Gradle both use incremental builds by default, if I'm not mistaken.

  • For the new / first build, it will create all class files.
  • After the next build, without any changes, it does not create class files.
  • If I change A.java, it recompiles the module.

Is it possible to get a list of class files compiled in this assembly?

+5
source share
1 answer

What about the maven.compiler.verbose property?

If I set it to true, I get either

 % mvn compile -Dmaven.compiler.verbose=true 2>&1 | grep class [INFO] Nothing to compile - all classes are up to date 

or if I modify the A.java file

 % mvn compile -Dmaven.compiler.verbose=true 2>&1 | grep class | grep wrote [wrote RegularFileObject[/tmp/mvn-compile/target/classes/A.class]] [wrote RegularFileObject[/tmp/mvn-compile/target/classes/B.class]] ... and other similar output 

You can also go a little deeper into your OS and trace (for example, strace on some Linux distributions) write syscalls (compatible with posix platforms) and grep .class files that are written.

You can also use the option -Dmaven.compiler.fork=true , which is forks javac (which used to be by default, but no longer seems). You can trace the javac arguments that are passed through a temporary file (which only exists during javac execution, for example, /tmp/org.codehaus.plexus.compiler.javac.JavacCompiler2179201625419771061arguments ). You can also cat this file and see which files are transferred to javac for recompilation. In my case (Mac OS X) this file is stored in /tmp , and I naively run it to output

 % cd /tmp; while true; do ls org.codehaus.plexus.compiler.javac.JavacCompiler* | xargs cat; sleep 1; done zsh: no matches found: org.codehaus.plexus.compiler.javac.JavacCompiler* zsh: no matches found: org.codehaus.plexus.compiler.javac.JavacCompiler* zsh: no matches found: org.codehaus.plexus.compiler.javac.JavacCompiler* "-d" "/tmp/mvn-compile/target/classes" "-classpath" "/Users/stepan/.m2/repository/com/typesafe/config/1.3.0/config-1.3.0.jar:/Users/stepan/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.4/jackson-core-2.6.4.jar:" "-sourcepath" "/tmp/mvn-compile/src/main/java/A.java" "/tmp/mvn-compile/src/main/java/B.java" "-s" "/tmp/mvn-compile/target/generated-sources/annotations" "-g" "-verbose" "-nowarn" "-target" "1.8" "-source" "1.8" "-encoding" "UTF-8" 

Another question is how Maven decides which class to recompile. If I remember her correctly, it was based on a comparison of the timestamps of the .java files and their .class matching files. If the .java file was newer, it was added to javac for recompilation. However, I can see right now, it’s much harder to understand the reasons, I would recommend debugging maven and looking at AbstractCompilerMojo.java and its execute method.
For example, if there is a package-info.java file in my source folder, all classes are always recompiled, even if they are not changed.

+1
source

Source: https://habr.com/ru/post/1238224/


All Articles