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

li.rudin.mavenjs.plugin.server.ServerMojo Maven / Gradle / Ivy

package li.rudin.mavenjs.plugin.server;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

@Mojo(requiresDependencyResolution=ResolutionScope.TEST, name="server")
public class ServerMojo extends AbstractMojo
{
	
	/**
	 * The port for the mavenjs:server
	 */
	@Parameter(defaultValue="8080") int port;
	
	/**
	 * URL mappings to redirect to
	 */
	@Parameter URLMapping[] urlMappings;
	
	/**
	 * project attribute
	 */
	@Parameter(defaultValue="${project}", readonly=true) MavenProject project;
	
	/**
	 * The default servlet path for the mavenjs servlet
	 */
	@Parameter(defaultValue="/*") String defaultServletPath;


	@Override
	public void execute() throws MojoExecutionException, MojoFailureException
	{
		final Log logger = getLog();

		try
		{
			List urls = new ArrayList();
						
			//src/test/resources
			for (Resource res: project.getBuild().getTestResources())
				urls.add( new File(res.getDirectory()).toURI().toURL() );

			//src/main/resources
			for (Resource res: project.getBuild().getResources())
				urls.add( new File(res.getDirectory()).toURI().toURL() );
			
			// dependencies
			for (Artifact artifact: (Set)project.getArtifacts())
				urls.add( artifact.getFile().toURI().toURL() );

			//Log urls
			for (URL url: urls)
				logger.info("Classpath entry: " + url);

			//TODO: reload on file change
			URLClassLoader classLoader = new URLClassLoader( urls.toArray(new URL[urls.size()]), ServerMojo.class.getClassLoader() );
			

			//Create isolated thread with server task
			Thread thread = new Thread( new ServerTask(this) );			

			thread.setContextClassLoader(classLoader);
			thread.start();
			thread.join();

		}
		catch (Exception e)
		{
			logger.error(e);
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy