001/* 002 * SPDX-FileCopyrightText: none 003 * SPDX-License-Identifier: CC0-1.0 004 */ 005 006package gov.nist.secauto.metaschema.maven.plugin; 007 008import gov.nist.secauto.metaschema.core.model.IModule; 009import gov.nist.secauto.metaschema.core.util.ObjectUtils; 010import gov.nist.secauto.metaschema.databind.codegen.IProduction; 011import gov.nist.secauto.metaschema.databind.codegen.JavaGenerator; 012import gov.nist.secauto.metaschema.databind.codegen.config.DefaultBindingConfiguration; 013 014import org.apache.maven.plugin.MojoExecutionException; 015import org.apache.maven.plugins.annotations.LifecyclePhase; 016import org.apache.maven.plugins.annotations.Mojo; 017import org.apache.maven.plugins.annotations.Parameter; 018 019import java.io.File; 020import java.io.IOException; 021import java.util.Arrays; 022import java.util.Collections; 023import java.util.List; 024import java.util.Set; 025import java.util.stream.Collectors; 026 027import edu.umd.cs.findbugs.annotations.NonNull; 028 029/** 030 * Goal which generates Java source files for a given set of Metaschema modules. 031 */ 032@Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES) 033public class GenerateSourcesMojo 034 extends AbstractMetaschemaMojo { 035 private static final String STALE_FILE_NAME = "generateSourcesStaleFile"; 036 037 /** 038 * A set of binding configurations. 039 */ 040 @Parameter 041 protected File[] configs; 042 043 /** 044 * <p> 045 * Gets the last part of the stale filename. 046 * </p> 047 * <p> 048 * The full stale filename will be generated by pre-pending 049 * {@code "." + getExecution().getExecutionId()} to this staleFileName. 050 * 051 * @return the stale filename postfix 052 */ 053 @Override 054 protected String getStaleFileName() { 055 return STALE_FILE_NAME; 056 } 057 058 /** 059 * Retrieve a list of binding configurations. 060 * 061 * @return the collection of binding configurations 062 */ 063 protected List<File> getConfigs() { 064 List<File> retval; 065 if (configs == null) { 066 retval = Collections.emptyList(); 067 } else { 068 retval = Arrays.asList(configs); 069 } 070 return retval; 071 } 072 073 /** 074 * Generate the Java source files for the provided Metaschemas. 075 * 076 * @param modules 077 * the collection of Metaschema modules to generate sources for 078 * @throws MojoExecutionException 079 * if an error occurred while generating sources 080 */ 081 @Override 082 @NonNull 083 protected List<File> generate(@NonNull Set<IModule> modules) throws MojoExecutionException { 084 DefaultBindingConfiguration bindingConfiguration = new DefaultBindingConfiguration(); 085 for (File config : getConfigs()) { 086 try { 087 if (getLog().isInfoEnabled()) { 088 getLog().info("Loading binding configuration: " + config.getPath()); 089 } 090 bindingConfiguration.load(config); 091 } catch (IOException ex) { 092 throw new MojoExecutionException( 093 String.format("Unable to load binding configuration from '%s'.", config.getPath()), ex); 094 } 095 } 096 097 IProduction production; 098 try { 099 if (getLog().isInfoEnabled()) { 100 getLog().info("Generating Java classes in: " + getOutputDirectory().getPath()); 101 } 102 production = JavaGenerator.generate( 103 modules, 104 ObjectUtils.notNull(getOutputDirectory().toPath()), 105 bindingConfiguration); 106 } catch (IOException ex) { 107 throw new MojoExecutionException("Creation of Java classes failed.", ex); 108 } 109 110 // add generated sources to Maven 111 try { 112 getMavenProject().addCompileSourceRoot(getOutputDirectory().getCanonicalFile().getPath()); 113 } catch (IOException ex) { 114 throw new MojoExecutionException("Unable to add output directory to maven sources.", ex); 115 } 116 117 return ObjectUtils.notNull(production.getGeneratedClasses() 118 .map(gen -> gen.getClassFile().toFile()) 119 .collect(Collectors.toUnmodifiableList())); 120 } 121}