All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.loci.LociMojo Maven / Gradle / Ivy

The newest version!
/*
 * Copyright "2011" Adam Warski and Amanj Sherwany
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package net.java.loci;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.CompilationFailureException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.compiler.CompilerError;

import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.File;

/**
 * This is a modified version of Checker framework's maven plugin,
 * I removed everything related to the Checker's framework and put Loci
 * stuff instead. The work also depends on Checker framework's maven plugin.
 * @goal check
 * @author Adam Warski (adam at warski dot org)
 * @author Amanj Sherwany
 */
public class LociMojo extends AbstractMojo {
	/**
	 * PARAMETERS
	 */


	/**
	 * A list of inclusion filters for the compiler.
	 *
	 * @parameter
	 */
	private Set includes = new HashSet();

	/**
	 * A list of exclusion filters for the compiler.
	 *
	 * @parameter
	 */
	private Set excludes = new HashSet();

	/**
	 * Should the build fail on a loci compile error.
	 *
	 * @parameter default-value="true"
	 */
	private boolean failOnError;

	/**
	 * @parameter
	 */
	private String executable;

	/**
	 * @parameter default-value="1.1.2"
	 */
	private String jsrVersion;
	
	

	/**
	 * @parameter
	 */
	private String javaParams;

	/**
	 * @parameter
	 */
	private String javacParams;

	/**
	 * DEPENDENCIES
	 */

	/**
	 * The source directories containing the sources to be compiled.
	 *
	 * @parameter expression="${project.compileSourceRoots}"
	 * @required
	 * @readonly
	 */
	private List compileSourceRoots;

	/**
	 * The current build session instance. This is used for
	 * toolchain manager API calls.
	 *
	 * @parameter expression="${session}"
	 * @required
	 * @readonly
	 */
	private MavenSession session;

	/**
	 * @component
	 * @required
	 * @readonly
	 */
	private ToolchainManager toolchainManager;

	/**
	 * @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
	 * @required
	 * @readonly
	 */
	private ArtifactResolver artifactResolver;

	/**
	 * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
	 * @required
	 * @readonly
	 */
	private ArtifactFactory artifactFactory;

//	/**
//	 * @parameter expression="${project}"
//	 * @readonly
//	 * @required
//	 */
//	private MavenProject mavenProject;
//
//	/**
//	 * @parameter expression="${plugin.artifacts}"
//	 * @readonly
//	 * @required
//	 */
//	private List pluginArtifacts;

	/**
	 * @parameter expression="${localRepository}"
	 * @required
	 * @readonly
	 */
	private ArtifactRepository localRepository;

	/**
	 * @parameter expression="${project.remoteArtifactRepositories}"
	 * @required
	 * @readonly
	 */
	private List remoteArtifactRepositories;

	/**
	 * @parameter expression="${project.compileClasspathElements}"
	 * @required
	 * @readonly
	 */
	private List classpathElements;

	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info("Running Loci Checker version: ");

		String processor = "loci.LociChecker";

		getLog().info("Running processor: " + processor);

		List sources = PathUtils.scanForSources(compileSourceRoots, includes, excludes);

		String jsrJar = PathUtils.getJSRJar(jsrVersion, artifactFactory, artifactResolver,
				remoteArtifactRepositories, localRepository);
		
		
		Commandline cl = new Commandline();

		if (StringUtils.isEmpty(executable)) {
			executable = "java";
		}
		cl.setExecutable(PathUtils.getExecutablePath(executable, toolchainManager, session));

		// Building the arguments
		List arguments = new ArrayList();

		// Setting the boot class path: prepending the jar with loci plugin
		arguments.add("-Xbootclasspath/p:" + jsrJar);
		// Javac currently assumes that assertions are enabled in the launcher
		arguments.add("-ea:com.sun.tools");
		// Optionally adding user-specified java parameters
		if (!StringUtils.isEmpty(javaParams)) {
			arguments.addAll(Arrays.asList(javaParams.split(" ")));
		}
		// Running the compile process - main class of this jar 
		arguments.add("-jar");
		arguments.add(jsrJar);

		// Now the arguments for the jar main class - that is, the compiler

		// Setting the name of the processor
		arguments.add("-processor");
		arguments.add(processor);
		// Running only the annotation processor, without compiling
		arguments.add("-proc:only");
		// Setting the classpath
		arguments.add("-classpath" );
		arguments.add(StringUtils.join(classpathElements.iterator(), File.pathSeparator));
		// Setting the source dir path
		arguments.add("-sourcepath");
		arguments.add(StringUtils.join(compileSourceRoots.iterator(), File.pathSeparator));
		// Optionally adding user-specified javac parameters
		if (!StringUtils.isEmpty(javacParams)) {
			arguments.addAll(Arrays.asList(javacParams.split(" ")));
		}

		// Now the source files
		arguments.addAll(sources);

		// And executing
		cl.addArguments(arguments.toArray(new String[arguments.size()]));

		executeCommandLine(cl);
	}

	private void executeCommandLine(Commandline cl) throws MojoExecutionException, MojoFailureException {
		CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
		CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();

		// Executing the command
		int exitCode;
		try {
			exitCode = CommandLineUtils.executeCommandLine(cl, out, err);
		} catch (CommandLineException e) {
			throw new MojoExecutionException("Unable to execute loci, executable: " + executable +
					", command line: " + Arrays.toString(cl.getCommandline()), e);
		}

		// Parsing the messages from the compiler
		List messages;
		try {
			messages = JavacCompilerUtil.parseModernStream( new BufferedReader( new StringReader( err.getOutput() ) ) );
		} catch (IOException e) {
			throw new MojoExecutionException("Unable to parse messages.", e);
		}

		// Sanity check - if the exit code is non-zero, there should be some messages
		if (exitCode != 0 && messages.isEmpty()) {
			throw new MojoExecutionException("Exit code from the compiler was not zero (" + exitCode +
					"), but no messages reported. Error stream content: " + err.getOutput());
		}

		if (messages.isEmpty()) {
			getLog().info("No errors found by the loci processor.");
		} else {
			if (failOnError) {
				throw new MojoFailureException(CompilationFailureException.longMessage(messages));
			} else {
				for (CompilerError compilerError : messages) {
					getLog().warn(compilerError.toString());
				}
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy