如何将值从build.xml文件传递到java类?

发布于 2024-12-10 13:49:38 字数 124 浏览 0 评论 0原文

我想在运行时将值从 build.xml 文件传递​​到 java 类。我们知道我们可以使用 ant 将值从命令行传递到 build.xml 文件。但是如何将此值从构建文件传递到 java 文件。我们可以用属性文件来完成,但是怎么做呢?

i want to pass the value from build.xml file to java class at run time. We know that we can pass value from command line with ant to the build.xml file. But how to pass this value from build file to java file. We can done it with property file but how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-12-17 13:49:38
  • 使用 PropertyFile 任务从 ant 操作属性文件。
  • 将该文件复制到类路径
  • 使用 getClass().getClassLoader().getResourceAsStream() 打开属性文件的输入流。
  • 将该 InputStream 传递给 Properties.load() 以加载应用程序中的属性。
  • Use the PropertyFile task to manipulate a properties file from ant.
  • Copy that file to your classpath
  • Use getClass().getClassLoader().getResourceAsStream() to open an input stream to the properties file.
  • Pass that InputStream to Properties.load() to load the properties in your app.
宫墨修音 2024-12-17 13:49:38

假设您想从 build.xml 读取 ,这里是纯 xml 读取/解析方法,

@Test
public void test_read_build_xml() {
    String buildVersion = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new File("build.xml"));
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("/project/property[@name='version']/@value");
        NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        buildVersion = nodes.item(0).getNodeValue();
    } catch (Exception ex) {
        logger.error("cannot read build version");
    }
    logger.info("buildVersion: " + buildVersion);
    assertNotNull(buildVersion);
}

您也可以使用 Project、ProjectHelper - 请参阅如何解析和解释 ant 的 build.xml

(如果需要)到从ant修改源代码,请参阅是否可以修改源代码与蚂蚁?

assume you want to read <property name="version" value="123"/> from build.xml, here is plain xml read/parse approach

@Test
public void test_read_build_xml() {
    String buildVersion = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new File("build.xml"));
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("/project/property[@name='version']/@value");
        NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        buildVersion = nodes.item(0).getNodeValue();
    } catch (Exception ex) {
        logger.error("cannot read build version");
    }
    logger.info("buildVersion: " + buildVersion);
    assertNotNull(buildVersion);
}

you can also user Project, ProjectHelper - see How to parse and interpret ant's build.xml

if you want to modify source code from ant, see Is it possible to modify source code with ant?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文