我们需要自动化测试我们发布的所有Java样本都能正确编译。我们需要它来构建所有文件而不会列出每个文件。列出每一个意味着如果某人忘记添加一个新的(将在某一天发生),显式调用将错过它。通过遍历所有build.xml文件,我们总能获得所有内容。
这样做非常简单:
步骤2需要一种生成build.xml文件的方法。有没有办法告诉ant在子目录下运行所有build.xml文件或创建一个调用所有底层build.xml文件的build.xml?
答案 0 :(得分:1)
听起来你想要做的就是为许多(希望)遵循标准布局模式的子项目运行相同的构建过程。
如果是这种情况,您可以创建一个知道如何编译这些项目的build.xml,并创建一个查找所有子目录的顶级构建脚本,然后在每个子目录中调用公共构建脚本。 Subant was taylor-made for this,并且不需要魔术C#程序在每个目录中生成脚本。
答案 1 :(得分:0)
我们找不到任何东西,所以我们编写了一个程序来创建一个build.xml,它调用目录下的所有build.xml文件。完整的解决方案是Windward Wrocks(我的博客)。
代码是(是的,使用C#为Java创建构建文件):
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace BuildJavaTestScript
{
public class Program
{
/// <summary>
/// Build build.xml for all build.xml files in sub-directories.
/// </summary>
/// <param name="args">Optional: build.xml root_folder</param>
public static void Main(string[] args)
{
string projFile = Path.GetFullPath(args.Length > 0 ? args[0] : "build.xml");
string rootDirectory = Path.GetFullPath(args.Length > 1 ? args[1] : Directory.GetCurrentDirectory());
Console.Out.WriteLine(string.Format("Creating build file {0}", projFile));
Console.Out.WriteLine(string.Format("Root directory {0}", rootDirectory));
XDocument xdoc = new XDocument();
XElement elementProject = new XElement("project");
xdoc.Add(elementProject);
elementProject.Add(new XAttribute("name", "BuildAll"));
elementProject.Add(new XAttribute("default", "compile"));
XElement elementTarget = new XElement("target");
elementProject.Add(elementTarget);
elementTarget.Add(new XAttribute("name", "compile"));
XElement elementEcho = new XElement("echo");
elementTarget.Add(elementEcho);
elementEcho.Add(new XAttribute("message", "Build All: jdk = ${java.home}, version = ${java.version}"));
// add .sln files - recursively
AddBuildXmlFiles(elementTarget, rootDirectory, rootDirectory);
Console.Out.WriteLine("writing build file to disk");
// no BOM
using (var writer = new XmlTextWriter(projFile, new UTF8Encoding(false)))
{
writer.Formatting = Formatting.Indented;
xdoc.Save(writer);
}
Console.Out.WriteLine("all done");
}
private static void AddBuildXmlFiles(XElement elementTarget, string rootDirectory, string folder)
{
// add build.xml files
foreach (string fileOn in Directory.GetFiles(folder, "build.xml"))
{
string filename = Path.GetFileName(fileOn);
string workingFolder;
if (folder.StartsWith(rootDirectory))
{
workingFolder = folder.Substring(rootDirectory.Length).Trim();
if ((workingFolder.Length > 0) && (workingFolder[0] == Path.DirectorySeparatorChar || workingFolder[0] == Path.AltDirectorySeparatorChar))
workingFolder = workingFolder.Substring(1);
}
else
workingFolder = folder;
if (workingFolder.Length == 0)
continue;
XElement elementExec = new XElement("ant");
elementExec.Add(new XAttribute("dir", workingFolder));
elementExec.Add(new XAttribute("antfile", filename));
elementTarget.Add(elementExec);
}
// look in sub-directories
foreach (string subDirectory in Directory.GetDirectories(folder))
AddBuildXmlFiles(elementTarget, rootDirectory, subDirectory);
}
}
}