Java Code Examples for org.apache.tools.ant.Project#executeTarget()

The following examples show how to use org.apache.tools.ant.Project#executeTarget() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovycTest.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected void setUp() {
    project = new Project();
    project.init();
    ProjectHelper.getProjectHelper().parse(project, antFile);
    project.executeTarget("clean");

    String altJavaHome = System.getProperty("java.home");
    if (altJavaHome.lastIndexOf("jre") >= 0) {
        altJavaHome = altJavaHome.substring(0, altJavaHome.lastIndexOf("jre"));
    } else {
        altJavaHome = altJavaHome + File.separator + "jre";
    }
    try {
        File altFile = new File(altJavaHome);
        if (altFile.exists()) {
            project.setProperty("alt.java.home", altJavaHome);
        }
    } catch (Exception ignore) {
        // could be security, io, etc.
        // End result is as if .exists() returned null
    }
}
 
Example 2
Source File: AntHarnessTest.java    From ExpectIt with Apache License 2.0 6 votes vote down vote up
@Test
public void runTarget() throws IOException {
    Project project = newProject();
    project.log("started: " + target);
    // prepare
    project.executeTarget("");
    boolean negative = target.endsWith("-negative");
    // run test
    try {
        project.executeTarget(target);
        if (negative) {
            fail("Negative test fails");
        }
    } catch (BuildException e) {
        e.printStackTrace();
        if (!negative) {
            fail("Positive test fails");
        }
    } finally {
        project.log("finished");
    }
}
 
Example 3
Source File: ShuffleModel.java    From Shuffle-Move with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Builds the bug report itself.
 */
private void buildBugReport() {
   try {
      Project p = getErrorProject();
      p.executeTarget(p.getDefaultTarget());
      LOG.log(Level.INFO,
            getString(KEY_BUG_FILE_SUCCESS, Paths.get(p.getProperty("zipname")).toAbsolutePath().toString()));
      UpdateCheck.showParentDirectoryOf(new File(Paths.get(p.getProperty("zipname")).toAbsolutePath().toString()));
   } catch (Exception e) {
      LOG.info(getString(KEY_BUG_REPORT_PROBLEM, e.getMessage()));
      e.printStackTrace();
   }
}
 
Example 4
Source File: FileSender.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void runAntScript() {
	Project ant = new Project();
	DefaultLogger consoleLogger = new DefaultLogger();
	consoleLogger.setErrorPrintStream(System.err);
	consoleLogger.setOutputPrintStream(System.out);
	consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
	ant.addBuildListener(consoleLogger);
	ant.init();
	ProjectHelper helper = new ProjectHelperImpl();
	helper.parse(ant, new File(filename));
	ant.executeTarget(ant.getDefaultTarget());
}
 
Example 5
Source File: PackagerCacheEntry.java    From ant-ivy with Apache License 2.0 4 votes vote down vote up
/**
 * Attempt to build this entry.
 *
 * @param packagerResource
 *            packager metadata resource
 * @param properties
 *            a map of properties to pass to the child Ant build responsible for dependency
 *            packaging
 *
 * @throws IOException
 *             if this entry has already been built
 */
public synchronized void build(Resource packagerResource, Map<String, String> properties) throws IOException {
    // Sanity check
    if (this.built) {
        throw new IllegalStateException("build in directory `" + this.dir
                + "' already completed");
    }

    // Remove work directory if it exists (e.g. left over from last time)
    if (this.dir.exists()) {
        if (!cleanup()) {
            throw new IOException("can't remove directory `" + this.dir + "'");
        }
    }

    // Create work directory
    if (!this.dir.mkdirs()) {
        throw new IOException("can't create directory `" + this.dir + "'");
    }

    // Write out packager XML
    InputStream packagerXML = packagerResource.openStream();
    saveFile("packager.xml", packagerXML);

    // Write packager XSLT
    saveFile("packager.xsl");

    // Write packager XSD
    saveFile("packager-1.0.xsd");

    // Write master Ant build file
    saveFile("build.xml");

    // Execute the Ant build file
    Project project = new Project();
    project.init();
    project.setUserProperty("ant.file", new File(dir, "build.xml").getAbsolutePath());
    ProjectHelper.configureProject(project, new File(dir, "build.xml"));
    project.setBaseDir(dir);

    // Configure logging verbosity
    BuildLogger logger = new DefaultLogger();
    logger.setMessageOutputLevel(this.verbose ? Project.MSG_VERBOSE
            : this.quiet ? Project.MSG_WARN : Project.MSG_INFO);
    logger.setOutputPrintStream(System.out);
    logger.setErrorPrintStream(System.err);
    project.addBuildListener(logger);

    // Set properties
    project.setUserProperty("ivy.packager.organisation", ""
            + this.mr.getModuleId().getOrganisation());
    project.setUserProperty("ivy.packager.module", "" + this.mr.getModuleId().getName());
    project.setUserProperty("ivy.packager.revision", "" + this.mr.getRevision());
    project.setUserProperty("ivy.packager.branch", "" + this.mr.getBranch());
    if (this.resourceCache != null) {
        project.setUserProperty("ivy.packager.resourceCache",
            "" + this.resourceCache.getCanonicalPath());
    }
    if (this.resourceURL != null) {
        project.setUserProperty("ivy.packager.resourceURL", "" + getResourceURL());
    }
    if (this.validate) {
        project.setUserProperty("ivy.packager.validate", "true");
    }
    project.setUserProperty("ivy.packager.restricted", "" + this.restricted);
    project.setUserProperty("ivy.packager.quiet", String.valueOf(quiet));
    if (properties != null) {
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            project.setUserProperty(entry.getKey(), entry.getValue());
        }
    }

    // Execute task
    Message.verbose("performing packager resolver build in " + this.dir);
    try {
        project.executeTarget("build");
        this.built = true;
    } catch (BuildException e) {
        Message.verbose("packager resolver build failed: " + e);
        throw e;
    }
}
 
Example 6
Source File: AntTaskUtils.java    From was-maven-plugin with Apache License 2.0 4 votes vote down vote up
public static void execute(WebSphereModel model, PlexusConfiguration target, MavenProject project,
                           MavenProjectHelper projectHelper, List<Artifact> pluginArtifact, Log logger)
    throws IOException, MojoExecutionException {
  // The fileName should probably use the plugin executionId instead of the targetName
  boolean useDefaultTargetName = false;
  String antTargetName = target.getAttribute("name");
  if (null == antTargetName) {
    antTargetName = DEFAULT_ANT_TARGET_NAME;
    useDefaultTargetName = true;
  }
  StringBuilder fileName = new StringBuilder(50);
  fileName.append("build");
  if (StringUtils.isNotBlank(model.getHost())) {
    fileName.append("-").append(model.getHost());
  }
  if (StringUtils.isNotBlank(model.getApplicationName())) {
    fileName.append("-").append(model.getApplicationName());
  }
  fileName.append("-").append(antTargetName).append("-").append(CommandUtils.getTimestampString()).append(".xml");
  File buildFile = getBuildFile(project, fileName.toString());

  if (model.isVerbose()) {
    logger.info("ant fileName: " + fileName);
  }

  if (buildFile.exists()) {
    logger.info("[SKIPPED] already executed");
    return;
  }

  StringWriter writer = new StringWriter();
  AntXmlPlexusConfigurationWriter xmlWriter = new AntXmlPlexusConfigurationWriter();
  xmlWriter.write(target, writer);

  StringBuffer antXML = writer.getBuffer();

  if (useDefaultTargetName) {
    stringReplace(antXML, "<target", "<target name=\"" + antTargetName + "\"");
  }

  final String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
  antXML.insert(0, xmlHeader);
  final String projectOpen = "<project name=\"" + Constants.PLUGIN_ID + "\" default=\"" + antTargetName + "\">\n";
  int index = antXML.indexOf("<target");
  antXML.insert(index, projectOpen);

  final String projectClose = "\n</project>";
  antXML.append(projectClose);

  buildFile.getParentFile().mkdirs();
  FileUtils.fileWrite(buildFile.getAbsolutePath(), "UTF-8", antXML.toString());

  Project antProject = generateAntProject(model, buildFile, project, projectHelper, pluginArtifact, logger);
  antProject.executeTarget(antTargetName);
}
 
Example 7
Source File: CCGBankConvert.java    From openccg with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void main(String[] args) {
	File baseDir = new File(System.getProperty("user.dir"));
	File buildFile = new File(baseDir, "build.xml");
	
	Project project = new Project();
	
	project.init();
	
	project.setBaseDir(baseDir);
	
	ProjectHelper helper = ProjectHelper.getProjectHelper();
	
	project.setProjectReference(helper);
	
	helper.parse(project, buildFile);
	
	DefaultLogger logger = new DefaultLogger();
	logger.setErrorPrintStream(System.err);
	logger.setOutputPrintStream(System.out);
	
	project.addBuildListener(logger);
	
	project.executeTarget("convert-base");		
}