ant 脚本中如何设置jdk的路径

我现在机子开发时 环境变量是jdk1.4的 有个工程代码需要用jdk1.5编译 每次用ant编译时 都要去改环境变量 如何在build脚本中 设置jdk版本 避免来回改环境变量
[b]问题补充:[/b]
我现在机子开发时 环境变量是jdk1.4的 有个工程代码需要用jdk1.5编译 每次用ant编译时 都要去改环境变量 如何在build脚本中 设置jdk版本 避免来回改环境变量

cats_tiger
<!--这一句改成jdk目录-->

这里我一般是放编译所需的库

jones
你的方法我试了 不起作用 能再具体点吗

bohemia
你的方法我测试通过了 在ant task 里也找到具体说明
http://ant.apache.org/manual/CoreTasks/javac.html

cats_tiger
我是在1.4的系统环境下 去编译1.5 如果是1.5 编译 1.4 这个方法就行了

上一站,火星
source 好像高版本编译低版本好用

谢谢各位的解答

方法1:
直接修改ANT的运行文件:ant.bat ,设置JAVA_HOME,这样所有的ANT都使用设定的JDK也是一种方法; 我以前做Jetty也是这么做的,挺方便;

方法2:
destdir="${build}"
[color=red]fork="yes"[/color]
[color=red]executable="/opt/java/jdk1.1/bin/javac"[/color]
compiler="javac1.5"
/>

这样就可以制定特定的java编译器了. :)

[code="xml"]

<!--这一句改成jdk目录-->




destdir="${build.dir}/source_class"
debug="true"
deprecation="true"
optimize="false" >
<!--设置classpath-->



[/code]

直接在ant使用的build.xml中用
[code="java"][/code]
覆盖你的系统环境变量即可,[color=red]如果build.xml中有和系统环境变量同名的变量,会使用build.xml中的进行覆盖[/color]

你这种情况应该直接覆盖JAVA_HOME变量就OK了

It is possible to use different compilers. This can be specified by either setting the global build.compiler property, which will affect all tasks throughout the build, or by setting the compiler attribute, specific to the current task. Valid values for either the build.compiler property or the compiler attribute are:

* classic (the standard compiler of JDK 1.1/1.2) – javac1.1 and javac1.2 can be used as aliases.
* modern (the standard compiler of JDK 1.3/1.4/1.5/1.6) – javac1.3 and javac1.4 and javac1.5 and javac1.6 can be used as aliases.
* jikes (the Jikes compiler).
* jvc (the Command-Line Compiler from Microsoft's SDK for Java / Visual J++) – microsoft can be used as an alias.
* kjc (the kopi compiler).
* gcj (the gcj compiler from gcc).
* sj (Symantec java compiler) – symantec can be used as an alias.
* extJavac (run either modern or classic in a JVM of its own).

The default is javac1.x with x depending on the JDK version you use while you are running Ant. If you wish to use a different compiler interface than those supplied, you can write a class that implements the CompilerAdapter interface (package org.apache.tools.ant.taskdefs.compilers). Supply the full classname in the build.compiler property or the compiler attribute.

The fork attribute overrides the build.compiler property or compiler attribute setting and expects a JDK1.1 or higher to be set in JAVA_HOME.

You can also use the compiler attribute to tell Ant which JDK version it shall assume when it puts together the command line switches - even if you set fork="true". This is useful if you want to run the compiler of JDK 1.1 while you current JDK is 1.2+. If you use compiler="javac1.1" and (for example) depend="true" Ant will use the command line switch -depend instead of -Xdepend.

其实你可以一直用jdk1.5,编译原来那些1.4的项目的时候,用jvmarg节点设置编译的级别。我没有设置过级别,但是用过jvmarg
[code="xml"]


[/code]

ant官方文档上写:
给javac任务加上compiler="javac1.4"

好多方法呀,我土了

在javac task中指定source属性为1.5,指定1.5位置即可:

[code="xml"][/code]

有一点必须要清楚,ant运行的时候默认是从哪里找到javac.exe这个应用程序的,默认应该是从环境变量的path中(windows默认的搜索路径),知道这个就好办多了,path的配置中哪个JDK目录在前就会调用哪个,在你的build.xml中通过property把你要用的JDK的bin目录路径加到系统path的前面拼在一起让ant用,这个应该没啥问题吧,让你的JDK在path的最前面

[code="java"]我是在1.4的系统环境下 去编译1.5[/code]
建议你换一下,在1.5环境下,编译1.4