package com.akirkpatrick.swfclipse; import java.io.File; import java.io.FileWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.ide.IdeDependency; import org.apache.maven.project.MavenProjectHelper; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.runtime.RuntimeInstance; import org.apache.velocity.runtime.RuntimeServices; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.codehaus.plexus.velocity.DefaultVelocityComponent; import org.codehaus.plexus.velocity.VelocityComponent; /** * @goal eclipse * @execute phase="generate-resources" * @requiresDependencyResolution */ public class FlexBuilderMojo extends AbstractIdeSupportMojo { /** * @component */ protected VelocityComponent velocityComponent; /** * @component */ protected MavenProjectHelper projectHelper; /** * @component */ protected ArtifactResolver resolver; /** * The main application mxml file. * * @parameter expression="${project.artifactId}.mxml" * @required */ protected String mainApplication; /** * The list of Flex applications in the module (there is no need to include the main app) * * @parameter */ protected List applications; protected static final String[] excludes; /** * Indicates whether this is an AIR application. Only valid for swf projects * * @parameter */ protected Boolean air = new Boolean(false); public List getExcludes() { // exclude the Flex libs that are included in the runtime return Arrays.asList(excludes); } public String getProjectNameForArifact(Artifact artifact) { return artifact.getArtifactId(); } protected boolean getUseProjectReferences() { return true; } protected boolean setup() throws MojoExecutionException { // only interested in SWF projects return true; // return project.getPackaging().equals("swf") || project.getPackaging().equals("swc"); } protected void writeConfiguration(IdeDependency[] deps) throws MojoExecutionException { writeEclipseProject(); writeFlexProperties(); if ( project.getPackaging().equals("swf") ) { writeSwfProperties(deps); } else { writeSwcProperties(deps); } } private void writeFlexProperties() { VelocityContext context = new VelocityContext(); writeConfigFile(context, "flexProperties"); } private void writeEclipseProject() { VelocityContext context = new VelocityContext(); context.put("projectName", project.getArtifactId()); context.put("air", air); writeConfigFile(context, "project"); } private void writeSwcProperties(IdeDependency[] deps) throws MojoExecutionException { if ( mainApplication.endsWith(".mxml") ) { // we have the default main app path, replace mxml with as for swc build mainApplication = mainApplication.replace(".mxml", ".as"); } VelocityContext context = new VelocityContext(); // context.put("dependencies", getDependencyArtifacts()); context.put("mainApplication", mainApplication); context.put("applications", applications); context.put("dependencies", listDependencies(deps)); writeConfigFile(context, "actionScriptProperties"); } private void writeSwfProperties(IdeDependency[] deps) throws MojoExecutionException { VelocityContext context = new VelocityContext(); // context.put("dependencies", getDependencyArtifacts()); context.put("mainApplication", mainApplication); context.put("applications", applications); context.put("dependencies", listDependencies(deps)); context.put("air", air); writeConfigFile(context, "actionScriptProperties"); } private ArrayList listDependencies(IdeDependency[] deps) throws MojoExecutionException { ArrayList links=new ArrayList(); for ( int n=0; n< deps.length; n++ ) { SwfIdeDependency d = (SwfIdeDependency) deps[n]; if ( !d.getType().equals("swc") ) { // not a SWC module, so ignore continue; } if ( d.isReferencedProject() ) { // link to project in workspace links.add("/"+d.getEclipseProjectName()+"/bin/"+d.getEclipseProjectName()+".swc"); } else { // add link to local repo String l=localRepository.getBasedir()+"/"+localRepository.pathOf(d.getArtifact()); links.add(l); } } return links; } private void writeConfigFile(VelocityContext context, String templateName) { // TODO: better exception handling Writer writer = null; try { Template template = velocityComponent.getEngine().getTemplate("/"+templateName+".vm"); writer = new FileWriter(new File(project.getBasedir(), "."+templateName)); template.merge(context, writer); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } } } static { // TODO: ensure complete list excludes = new String[] { "com.adobe.flex.framework:airframework", "com.adobe.flex.framework:airglobal", "com.adobe.flex.framework:servicemonitor", "com.adobe.flex.framework:flex", "com.adobe.flex.framework:framework", "com.adobe.flex.framework:rpc", "com.adobe.flex.framework:utilities" }; } }