public void copyTo(ProjectItem prj, String dest) throws IOException { File outputDir = addPath(prjList.sourceDir, dest); outputDir.mkdirs(); String path = addPath(prjList.sourceDir, prj.dir).getCanonicalPath(); JarFileCopy copy = new JarFileCopy(); copy.setProject(project); copy.setFile(new File(path + "/completed/" + prj.name + ".jar")); copy.setTodir(outputDir); int cnt = copy.execute(); if (cnt > 0) { log(prj.name + ":jar copied to " + dest); } if (prj.cp != null) { for (Object o : prj.cp) { File f = addPath(prjList.sourceDir, o.toString()); if (f.isDirectory()) { for (File f1 : f.listFiles()) { if (f1.getName().endsWith(".jar")) { copy.setFile(f1); copy.execute(); } } } else { copy.setFile(f); copy.execute(); } } } }
@SuppressWarnings("unchecked") public void buildProject(ProjectItem prj) throws Exception { String prjName = prj.name; log(prjName + ":build start"); File path = addPath(prjList.sourceDir, prj.dir); project = new ProjectName(prjList); project.setName(prjName); JavaCompile javac = new JavaCompile(); javac.setProject(project); javac.setExecutable(prjList.javaHome + (JDK.isWindows() ? "/bin/javac.exe" : "/bin/javac")); javac.setTarget(getParam("target", "1.8")); javac.setSource(getParam("source", "1.8")); javac.setEncoding(getParam("encoding", LSystem.ENCODING)); javac.setDebug(new Boolean(getParam("debug", "false"))); File srcDir = new File(path.getCanonicalPath(), "/" + sourceFileName); if (!srcDir.exists()) { throw new RuntimeException("src dir not found:" + srcDir.getCanonicalPath()); } javac.setSrcdir(path.getCanonicalPath() + "/" + sourceFileName); File buildDir = new File(path.getCanonicalPath() + "/build"); buildDir.mkdirs(); String buildOutputPath = buildDir.getCanonicalPath(); javac.setDestdir(buildOutputPath); JavaPath cp = new JavaPath(project); if (prj.classpaths != null) { for (Object o : prj.classpaths) { String classPath = o.toString(); File classFile = new File(classPath); if (!classFile.exists()) { classFile = addPath(prjList.sourceDir, classPath); } cp.add(classFile.getAbsolutePath()); } } if (prj.cp != null) { for (Object o : prj.cp) { File f1 = addPath(prjList.sourceDir, o.toString()); if (f1.isDirectory()) { File[] fs = f1.listFiles(); for (File f : fs) { if (f.getName().endsWith(".jar")) { cp.add(f.getCanonicalPath()); } } } else { cp.add(addPath(prjList.sourceDir, o.toString()).getCanonicalPath()); } } } if (prj.depends != null) { for (Object o : prj.depends) { ProjectItem p1 = prjList.maps.get(o.toString()); String po = addPath(prjList.sourceDir, p1.dir).getCanonicalPath() + "/completed/" + p1.name + ".jar"; cp.add(po); } } javac.setClasspath(cp); int cnt = javac.execute(); if (cnt < 0) { throw new RuntimeException("javac failed with code:" + cnt); } if (cnt == 0) { log(prjName + "::the project no more to compile"); } else { log(prjName + "::the compile files count(" + cnt + ")"); } if (prj.jars != null) { for (Object o : prj.jars) { File f1 = addPath(prjList.sourceDir, o.toString()); if (f1.exists()) { FileUtils.copyDir(f1.getAbsolutePath(), buildDir.getAbsolutePath()); } } } JarFileCopy copy = new JarFileCopy(); copy.setProject(project); copy.setTodir(buildDir); JavaFileSet fileSet = new JavaFileSet(); fileSet.addFile(new File(path.getCanonicalPath() + "/" + sourceFileName)); fileSet.setExcludesEndsWith(".java"); fileSet.ignoreEclipsePrjFile = true; copy.addFileset(fileSet); int cnt2 = copy.execute(); log(String.format("%s:copy %d resources", prjName, cnt2)); JarConfig jar = new JarConfig(); jar.setProject(project); File jarFile = new File(path.getCanonicalPath() + "/completed/" + prjName + ".jar"); jarFile.getParentFile().mkdirs(); jar.setDestFile(jarFile); jar.setBasedir(buildDir); if (prj.manifests != null) { for (Object o : prj.manifests) { String result = o.toString().trim(); int idx = result.indexOf(':'); if (idx != -1 && StringUtils.charCount(result, ':') == 1) { String key = result.substring(0, idx); String value = result.substring(idx + 1, result.length()); jar.addManifest(key, value); } } } if (prj.mainClass != null) { // 主函数 jar.addManifest("Main-Class", prj.mainClass); } jar.execute(); copyTo(prj, outputDir); if (prj.run != null) { JavaCall run = new JavaCall(); cp.add(jarFile.getCanonicalPath()); run.setClasspath(cp); run.setProject(project); for (Object o : prj.run) { List<Object> row = (List<Object>) o; run.setClassname((String) row.get(0)); for (Object o1 : (List<Object>) row.get(2)) { run.addArg(o1.toString()); } run.execute(); } } if (prj.outSrc != null && StringUtils.toBoolean(prj.outSrc)) { // 打包源码 File file = new File(path.getCanonicalPath() + "/" + sourceFileName); if (file.exists()) { ZipFileMake make = new ZipFileMake(); File output = addPath(prjList.sourceDir, outputDir); String sourceFilePath = output.getCanonicalPath(); make.zipFolder(file.getCanonicalPath(), sourceFilePath + "/" + prjName + "-source.jar"); } } }