如何使用 build.xml 在 java 代码中设置属性 [英] How to set a property in java code using build.xml

查看:33
本文介绍了如何使用 build.xml 在 java 代码中设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ant 的新手,任何帮助将不胜感激.

I am new to Ant and any help will be appreciated.

我想做的是:

当我调用 Ant 目标时,我在做:

When I am invoking an Ant target, I am doing :

ant -DSIMV3.1=true run-tenantManagement

现在在 build.xml 中,我有:

Now Inside build.xml, I have:

<target name="run-tenantManagement" depends="jar">
   <property name="SIMV3.1" value="${SIMV3.1}" />
    ...
</target>

现在我希望属性 SIMV3.1 的值在我的 java 代码中可见.因为在我的java代码中,我想设置一个条件:

Now I want the value of the property SIMV3.1 to be visible inside my java code. Because in my java code, I want to set a condition that:

if(SIMV3.1==true){
//do something
}else{
//do something else
}

请帮忙.

推荐答案

就目标构建(即生成的 jar 文件)中的属性值而言,另一种更持久的方法是让 ant 将属性值写入资源包含在目标 jar 文件中的文件.这使在构建期间设置的属性值具有更好的生命周期(因为缺少更好的词),以便它可以在项目(即构建的)产品中可用.例如,一个示例 ant 文件可能如下所示:

Another approach that's more persistent as far as the property value in your target build (i.e. the resulting jar file) is to have ant write the property value to a resource file that gets included in the target jar file. This gives the property value that gets set during the build a better lifespan (for lack of a better word) so that it can be made available in the project's (i.e. build's) products. For instance, an example ant file may look like:

<project name="sandbox" basedir="." default="build">
    <condition property="SIMV3.1" value="${SIMV3.1}" else="false">
        <isset property="SIMV3.1"/>
    </condition>

    <target name="clean">
        <delete verbose="${verbose}" dir="bin" failonerror="false"/>
        <delete verbose="${verbose}" file="lib/sandbox.jar" failonerror="false"/>
    </target>

    <target name="compile">
        <mkdir dir="bin"/>
        <javac srcdir="src" destdir="bin"/>
    </target>

    <target name="resources">
        <echo file="bin/sandbox/resources/SIMV3.1">${SIMV3.1}</echo>
    </target>

    <target name="build" depends="compile, resources">
        <mkdir dir="lib"/>
        <jar destfile="lib/sandbox.jar" basedir="bin">
            <manifest>
                <attribute name="Main-Class" value="sandbox.Sandbox"/>
            </manifest>
        </jar>
   </target>
</project>

condition 任务将属性值默认为 false,如果未提供,resources 任务会将属性值写入将包含在 jar 文件中的资源文件(在 build 任务中).因此,像这样构建:

The condition task defaults the property value to false if it's not provided and the resources tasks writes the property value to the resource file that will be included in the jar file (in the build task). Therefore building like this:

ant -DSIMV3.1=true

将导致将真"值写入resources 包中的SIMV3.1 文件.在不指定属性的情况下调用 ant 将导致将 false 的默认属性写入该资源文件.

will result in a "true" value being written to the SIMV3.1 file in the resources package. Invoking ant without specifying the property will result in a default property of false being written to that resource file.

在代码中,可以通过 InputStream 访问该资源,方法是从文件中读取第一行(也是唯一行)并使用 Boolean 类解析该行,如下所示:

In the code this resource can be accessed via InputStream by reading the first (and only) line from the file and parsing that line using the Boolean class like so:

package sandbox;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Sandbox {

    public static void main(String[] args) {

        String resource = "/sandbox/resources/SIMV3.1";
        InputStream stream = Sandbox.class.getResourceAsStream(resource);
        InputStreamReader reader = new InputStreamReader(stream);
        BufferedReader buffer = new BufferedReader(reader);
        boolean simv3_1 = false;

        try {

            simv3_1 = Boolean.parseBoolean(buffer.readLine());
        }
        catch (IOException e) {

            e.printStackTrace();
        }

        System.out.println("SIMV3.1 = " + simv3_1);
    }
}

这篇关于如何使用 build.xml 在 java 代码中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆