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.AbstractStaticServiceLoaderSourceGenerator; 019import io.micronaut.aot.std.sourcegen.KnownMissingTypesSourceGenerator; 020import io.micronaut.maven.services.CompilerService; 021import io.micronaut.maven.services.DependencyResolutionService; 022import io.micronaut.maven.services.ExecutorService; 023import org.apache.commons.io.FileUtils; 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; 029 030import javax.inject.Inject; 031import java.io.File; 032import java.io.IOException; 033import java.io.InputStream; 034import java.io.OutputStream; 035import java.nio.file.Files; 036import java.nio.file.NoSuchFileException; 037import java.nio.file.Path; 038import java.util.ArrayList; 039import java.util.List; 040import java.util.Properties; 041import java.util.stream.Stream; 042 043/** 044 * <p>Invokes the <a href="https://micronaut-projects.github.io/micronaut-aot/latest/guide/">Micronaut AOT</a> 045 * optimizer, generating sources/classes and the effective AOT configuration properties file. Refer to the Micronaut 046 * AOT documentation for more information.</p> 047 * 048 * <p><strong>WARNING</strong>: this goal is not intended to be executed directly. Instead, enable AOT with the 049 * <code>micronaut.aot.enabled</code> property, eg:</p> 050 * 051 * <pre>mvn -Dmicronaut.aot.enabled=true package</pre> 052 * <pre>mvn -Dmicronaut.aot.enabled=true mn:run</pre> 053 */ 054@Mojo(name = AotAnalysisMojo.NAME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) 055public class AotAnalysisMojo extends AbstractMicronautAotCliMojo { 056 057 public static final String NAME = "aot-analysis"; 058 public static final String AOT_PROPERTIES_FILE_NAME = "aot.properties"; 059 060 /** 061 * The project's target directory. 062 */ 063 @Parameter(defaultValue = "${project.build.directory}", required = true) 064 private File baseDirectory; 065 066 /** 067 * Micronaut AOT configuration file. Run the <a href="aot-sample-config-mojo.html"><code>aot-sample-config</code> goal</a> to 068 * see all the possible options. 069 */ 070 @Parameter(property = "micronaut.aot.config", defaultValue = AOT_PROPERTIES_FILE_NAME) 071 private File configFile; 072 073 @Inject 074 @SuppressWarnings("CdiInjectionPointsInspection") 075 public AotAnalysisMojo(CompilerService compilerService, ExecutorService executorService, MavenProject mavenProject, 076 DependencyResolutionService dependencyResolutionService) { 077 super(compilerService, executorService, mavenProject, dependencyResolutionService); 078 } 079 080 @Override 081 protected List<String> getExtraArgs() throws MojoExecutionException { 082 var args = new ArrayList<String>(); 083 args.add("--output"); 084 File generated = outputFile("generated"); 085 args.add(generated.getAbsolutePath()); 086 File effectiveConfigFile = writeEffectiveConfigFile(); 087 args.add("--config"); 088 args.add(effectiveConfigFile.getAbsolutePath()); 089 return args; 090 } 091 092 private File writeEffectiveConfigFile() throws MojoExecutionException { 093 File userProvidedFile = this.configFile == null ? new File(baseDirectory, AOT_PROPERTIES_FILE_NAME) : this.configFile; 094 var props = new Properties(); 095 if (userProvidedFile.exists()) { 096 try (InputStream in = Files.newInputStream(userProvidedFile.toPath())) { 097 getLog().info("Using AOT configuration file: " + configFile.getAbsolutePath()); 098 props.load(in); 099 } catch (IOException e) { 100 throw new MojoExecutionException("Unable to parse configuration file", e); 101 } 102 } 103 if (!props.containsKey(KnownMissingTypesSourceGenerator.OPTION.key())) { 104 props.put(KnownMissingTypesSourceGenerator.OPTION.key(), String.join(",", Constants.TYPES_TO_CHECK)); 105 } 106 props.computeIfAbsent(AbstractStaticServiceLoaderSourceGenerator.SERVICE_TYPES, 107 key -> String.join(",", Constants.SERVICE_TYPES)); 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}