Package org.apache.tools.ant.types

Examples of org.apache.tools.ant.types.Commandline


     * @throws Exception Indicates an error executing the target {@link ExecTask}.
     */
    private void startOpenOfficeServer() throws Exception
    {
        ExecTask execTask = (ExecTask) this.project.createTask("exec");
        Commandline commandLine =
            new Commandline(OPENOFFICE_EXECUTABLE
                + " -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard");
        execTask.setCommand(commandLine);
        execTask.execute();
    }
View Full Code Here


    /** reset variables to permit gc */
    public void reset() {
        //version = false;
        source14 = false;
        ignoredOptions = new HashSet();
        cmd = new Commandline();
        vmcmd = new Commandline();
        threads = -1;
        destdir = null;
        workingdir = null;
        internalclasspath = null;
        classpath = null;
View Full Code Here

    static boolean isEmpty(String s) {
      return ((null == s) || (0 == s.trim().length()));
    }

    GuardedCommand() {
      command = new Commandline();
    }
View Full Code Here

    public Ajdoc() {
        reset();
    }

    protected void reset() {
        cmd = new Commandline();
        vmcmd = new Commandline();
        sourcepath  = null;
        destdir  = null ;
        sourcefiles  = null;
        packagenames  = null;
        packageList  = null;
View Full Code Here

   * @throws BuildException if anything wrong happen during the compilation
   * @return boolean true if the compilation is ok, false otherwise
   */
  public boolean execute() throws BuildException {
    this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.usingJDTCompiler"), Project.MSG_VERBOSE); //$NON-NLS-1$
    Commandline cmd = setupJavacCommand();

    try {
      Class c = Class.forName(compilerClass);
      Constructor batchCompilerConstructor = c.getConstructor(new Class[] { PrintWriter.class, PrintWriter.class, Boolean.TYPE, Map.class});
      Object batchCompilerInstance = batchCompilerConstructor.newInstance(new Object[] {new PrintWriter(System.out), new PrintWriter(System.err), Boolean.TRUE, this.customDefaultOptions});
      Method compile = c.getMethod("compile", new Class[] {String[].class}); //$NON-NLS-1$
      Object result = compile.invoke(batchCompilerInstance, new Object[] { cmd.getArguments()});
      final boolean resultValue = ((Boolean) result).booleanValue();
      if (!resultValue && this.logFileName != null) {
        this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.error.compilationFailed", this.logFileName)); //$NON-NLS-1$
      }
      return resultValue;
View Full Code Here

    }
  }
 
 
  protected Commandline setupJavacCommand() throws BuildException {
    Commandline cmd = new Commandline();
    this.customDefaultOptions = new CompilerOptions().getMap();
   
    Class javacClass = Javac.class;
   
    /*
     * Read in the compiler arguments first since we might need to modify
     * the classpath if any access rules were specified
     */
    String [] compilerArgs = processCompilerArguments(javacClass);

    /*
     * This option is used to never exit at the end of the ant task.
     */
    cmd.createArgument().setValue("-noExit"); //$NON-NLS-1$

        if (this.bootclasspath != null) {
      cmd.createArgument().setValue("-bootclasspath"); //$NON-NLS-1$
          if (this.bootclasspath.size() != 0) {
          /*
           * Set the bootclasspath for the Eclipse compiler.
           */
          cmd.createArgument().setPath(this.bootclasspath);
          } else {
          cmd.createArgument().setValue(Util.EMPTY_STRING);
          }
        }

        Path classpath = new Path(this.project);

       /*
         * Eclipse compiler doesn't support -extdirs.
         * It is emulated using the classpath. We add extdirs entries after the
         * bootclasspath.
         */
        if (this.extdirs != null) {
      cmd.createArgument().setValue("-extdirs"); //$NON-NLS-1$
      cmd.createArgument().setPath(this.extdirs);         
        }

    /*
     * The java runtime is already handled, so we simply want to retrieve the
     * ant runtime and the compile classpath.
     */
        classpath.append(getCompileClasspath());

        // For -sourcepath, use the "sourcepath" value if present.
        // Otherwise default to the "srcdir" value.
        Path sourcepath = null;
       
        // retrieve the method getSourcepath() using reflect
        // This is done to improve the compatibility to ant 1.5
        Method getSourcepathMethod = null;
        try {
          getSourcepathMethod = javacClass.getMethod("getSourcepath", null); //$NON-NLS-1$
        } catch(NoSuchMethodException e) {
          // if not found, then we cannot use this method (ant 1.5)
        }
        Path compileSourcePath = null;
        if (getSourcepathMethod != null) {
       try {
        compileSourcePath = (Path) getSourcepathMethod.invoke(this.attributes, null);
      } catch (IllegalAccessException e) {
        // should never happen
      } catch (InvocationTargetException e) {
        // should never happen
      }
        }
        if (compileSourcePath != null) {
            sourcepath = compileSourcePath;
        } else {
            sourcepath = this.src;
        }
    classpath.append(sourcepath);
    /*
     * Set the classpath for the Eclipse compiler.
     */
    cmd.createArgument().setValue("-classpath"); //$NON-NLS-1$
    createClasspathArgument(cmd, classpath);

        final String javaVersion = JavaEnvUtils.getJavaVersion();
    String memoryParameterPrefix = javaVersion.equals(JavaEnvUtils.JAVA_1_1) ? "-J-" : "-J-X";//$NON-NLS-1$//$NON-NLS-2$
        if (this.memoryInitialSize != null) {
            if (!this.attributes.isForkedJavac()) {
                this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.ignoringMemoryInitialSize"), Project.MSG_WARN); //$NON-NLS-1$
            } else {
                cmd.createArgument().setValue(memoryParameterPrefix
                                              + "ms" + this.memoryInitialSize); //$NON-NLS-1$
            }
        }

        if (this.memoryMaximumSize != null) {
            if (!this.attributes.isForkedJavac()) {
                this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.ignoringMemoryMaximumSize"), Project.MSG_WARN); //$NON-NLS-1$
            } else {
                cmd.createArgument().setValue(memoryParameterPrefix
                                              + "mx" + this.memoryMaximumSize); //$NON-NLS-1$
            }
        }

        if (this.debug) {
         // retrieve the method getSourcepath() using reflect
          // This is done to improve the compatibility to ant 1.5
          Method getDebugLevelMethod = null;
          try {
            getDebugLevelMethod = javacClass.getMethod("getDebugLevel", null); //$NON-NLS-1$
          } catch(NoSuchMethodException e) {
            // if not found, then we cannot use this method (ant 1.5)
            // debug level is only available with ant 1.5.x
          }
           String debugLevel = null;
          if (getDebugLevelMethod != null) {
        try {
          debugLevel = (String) getDebugLevelMethod.invoke(this.attributes, null);
        } catch (IllegalAccessException e) {
          // should never happen
        } catch (InvocationTargetException e) {
          // should never happen
        }
          }
      if (debugLevel != null) {
        this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
        this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
        this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.DO_NOT_GENERATE);
        if (debugLevel.length() != 0) {
          if (debugLevel.indexOf("vars") != -1) {//$NON-NLS-1$
            this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
          }
          if (debugLevel.indexOf("lines") != -1) {//$NON-NLS-1$
            this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
          }
          if (debugLevel.indexOf("source") != -1) {//$NON-NLS-1$
            this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.GENERATE);
          }
        }
      } else {
        this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
        this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
        this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.GENERATE);
            }
        } else {
      this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
      this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
      this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.DO_NOT_GENERATE);
        }
     
    /*
     * Handle the nowarn option. If none, then we generate all warnings.
     */
    if (this.attributes.getNowarn()) {
          // disable all warnings
      Object[] entries = this.customDefaultOptions.entrySet().toArray();
      for (int i = 0, max = entries.length; i < max; i++) {
        Map.Entry entry = (Map.Entry) entries[i];
        if (!(entry.getKey() instanceof String))
          continue;
        if (!(entry.getValue() instanceof String))
          continue;
        if (((String) entry.getValue()).equals(CompilerOptions.WARNING)) {
          this.customDefaultOptions.put(entry.getKey(), CompilerOptions.IGNORE);
        }
      }
      this.customDefaultOptions.put(CompilerOptions.OPTION_TaskTags, Util.EMPTY_STRING);
      if (this.deprecation) {
        this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.WARNING);
        this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.ENABLED);
        this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.ENABLED);
      }
    } else if (this.deprecation) {
      this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.WARNING);
      this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.ENABLED);
      this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.ENABLED);
    } else {
      this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
      this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.DISABLED);
      this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.DISABLED);
    }

       /*
     * destDir option.
     */   
    if (this.destDir != null) {
      cmd.createArgument().setValue("-d"); //$NON-NLS-1$
      cmd.createArgument().setFile(this.destDir.getAbsoluteFile());
    }

    /*
     * verbose option
     */
    if (this.verbose) {
      cmd.createArgument().setValue("-verbose"); //$NON-NLS-1$
    }

    /*
     * failnoerror option
     */
    if (!this.attributes.getFailonerror()) {
      cmd.createArgument().setValue("-proceedOnError"); //$NON-NLS-1$
    }

    /*
     * target option.
     */
    if (this.target != null) {
      this.customDefaultOptions.put(CompilerOptions.OPTION_TargetPlatform, this.target);
    }

    /*
     * source option
     */
    String source = this.attributes.getSource();
        if (source != null) {
      this.customDefaultOptions.put(CompilerOptions.OPTION_Source, source);
        }

    /*
     * encoding option
     */
        if (this.encoding != null) {
            cmd.createArgument().setValue("-encoding"); //$NON-NLS-1$
            cmd.createArgument().setValue(this.encoding);
        }

    if (compilerArgs != null) {
          /*
       * Add extra argument on the command line
       */
      final int length = compilerArgs.length;
      if (length != 0) {
        for (int i = 0, max = length; i < max; i++) {
          String arg = compilerArgs[i];
          if (this.logFileName == null && "-log".equals(arg) && ((i + 1) < max)) { //$NON-NLS-1$
            this.logFileName = compilerArgs[i + 1];
          }
              cmd.createArgument().setValue(arg);
        }
      }
       }
       /*
     * Eclipse compiler doesn't have a -sourcepath option. This is
View Full Code Here

    public void execute() throws BuildException {
        checkOptions();

        File paramfile = createParamFile();
        try {
            Commandline cmdl = new Commandline();
            cmdl.setExecutable(new File(home, "jpcovmerge").getAbsolutePath());
            if (verbose) {
                cmdl.createArgument().setValue("-v");
            }
            cmdl.createArgument().setValue("-jp_paramfile=" + paramfile.getAbsolutePath());

            LogStreamHandler handler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
            Execute exec = new Execute(handler);
            log(cmdl.describeCommand(), Project.MSG_VERBOSE);
            exec.setCommandline(cmdl.getCommandline());

            // JProbe process always return 0 so  we will not be
            // able to check for failure ! :-(
            int exitValue = exec.execute();
            if (exitValue != 0) {
View Full Code Here

   private String[] environment;

   public Command()
   {
      super();
      this.commandLine = new Commandline();
      environment = new String[0];
   }
View Full Code Here

                                     + "been specified.");
        }

        log("Generating Javadoc", Project.MSG_INFO);

        Commandline toExecute = (Commandline) cmd.clone();

        // ------------------------------------------ general javadoc arguments
        if (doctitle != null) {
            toExecute.createArgument().setValue("-doctitle");
            toExecute.createArgument().setValue(expand(doctitle.getText()));
        }
        if (header != null) {
            toExecute.createArgument().setValue("-header");
            toExecute.createArgument().setValue(expand(header.getText()));
        }
        if (footer != null) {
            toExecute.createArgument().setValue("-footer");
            toExecute.createArgument().setValue(expand(footer.getText()));
        }
        if (bottom != null) {
            toExecute.createArgument().setValue("-bottom");
            toExecute.createArgument().setValue(expand(bottom.getText()));
        }

        if (classpath == null) {
            classpath = (new Path(getProject())).concatSystemClasspath("last");
        } else {
            classpath = classpath.concatSystemClasspath("ignore");
        }

        if (!javadoc1) {
            if (classpath.size() > 0) {
                toExecute.createArgument().setValue("-classpath");
                toExecute.createArgument().setPath(classpath);
            }
            if (sourceDirs.size() > 0) {
                toExecute.createArgument().setValue("-sourcepath");
                toExecute.createArgument().setPath(sourceDirs);
            }
        } else {
            //sourceDirs.append(classpath);
            if (sourceDirs.size() > 0) {
                toExecute.createArgument().setValue("-classpath");
                toExecute.createArgument().setPath(sourceDirs);
            }
        }

        if (version && doclet == null) {
            toExecute.createArgument().setValue("-version");
        }
        if (author && doclet == null) {
            toExecute.createArgument().setValue("-author");
        }

        if (javadoc1 || doclet == null) {
            if (destDir == null) {
                String msg = "destDir attribute must be set!";
                throw new BuildException(msg);
            }
        }

        // ---------------------------- javadoc2 arguments for default doclet

        if (!javadoc1) {
            if (doclet != null) {
                if (doclet.getName() == null) {
                    throw new BuildException("The doclet name must be "
                                             + "specified.", getLocation());
                } else {
                    toExecute.createArgument().setValue("-doclet");
                    toExecute.createArgument().setValue(doclet.getName());
                    if (doclet.getPath() != null) {
                        Path docletPath
                            = doclet.getPath().concatSystemClasspath("ignore");
                        if (docletPath.size() != 0) {
                            toExecute.createArgument().setValue("-docletpath");
                            toExecute.createArgument().setPath(docletPath);
                        }
                    }
                    for (Enumeration e = doclet.getParams();
                         e.hasMoreElements();) {
                        DocletParam param = (DocletParam) e.nextElement();
                        if (param.getName() == null) {
                            throw new BuildException("Doclet parameters must "
                                                     + "have a name");
                        }

                        toExecute.createArgument().setValue(param.getName());
                        if (param.getValue() != null) {
                            toExecute.createArgument()
                                .setValue(param.getValue());
                        }
                    }
                }
            }
            if (bootclasspath != null && bootclasspath.size() > 0) {
                toExecute.createArgument().setValue("-bootclasspath");
                toExecute.createArgument().setPath(bootclasspath);
            }

            // add the links arguments
            if (links.size() != 0) {
                for (Enumeration e = links.elements(); e.hasMoreElements();) {
                    LinkArgument la = (LinkArgument) e.nextElement();

                    if (la.getHref() == null || la.getHref().length() == 0) {
                        log("No href was given for the link - skipping",
                            Project.MSG_VERBOSE);
                        continue;
                    } else {
                        // is the href a valid URL
                        try {
                            URL base = new URL("file://.");
                            new URL(base, la.getHref());
                        } catch (MalformedURLException mue) {
                            // ok - just skip
                            log("Link href \"" + la.getHref()
                                + "\" is not a valid url - skipping link",
                                Project.MSG_WARN);
                            continue;
                        }
                    }

                    if (la.isLinkOffline()) {
                        File packageListLocation = la.getPackagelistLoc();
                        if (packageListLocation == null) {
                            throw new BuildException("The package list "
                                + " location for link " + la.getHref()
                                + " must be provided because the link is "
                                + "offline");
                        }
                        File packageListFile =
                            new File(packageListLocation, "package-list");
                        if (packageListFile.exists()) {
                            try {
                                String packageListURL =
                                    fileUtils.getFileURL(packageListLocation)
                                    .toExternalForm();
                                toExecute.createArgument()
                                    .setValue("-linkoffline");
                                toExecute.createArgument()
                                    .setValue(la.getHref());
                                toExecute.createArgument()
                                    .setValue(packageListURL);
                            } catch (MalformedURLException ex) {
                                log("Warning: Package list location was "
                                    + "invalid " + packageListLocation,
                                    Project.MSG_WARN);
                            }
                        } else {
                            log("Warning: No package list was found at "
                                + packageListLocation, Project.MSG_VERBOSE);
                        }
                    } else {
                        toExecute.createArgument().setValue("-link");
                        toExecute.createArgument().setValue(la.getHref());
                    }
                }
            }

            // add the single group arguments
            // Javadoc 1.2 rules:
            //   Multiple -group args allowed.
            //   Each arg includes 3 strings: -group [name] [packagelist].
            //   Elements in [packagelist] are colon-delimited.
            //   An element in [packagelist] may end with the * wildcard.

            // Ant javadoc task rules for group attribute:
            //   Args are comma-delimited.
            //   Each arg is 2 space-delimited strings.
            //   E.g., group="XSLT_Packages org.apache.xalan.xslt*,
            //                XPath_Packages org.apache.xalan.xpath*"
            if (group != null) {
                StringTokenizer tok = new StringTokenizer(group, ",", false);
                while (tok.hasMoreTokens()) {
                    String grp = tok.nextToken().trim();
                    int space = grp.indexOf(" ");
                    if (space > 0){
                        String name = grp.substring(0, space);
                        String pkgList = grp.substring(space + 1);
                        toExecute.createArgument().setValue("-group");
                        toExecute.createArgument().setValue(name);
                        toExecute.createArgument().setValue(pkgList);
                    }
                }
            }

            // add the group arguments
            if (groups.size() != 0) {
                for (Enumeration e = groups.elements(); e.hasMoreElements();) {
                    GroupArgument ga = (GroupArgument) e.nextElement();
                    String title = ga.getTitle();
                    String packages = ga.getPackages();
                    if (title == null || packages == null) {
                        throw new BuildException("The title and packages must "
                                                 + "be specified for group "
                                                 + "elements.");
                    }
                    toExecute.createArgument().setValue("-group");
                    toExecute.createArgument().setValue(expand(title));
                    toExecute.createArgument().setValue(packages);
                }
            }

            // JavaDoc 1.4 parameters
            if (javadoc4) {
                for (Enumeration e = tags.elements(); e.hasMoreElements();) {
                    Object element = e.nextElement();
                    if (element instanceof TagArgument) {
                        TagArgument ta = (TagArgument) element;
                        toExecute.createArgument().setValue ("-tag");
                        toExecute.createArgument().setValue (ta.getParameter());
                    } else {
                        ExtensionInfo tagletInfo = (ExtensionInfo) element;
                        toExecute.createArgument().setValue("-taglet");
                        toExecute.createArgument().setValue(tagletInfo
                                                            .getName());
                        if (tagletInfo.getPath() != null) {
                            Path tagletPath = tagletInfo.getPath()
                                .concatSystemClasspath("ignore");
                            if (tagletPath.size() != 0) {
                                toExecute.createArgument()
                                    .setValue("-tagletpath");
                                toExecute.createArgument().setPath(tagletPath);
                            }
                        }
                    }
                }

                if (source != null) {
                    toExecute.createArgument().setValue("-source");
                    toExecute.createArgument().setValue(source);
                }
            }

        }

        File tmpList = null;
        PrintWriter srcListWriter = null;
        try {

            /**
             * Write sourcefiles and package names to a temporary file
             * if requested.
             */
            if (useExternalFile) {
                if (tmpList == null) {
                    tmpList = fileUtils.createTempFile("javadoc", "", null);
                    toExecute.createArgument()
                        .setValue("@" + tmpList.getAbsolutePath());
                }
                srcListWriter = new PrintWriter(
                                    new FileWriter(tmpList.getAbsolutePath(),
                                                   true));
            }

            Enumeration e = packagesToDoc.elements();
            while (e.hasMoreElements()) {
                String packageName = (String) e.nextElement();
                if (useExternalFile) {
                    srcListWriter.println(packageName);
                } else {
                    toExecute.createArgument().setValue(packageName);
                }
            }

            e = sourceFilesToDoc.elements();
            while (e.hasMoreElements()) {
                SourceFile sf = (SourceFile) e.nextElement();
                String sourceFileName = sf.getFile().getAbsolutePath();
                if (useExternalFile) {
                    srcListWriter.println(sourceFileName);
                } else {
                    toExecute.createArgument().setValue(sourceFileName);
                }
            }

        } catch (IOException e) {
            tmpList.delete();
            throw new BuildException("Error creating temporary file",
                                     e, getLocation());
        } finally {
            if (srcListWriter != null) {
                srcListWriter.close();
            }
        }

        if (packageList != null) {
            toExecute.createArgument().setValue("@" + packageList);
        }
        log(toExecute.describeCommand(), Project.MSG_VERBOSE);

        log("Javadoc execution", Project.MSG_INFO);

        JavadocOutputStream out = new JavadocOutputStream(Project.MSG_INFO);
        JavadocOutputStream err = new JavadocOutputStream(Project.MSG_WARN);
        Execute exe = new Execute(new PumpStreamHandler(out, err));
        exe.setAntRun(getProject());

        /*
         * No reason to change the working directory as all filenames and
         * path components have been resolved already.
         *
         * Avoid problems with command line length in some environments.
         */
        exe.setWorkingDirectory(null);
        try {
            exe.setCommandline(toExecute.getCommandline());
          int ret=0;
          String[] arguments = toExecute.getArguments();
          for (int i = 0; i < arguments.length; i++) {
            String argument = arguments[i];
            System.out.println(i+") "+argument);
          }
          Main.main(arguments);// int ret =exe.execute();
View Full Code Here

        cp = new Path(project, getTestClassPath());
        ej.setClasspath(cp);
    }

    private Commandline getCommandline(int timetorun) throws Exception {
        Commandline cmd = new Commandline();
        cmd.setExecutable(TimeProcess.class.getName());
        cmd.createArgument().setValue(String.valueOf(timetorun));
        return cmd;
    }
View Full Code Here

TOP

Related Classes of org.apache.tools.ant.types.Commandline

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.