QueryMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException { Log log = getLog(); outputEncoding = validateEncoding(outputEncoding); if (log.isDebugEnabled()) { for (String e : excludes) { log.debug("DataQL: Exclude: " + e); } for (String e : includes) { log.debug("DataQL: Include: " + e); } log.debug("DataQL: Output: " + outputSourceDirectory); } if (!sourceDirectory.isDirectory()) { log.info("No DataQL files to compile in " + sourceDirectory.getAbsolutePath()); return; } try { log.info("delete generate Query Jave Class in Directory " + sourceDirectory.getAbsolutePath()); FileUtils.deleteDirectory(outputSourceDirectory); if (!outputSourceDirectory.exists()) { outputSourceDirectory.mkdirs(); } log.info("delete generate Query Resource in Directory " + outputResourceDirectory.getAbsolutePath()); FileUtils.deleteDirectory(outputResourceDirectory); if (!outputResourceDirectory.exists()) { outputResourceDirectory.mkdirs(); } } catch (Exception e) { log.error(e); } if (!project.getCompileSourceRoots().contains(outputSourceDirectory.getAbsolutePath())) { log.info("DataQL directory " + outputSourceDirectory.getAbsolutePath() + " add to CompileSource Directory."); project.addCompileSourceRoot(outputSourceDirectory.getAbsolutePath()); } if (!project.getResources().contains(outputResourceDirectory.getAbsolutePath())) { log.info("DataQL directory " + outputResourceDirectory.getAbsolutePath() + " add to Resource Directory."); Resource resource = new Resource(); resource.setDirectory(outputResourceDirectory.getAbsolutePath()); project.addResource(resource); } log.info("DataQL : Processing source directory " + outputSourceDirectory.getAbsolutePath()); try { List<String> stringList = IOUtils.readLines(ResourcesUtils.getResourceAsStream("/META-INF/dataql-codegen-template.tpl"), "UTF-8"); final String javaTemp = StringUtils.join(stringList.toArray(new String[0]), "\n"); SourceInclusionScanner scan = new SimpleSourceInclusionScanner(getIncludesPatterns(), excludes); scan.addSourceMapping(new SuffixMapping("ql", Collections.emptySet())); Set<File> qlFiles = scan.getIncludedSources(sourceDirectory, null); for (File qlFile : qlFiles) { buildContext.refresh(qlFile); buildContext.removeMessages(qlFile); log.info("Query file '" + qlFile.getPath() + "' detected."); String relPathBase = MojoUtils.findSourceSubdir(sourceDirectory, qlFile); String relPath = relPathBase + qlFile.getName(); getLog().debug(" ... relative path is: " + relPath); boolean javaxInject = false; String javaxInjectName = ""; try { RootBlockSet queryModel = (RootBlockSet) QueryHelper.queryParser(new AutoCloseInputStream(new FileInputStream(qlFile))); List<HintInst> optionSet = queryModel.getOptionSet(); for (HintInst inst : optionSet) { if ("javax_inject".equalsIgnoreCase(inst.getHint().getValue())) { if (inst.getValue().getValueType() == PrimitiveVariable.ValueType.Boolean) { javaxInject = (Boolean) inst.getValue().getValue(); } } if ("javax_inject_name".equalsIgnoreCase(inst.getHint().getValue())) { if (inst.getValue().getValueType() != PrimitiveVariable.ValueType.Null) { javaxInjectName = inst.getValue().getValue().toString(); } } } } catch (Exception e) { if (!this.ignoreError) { throw e; } else { getLog().error(e); } } String injectReplaceString = ""; if (javaxInject) { if (StringUtils.isNotBlank(javaxInjectName)) { injectReplaceString = "\"" + javaxInjectName.trim() + "\""; } injectReplaceString = "@javax.inject.Named(" + javaxInjectName + ")"; } try (InputStream sourceQueryFile = FileUtils.openInputStream(qlFile)) { File outFile = new File(outputResourceDirectory, relPath); try (OutputStream targetQueryFile = FileUtils.openOutputStream(outFile)) { IOUtils.copy(sourceQueryFile, targetQueryFile); targetQueryFile.flush(); } } String className = qlFile.getName().split("\\.")[0] + "Query"; String targetPackageName = ""; if (StringUtils.isNotBlank(relPathBase)) { targetPackageName = "package " + relPathBase.replace(File.separator, "."); targetPackageName = targetPackageName.substring(0, targetPackageName.length() - 1) + ";"; } String targetJavaName = relPath.replace(File.separator, "."); String tempClass = new String(javaTemp.toCharArray().clone()); tempClass = tempClass.replace("%target_pacakge%", targetPackageName); tempClass = tempClass.replace("%source_resource%", "/" + relPath.replace(File.separator, "/")); tempClass = tempClass.replace("%target_name%", className); tempClass = tempClass.replace("%inject_name%", injectReplaceString); File outFile = new File(new File(outputSourceDirectory, relPath).getParentFile(), className + ".java"); try (OutputStream targetQueryFile = FileUtils.openOutputStream(outFile)) { targetQueryFile.write(tempClass.getBytes()); targetQueryFile.flush(); } } } catch (Exception e) { log.error(e); throw new MojoExecutionException("Fatal error occured while evaluating the names of the query files to analyze", e); } }  @Override void execute();  }
@Test public void buildExample() throws Exception { File basedir = resources.getBasedir("example"); MavenExecutionResult result = mavenRuntime.forProject(basedir).execute("clean", "clean", "package"); result.assertErrorFreeLog(); }