Validate.notNullAndNoNullValues(scopes, "Cannot compile sources, there were no scopes defined");
Validate.notNull(inputDirectory, "Directory with sources to be compiled must not be null");
Validate.notNull(outputDirectory, "Target directory for compiled sources must not be null");
JavacCompiler compiler = new JavacCompiler();
CompilerConfiguration configuration = getCompilerConfiguration();
if (log.isLoggable(Level.FINE)) {
log.log(Level.FINE, "Compiling sources from {0} directory into {1}",
new Object[] { inputDirectory, outputDirectory });
}
// in order to compile sources, we need to resolve dependencies first
// so we have a classpath available
new AddScopedDependenciesTask(ScopeType.values()).execute(session);
final MavenResolutionStrategy scopeStrategy = new AcceptScopesStrategy(scopes);
final Collection<MavenResolvedArtifact> artifactResults = session.resolveDependencies(scopeStrategy);
for (MavenResolvedArtifact artifact : artifactResults) {
String classpathEntry = artifact.asFile().getAbsolutePath();
configuration.addClasspathEntry(classpathEntry);
if (log.isLoggable(Level.FINER)) {
log.log(Level.FINER, "Adding {0} to compilation classpath", classpathEntry);
}
}
configuration.addSourceLocation(inputDirectory.getPath());
configuration.setOutputLocation(outputDirectory.getPath());
try {
CompilerResult result = compiler.performCompile(configuration);
if (!result.isSuccess()) {
// try to fork compilation process if it wasn't set to fork already
if (!configuration.isFork()) {
log.log(Level.WARNING,
"MavenImporter was not able to identify javac compiler, probably JAVA_HOME points to JRE instead of JDK. MavenImporter will try to fork and use javac from $PATH");
configuration.setFork(true);
CompilerResult result2 = compiler.performCompile(configuration);
if (!result2.isSuccess()) {
log.log(Level.WARNING,
"Unable to compile project with neither forked nor embedded compiler. Returning original exception message");
// throw original exception
throw constructCompilationException(result, inputDirectory);