且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 build.xml 在 java 代码中设置属性

更新时间:2023-11-20 09:10:58

就目标构建(即生成的 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);
    }
}