Esempio n. 1
0
  @SuppressWarnings("unchecked")
  public static void load(String[] args) throws Exception {
    ArrayMap params = makeDefaultConfig(args);
    if (args.length == 0) {
      if (params == null) return;
    } else {
      String path = args[0];
      if (".".equals(path)) {
        File thisPath = new File("");
        args[0] = thisPath.getAbsolutePath() + "\\build.txt";
        args[0] = StringUtils.replace(args[0], "\\", "/");
      }
      String fileContext = readString(new FileInputStream(args[0]), LSystem.ENCODING);
      ArrayMap map = (ArrayMap) ParseData.parseAll(fileContext);
      params.putAll(map);
    }
    String pb1 = (String) params.get("sourceDir");
    String outputDir = (String) params.get("outputDir");
    String javaHome = JVM.getJavaHome((String) params.get("javaHome"));
    if (outputDir == null) {
      outputDir = ".";
    }
    if (!StringUtils.isEmpty(javaHome)) {
      log("found latest JDK:" + javaHome);
    } else {
      log("didnot found JDK");
    }
    Object prjList = params.get("list");
    Projects prjs1 = new Projects();
    prjs1.verbose = true;
    prjs1.addPrjs((List<Object>) prjList);
    prjs1.sourceDir =
        args.length == 0 ? "." : addPath(new File(args[0]).getParent(), pb1).getCanonicalPath();
    prjs1.javaHome = javaHome;

    long t1 = System.currentTimeMillis();
    if (args.length > 1 && args[1].equals("clean")) {
      new JavaBuild(params).clean(prjs1);
    }
    new JavaBuild(params).build(prjs1, outputDir);
    long t2 = System.currentTimeMillis();
    log(
        String.format(
            "Compile end. time cost %,d ms, javac(compiled):%,d, copy:%,d(%,d bytes), jar:%,d, java(exec):%,d.",
            t2 - t1,
            prjs1.totalJavac,
            prjs1.totalCopy,
            prjs1.totalCopys,
            prjs1.totalJar,
            prjs1.totalJava));
  }
Esempio n. 2
0
  @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");
      }
    }
  }