001/* 002 * Copyright 2017-2022 original authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.micronaut.maven.aot; 017 018import io.micronaut.aot.std.sourcegen.KnownMissingTypesSourceGenerator; 019import io.micronaut.maven.services.CompilerService; 020import io.micronaut.maven.services.DependencyResolutionService; 021import io.micronaut.maven.services.ExecutorService; 022import org.apache.commons.io.FileUtils; 023import org.apache.maven.execution.MavenSession; 024import org.apache.maven.plugin.MojoExecutionException; 025import org.apache.maven.plugins.annotations.Mojo; 026import org.apache.maven.plugins.annotations.Parameter; 027import org.apache.maven.plugins.annotations.ResolutionScope; 028import org.apache.maven.project.MavenProject; 029import org.apache.maven.toolchain.ToolchainManager; 030 031import javax.inject.Inject; 032import java.io.File; 033import java.io.IOException; 034import java.io.InputStream; 035import java.io.OutputStream; 036import java.nio.file.Files; 037import java.nio.file.NoSuchFileException; 038import java.nio.file.Path; 039import java.util.ArrayList; 040import java.util.List; 041import java.util.Properties; 042import java.util.stream.Stream; 043 044/** 045 * <p>Invokes the <a href="https://micronaut-projects.github.io/micronaut-aot/latest/guide/">Micronaut AOT</a> 046 * optimizer, generating sources/classes and the effective AOT configuration properties file. Refer to the Micronaut 047 * AOT documentation for more information.</p> 048 * 049 * <p><strong>WARNING</strong>: this goal is not intended to be executed directly. Instead, enable AOT with the 050 * <code>micronaut.aot.enabled</code> property, eg:</p> 051 * 052 * <pre>mvn -Dmicronaut.aot.enabled=true package</pre> 053 * <pre>mvn -Dmicronaut.aot.enabled=true mn:run</pre> 054 */ 055@Mojo(name = AotAnalysisMojo.NAME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) 056public class AotAnalysisMojo extends AbstractMicronautAotCliMojo { 057 058 public static final String NAME = "aot-analysis"; 059 public static final String AOT_PROPERTIES_FILE_NAME = "aot.properties"; 060 061 /** 062 * The project's target directory. 063 */ 064 @Parameter(defaultValue = "${project.build.directory}", required = true) 065 private File baseDirectory; 066 067 /** 068 * Micronaut AOT configuration file. Run the <a href="aot-sample-config-mojo.html"><code>aot-sample-config</code> goal</a> to 069 * see all the possible options. 070 */ 071 @Parameter(property = "micronaut.aot.config", defaultValue = AOT_PROPERTIES_FILE_NAME) 072 private File configFile; 073 074 @Inject 075 @SuppressWarnings("CdiInjectionPointsInspection") 076 public AotAnalysisMojo(CompilerService compilerService, ExecutorService executorService, MavenProject mavenProject, 077 DependencyResolutionService dependencyResolutionService, 078 MavenSession mavenSession, ToolchainManager toolchainManager) { 079 super(compilerService, executorService, mavenProject, dependencyResolutionService, mavenSession, toolchainManager); 080 } 081 082 @Override 083 protected List<String> getExtraArgs() throws MojoExecutionException { 084 var args = new ArrayList<String>(); 085 args.add("--output"); 086 File generated = outputFile("generated"); 087 args.add(generated.getAbsolutePath()); 088 File effectiveConfigFile = writeEffectiveConfigFile(); 089 args.add("--config"); 090 args.add(effectiveConfigFile.getAbsolutePath()); 091 return args; 092 } 093 094 private File writeEffectiveConfigFile() throws MojoExecutionException { 095 File userProvidedFile = this.configFile == null ? new File(baseDirectory, AOT_PROPERTIES_FILE_NAME) : this.configFile; 096 var props = new Properties(); 097 if (userProvidedFile.exists()) { 098 try (InputStream in = Files.newInputStream(userProvidedFile.toPath())) { 099 getLog().info("Using AOT configuration file: " + configFile.getAbsolutePath()); 100 props.load(in); 101 } catch (IOException e) { 102 throw new MojoExecutionException("Unable to parse configuration file", e); 103 } 104 } 105 if (!props.containsKey(KnownMissingTypesSourceGenerator.OPTION.key())) { 106 props.put(KnownMissingTypesSourceGenerator.OPTION.key(), String.join(",", Constants.TYPES_TO_CHECK)); 107 } 108 File effectiveConfig = outputFile("effective-" + AOT_PROPERTIES_FILE_NAME); 109 try (OutputStream out = Files.newOutputStream(effectiveConfig.toPath())) { 110 props.store(out, "Effective AOT configuration"); 111 } catch (IOException e) { 112 throw new MojoExecutionException("Unable to parse configuration file", e); 113 } 114 return effectiveConfig; 115 } 116 117 @Override 118 protected void onSuccess(File outputDir) throws MojoExecutionException { 119 Path generated = outputDir.toPath().resolve("generated"); 120 Path generatedClasses = generated.resolve("classes"); 121 try { 122 FileUtils.copyDirectory(generatedClasses.toFile(), outputDirectory); 123 try (Stream<String> linesStream = Files.lines(generated.resolve("logs").resolve("resource-filter.txt"))) { 124 linesStream.forEach(toRemove -> { 125 try { 126 Files.delete(outputDirectory.toPath().resolve(toRemove)); 127 getLog().debug("Removed " + toRemove); 128 } catch (IOException e) { 129 if (!(e instanceof NoSuchFileException)) { 130 getLog().warn("Error while deleting " + toRemove, e); 131 } 132 } 133 }); 134 } 135 } catch (IOException e) { 136 throw new MojoExecutionException("Error when copying the Micronaut AOT generated classes into the target directory", e); 137 } 138 } 139 140 @Override 141 String getName() { 142 return NAME; 143 } 144}