1 16 17 package org.springframework.metadata.commons; 18 19 import java.io.File ; 20 import java.net.URL ; 21 22 import org.apache.commons.attributes.compiler.AttributeCompiler; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.taskdefs.Javac; 25 import org.apache.tools.ant.types.FileSet; 26 import org.apache.tools.ant.types.Path; 27 28 import org.springframework.core.ControlFlowFactory; 29 30 39 public class CommonsAttributeCompilerUtils { 40 41 public static final String MARKER_FILE = "/org.springframework.test.marker"; 42 43 public static void compileAttributesIfNecessary(String testWildcards) { 44 if (inIde()) { 45 ideAttributeCompile(testWildcards); 46 } 47 } 48 49 public static boolean inIde() { 50 return inEclipse(); 51 } 52 53 public static boolean inEclipse() { 54 return ControlFlowFactory.createControlFlow().underToken("eclipse.jdt"); 56 } 57 58 public static void ideAttributeCompile(String testWildcards) { 59 System.out.println("Compiling attributes under IDE"); 60 Project project = new Project(); 61 62 URL markerUrl = CommonsAttributeCompilerUtils.class.getResource(MARKER_FILE); 63 File markerFile = new File (markerUrl.getFile()); 64 File root = markerFile.getParentFile().getParentFile().getParentFile(); 66 67 project.setBaseDir(root); 68 project.init(); 69 70 AttributeCompiler commonsAttributesCompiler = new AttributeCompiler(); 71 commonsAttributesCompiler.setProject(project); 72 73 String tempPath = "target/generated-commons-attributes-src"; 75 commonsAttributesCompiler.setDestdir(new File (tempPath)); 76 FileSet fileset = new FileSet(); 77 fileset.setDir(new File (root.getPath() + File.separator + "test")); 78 String attributeClasses = testWildcards; 79 fileset.setIncludes(attributeClasses); 80 commonsAttributesCompiler.addFileset(fileset); 81 82 commonsAttributesCompiler.execute(); 83 84 System.out.println("Compiling Java sources generated by Commons Attributes using Javac: requires tools.jar on Eclipse project classpath"); 85 Javac javac = new Javac(); 88 javac.setProject(project); 89 Path path = new Path(project, tempPath); 91 javac.setSrcdir(path); 92 93 javac.setDestdir(new File (root.getPath() + File.separator + "target/test-classes")); 96 javac.setIncludes(attributeClasses); 97 javac.execute(); 98 } 99 100 } 101 | Popular Tags |