Skip to content

Method: findFiles(String)

1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.maven;
19:
20: import cz.cvut.kbss.jopa.modelgen.ModelGenProcessor;
21: import org.apache.maven.artifact.DependencyResolutionRequiredException;
22: import org.apache.maven.plugin.AbstractMojo;
23: import org.apache.maven.plugins.annotations.LifecyclePhase;
24: import org.apache.maven.plugins.annotations.Mojo;
25: import org.apache.maven.plugins.annotations.Parameter;
26: import org.apache.maven.plugins.annotations.ResolutionScope;
27: import org.apache.maven.project.MavenProject;
28:
29: import javax.tools.*;
30: import java.io.File;
31: import java.net.URISyntaxException;
32: import java.net.URL;
33: import java.net.URLClassLoader;
34: import java.util.*;
35:
36: import static org.apache.commons.lang3.StringUtils.isNotBlank;
37: import static org.apache.commons.lang3.StringUtils.join;
38:
39:
40: @Mojo(
41: requiresDependencyResolution = ResolutionScope.COMPILE,
42: defaultPhase = LifecyclePhase.GENERATE_SOURCES,
43: name = "modelgen"
44: )
45: public class ModelGenMojo extends AbstractMojo {
46:
47: private static final String OUTPUT_DIRECTORY_PARAM = "output-directory";
48: private static final String ADDITIONAL_SOURCES_PARAM = "additional-sources";
49: private static final String SOURCE_PACKAGE_PARAM = "source-package";
50: public static final String DEBUG_PARAM = "debug-option";
51:
52: @Parameter(defaultValue = "${project}", readonly = true, required = true)
53: private MavenProject project;
54: @Parameter(name = OUTPUT_DIRECTORY_PARAM, defaultValue = "./target/generated-sources/static-metamodel")
55: private String outputDirectory;
56: @Parameter(name = SOURCE_PACKAGE_PARAM)
57: private String sourcePackage;
58: @Parameter(name = DEBUG_PARAM, defaultValue = "false")
59: private String debugOption;
60: @Parameter(name = ADDITIONAL_SOURCES_PARAM)
61: private String additionalSources;
62:
63:
64: public void execute() {
65: printParameterValues();
66: getLog().info("");
67: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
68:
69: List<String> options = new ArrayList<>();
70: options.add("-processor");
71: options.add(ModelGenProcessor.class.getName());
72:
73: final File[] classPathFiles = getClassPathFiles();
74:
75: final String compileClassPath = join(classPathFiles, File.pathSeparator);
76:
77: options.add("-cp");
78: options.add(compileClassPath);
79:
80: options.add("-d");
81: File folder = new File("./target/classes");
82: if (!folder.exists()) {
83: folder.mkdirs();
84: }
85: options.add("./target/classes");
86:
87: if (isNotBlank(outputDirectory)) {
88: options.add("-AoutputDirectory=" + outputDirectory);
89: }
90:
91: if (isNotBlank(sourcePackage)) {
92: options.add("-AsourcePackage=" + sourcePackage);
93: }
94:
95: if (isNotBlank(debugOption)) {
96: options.add("-AdebugOption=" + debugOption);
97: }
98:
99: StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
100:
101: DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
102:
103: List<File> sourceFiles = new ArrayList<>();
104:
105: for (File directory : getSourceDirectories()) {
106: sourceFiles.addAll(findFiles(directory.getPath()));
107: }
108: Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
109:
110: JavaCompiler.CompilationTask task =
111: compiler.getTask(null, fileManager, diagnosticCollector, options, null, compilationUnits);
112:
113: getLog().info("Processing annotations.");
114: task.call();
115: logTaskDiagnostics(diagnosticCollector);
116: getLog().info("Static metamodel generated.");
117: getLog().info("------------------------------------------------------------------------");
118: }
119:
120: private Set<File> getSourceDirectories() {
121: Set<File> directories = new HashSet<>();
122: List<String> directoryNames = getCompileSourceRoots();
123: for (String name : directoryNames) {
124: File file = new File(name);
125: if (file.exists() && file.isDirectory()) {
126: directories.add(file);
127: }
128: }
129: return directories;
130: }
131:
132: private List<String> getCompileSourceRoots() {
133: final List<String> compileSourceRoots = project.getCompileSourceRoots();
134: if (additionalSources != null && !additionalSources.isEmpty()) {
135: compileSourceRoots.add(additionalSources);
136: }
137: return compileSourceRoots;
138: }
139:
140: public List<File> findFiles(String directoryName) {
141: List<File> resultList = new ArrayList<>();
142: File directory = new File(directoryName);
143:
144: // Get all files from a directory.
145: File[] fList = directory.listFiles();
146:• if (fList != null) {
147:• for (File file : fList) {
148:• if (file.isFile()) {
149: resultList.add(file);
150:• } else if (file.isDirectory()) {
151: resultList.addAll(findFiles(file.getAbsolutePath()));
152: }
153: }
154: }
155: return resultList;
156:
157: }
158:
159: private File[] getClassPathFiles() {
160: final Set<File> files = new TreeSet<>(getCurrentClassPath());
161: List<?> classpathElements;
162: try {
163: classpathElements = project.getTestClasspathElements();
164: } catch (DependencyResolutionRequiredException e) {
165: throw new RuntimeException(e.getMessage(), e);
166: }
167:
168: for (final Object o : classpathElements) {
169: if (o != null) {
170: final File file = new File(o.toString());
171: if (file.canRead()) {
172: files.add(file);
173: }
174: }
175: }
176:
177: return files.toArray(new File[0]);
178: }
179:
180: private List<File> getCurrentClassPath() {
181: final List<File> retVal = new ArrayList<>();
182: final URLClassLoader cl = (URLClassLoader) this.getClass().getClassLoader();
183: try {
184: for (URL url : cl.getURLs()) {
185: retVal.add(new File(url.toURI()));
186: }
187: return retVal;
188: } catch (URISyntaxException exc) {
189: throw new RuntimeException(exc.getMessage(), exc);
190: }
191: }
192:
193: private void logTaskDiagnostics(DiagnosticCollector<JavaFileObject> diagnosticCollector) {
194: for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) {
195: switch (diagnostic.getKind()) {
196: case ERROR:
197: getLog().error(diagnostic.getMessage(null));
198: break;
199: case WARNING: // Intentional fall-through
200: case MANDATORY_WARNING:
201: getLog().warn(diagnostic.getMessage(null));
202: break;
203: case NOTE: // Intentional fall-through
204: case OTHER:
205: getLog().info(diagnostic.getMessage(null));
206: break;
207: }
208: }
209: }
210:
211: private void printParameterValues() {
212: Utils.logParameterValue(OUTPUT_DIRECTORY_PARAM, outputDirectory, getLog());
213: Utils.logParameterValue(SOURCE_PACKAGE_PARAM, sourcePackage, getLog());
214: Utils.logParameterValue(DEBUG_PARAM, debugOption, getLog());
215: Utils.logParameterValue(ADDITIONAL_SOURCES_PARAM, additionalSources, getLog());
216: }
217: }