Java 我可以在 Ant 脚本中声明和初始化变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19496964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:45:40  来源:igfitidea点击:

Can I declare and initialize a variable in an Ant script?

javaant

提问by AndreaNobili

In Ant, can I create a target that contains something like a variable that represents a path?

在 Ant 中,我可以创建一个包含诸如表示路径的变量之类的目标吗?

For example, something like the following pseudo target:

例如,类似于以下伪目标:

<target name="initPath">
    Path = "${basedir}/../../myProject/Project/"
</target>

Where Pathis my variable and is initializated to specific value.

其中Path是我的变量并初始化为特定值。

How can I do this?

我怎样才能做到这一点?

回答by Ivan Mushketyk

Here is how you can define a propertyin Ant script.

以下是在 Ant 脚本中定义属性的方法

Unfortunately it is not a variable, since they are immutable. You can set a value to it, but you cannot change it during your script execution.

不幸的是,它不是变量,因为它们是不可变的。您可以为其设置一个值,但不能在脚本执行期间更改它。

Hereyou can see an example of assigning value to a property.

在这里,您可以看到为属性赋值的示例。

Update. You can use path task. For example:

更新。您可以使用路径任务。例如:

    <path id="combinedPath">
        <path path="${toString:oldPath}"/>
        <path path="my.jar"/>
    </path>

    <path id="reanamePath">
        <path path="${toString:oldPath}"/>
    </path>

If you change path in one target you can definitely access it in another one.

如果您在一个目标中更改路径,则绝对可以在另一个目标中访问它。

回答by Dave Newton

Ant has classpath-like support built-in.

Ant 内置了类路径支持

In general you wouldn't use a property for this functionality.

通常,您不会为此功能使用属性。

Please consider reading the Ant documentation; this is Ant 101 and it'll be quicker in the long run if you spend some time with the docs and figuring out the Ant way of doing things.

请考虑阅读 Ant 文档;这是 Ant 101,从长远来看,如果您花一些时间在文档上并弄清楚 Ant 的做事方式,它会更快。

回答by David W.

Ant build scripts are written in XML. To create a propertyhas to be in XML style, so instead of this:

Ant 构建脚本是用 XML 编写的。要创建属性必须是 XML 样式,而不是这样:

some_prop="some value"

It is this:

就是这个:

<property name="some_prop"  value="some value"/>

Properties can contain periods, and I recommend using them as name separators:

属性可以包含句点,我建议使用它们作为名称分隔符:

<property name="some.prop"  value="some value"/>

How do you declare a constant? Here:

你如何声明一个常量?这里:

<property name="some.prop"  value="some value"/>

That's because once a property is set, it cannot be changed.

这是因为一旦设置了属性,就无法更改它。

This way, you can do something like this:

这样,您可以执行以下操作:

<property file="${basedir}/build.properties"/>
<property name="some.prop" value="some value"/>

Let's say that the build.propertiesfile contains this line:

假设build.properties文件包含这一行:

some.prop="Some other value"/>

Now, when you run your Ant build file, the value of some.propwill be "Some other value", and the <property name="some.prop" value="some value"/>won't change it. I could even do this:

现在,当您运行 Ant 构建文件时, 的值some.prop将是“其他值”,并且<property name="some.prop" value="some value"/>不会更改它。我什至可以这样做:

$ ant -Dsome.prop="A completely different value"

And this value of the some.propproperty will override what I have in my build.properties file and what I have in my Ant build file.

并且该some.prop属性的值将覆盖我在 build.properties 文件中的内容以及我在 Ant 构建文件中的内容。

This is a very nice feature. It allows me to set a default value that developers can override:

这是一个非常好的功能。它允许我设置开发人员可以覆盖的默认值:

<property name="copy.verbose"  value="false"/>
...
<copy todir="${copy.to.dir}"
    verbose="${copy.verbose}">
    <fileset dir="${copy.from.dir}"/>
</copy>

By default, when my copytask runs, it runs in non-verbose mode which is what I want. However, let's say I am having a few problems with my build, and I want to see exactly what is being copied, I could do this:

默认情况下,当我的copy任务运行时,它以非详细模式运行,这正是我想要的。但是,假设我的构建存在一些问题,我想确切地查看正在复制的内容,我可以这样做:

$ ant -Dcopy.verbose=true

And, now my copy tasks will show me all the files being copied.

而且,现在我的复制任务将显示所有正在复制的文件。



A pathis a way to declare something like $CLASSPATHor $PATHin the command line. You can predeclare a pathwith an id, and then use it later:

一个路径是宣布类似的方式$CLASSPATH或者$PATH在命令行。您可以预先声明一个带有 id的路径,然后稍后使用它:

<javac destdir="${main.destdir}"
    srcdir="${main.srcdir}">
    <classpath>
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
    </classpath>
</javac>

Here I'm adding a classpath. This is using <fileset/>to create a classpath based upon all of the jars in my ${lib.dir}directory.

在这里,我添加了一个类路径。这<fileset/>用于根据我${lib.dir}目录中的所有 jar 创建一个类路径。

I can also do this:

我也可以这样做:

<path id="main.classpath">
    <fileset dir="${lib.dir}">
        <include name="*.jar"/>
    </fileset>
</path>

<javac destdir="${main.destdir}"
    srcdir="${main.srcdir}"
    classpathref="main.classpath"/>

Here, I predeclare my main.classpathand then use that later in my <javac>task.

在这里,我预先声明了 my main.classpath,然后在我的<javac>任务中使用它。

You should read up on Ant in the on line Ant Manual. There is a semi-decent introduction in the manual which might help clarify a few issues for you.

您应该在在线 Ant 手册中阅读有关 Ant 的内容。手册中有一个半体面的介绍,可能有助于为您澄清一些问题。