private void generateSchemas() throws Exception { File[] jars = collectJars(); if (jars == null) { throw new MojoExecutionException("No classpath file found in the target directory"); } List<URL> jarURLs = new ArrayList<URL>(); jarURLs.add(classesDirectory.toURI().toURL()); for (File jar : jars) { jarURLs.add(jar.toURI().toURL()); } final List<String> injectableClassNames = new ArrayList<String>(); // Scan classes only new Reflections(new ConfigurationBuilder(). addUrls(classesDirectory.toURI().toURL()).setScanners(new AbstractScanner() { @Override public void scan(Object cls) { if ((cls instanceof ClassFile) && isInjectable((ClassFile) cls, getLog())) { injectableClassNames.add(((ClassFile) cls).getName()); } } })); URLClassLoader classLoader = new URLClassLoader(jarURLs.toArray(new URL[jarURLs.size()]), ConfigProperty.class.getClassLoader()); ClassLoader[] loaders = new ClassLoader[]{classLoader}; List<Class> injectableClasses = (List<Class>) (Object) ReflectionUtils.forNames(injectableClassNames, loaders); processClasses(injectableClasses); }
private void createConfigFile() throws Exception { File classesDir = new File(outputDirectory, "classes/"); File libDir = new File(outputDirectory, "lib"); File[] jars = libDir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".jar"); } }); if (jars == null) { throw new MojoExecutionException("No jar files found in " + libDir.getAbsolutePath()); } List<URL> jarURLs = new ArrayList<URL>(); jarURLs.add(classesDir.toURI().toURL()); for (File jar : jars) { jarURLs.add(jar.toURI().toURL()); } // Scan classes and all jars final List<String> candidates = new ArrayList<String>(); new Reflections(new ConfigurationBuilder(). addUrls(jarURLs).setScanners(new AbstractScanner() { @Override public void scan(Object cls) { if ((cls instanceof ClassFile)) { ClassFile classFile = (ClassFile) cls; if (XmlSchemaGeneratingMojo.isInjectable(classFile, getLog()) && (classFile.getName().equals(configClassName) || classFile.getName().endsWith(configClassName))) { candidates.add(classFile.getName()); } } } })); if (candidates.isEmpty()) { throw new MojoFailureException("No class found for " + configClassName); } if (candidates.size() > 1) { throw new MojoFailureException("More classes found for " + configClassName + ":" + candidates); } String fullClassName = candidates.get(0); URLClassLoader classLoader = new URLClassLoader(jarURLs.toArray(new URL[jarURLs.size()]), ConfigProperty.class.getClassLoader()); Class configClass = ReflectionUtils.forName(fullClassName, classLoader); generateToFile(configClass); }