将参数从 build.xml 传递到 java 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6545007/
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
passing parameter from build.xml to java file
提问by TechFind
when I run java test file(single file) through eclipse by passing arguments
当我通过传递参数通过 Eclipse 运行 java 测试文件(单个文件)时
-DappRoot=ECM -DappName=ESW -Dapp.module=FNT -Dapp.env=LOC -DcloneNumber=1
-DappRoot=ECM -DappName=ESW -Dapp.module=FNT -Dapp.env=LOC -DcloneNumber=1
test file executes without error, if I dont give arguments error occurs as Could not resolve placeholder 'appRoot'.
测试文件执行没有错误,如果我不给参数错误发生,因为无法解析占位符“appRoot”。
I have junit target to generate report in html format.
我有 junit 目标来生成 html 格式的报告。
<target name="junit" depends="init-junit">
<junit printsummary="on" fork="yes" forkmode="perBatch" haltonfailure="false" failureproperty="junit.failure" showoutput="false">
<classpath>
<path refid="CLASSPATH_JUNIT"/>
</classpath>
<batchtest fork="no" todir="${TEST_BUILD_DIR}">
<fileset dir="${COMP_TEST_SRC}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
<formatter type="xml" />
</junit>
<junitreport todir="${JUNIT_REPORT}">
<fileset dir="${TEST_BUILD_DIR}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${JUNIT_REPORT}"/>
</junitreport>
</target>
When I run above build script, getting following error: Could not resolve placeholder 'appRoot' where this has passed as an arguments.
当我在构建脚本上方运行时,收到以下错误:无法解析占位符“appRoot”,它已作为参数传递。
I have passed parameter to build.xml through eclipse, this parameter passes to build.xml file but not passing to java files. How can I solve this?
我已经通过 eclipse 将参数传递给 build.xml,这个参数传递给 build.xml 文件但不传递给 java 文件。我该如何解决这个问题?
EDIT:tried with following parameter:
编辑:尝试使用以下参数:
<junit printsummary="on" fork="yes" forkmode="perBatch" haltonfailure="false" failureproperty="junit.failure" showoutput="false">
<jvmarg value="-DappRoot=ECM" />
<jvmarg value="-DappName=ESW" />
<jvmarg value="-Dapp.module=FNT" />
<jvmarg value="-Dapp.env=LOC" />
<jvmarg value="-DcloneNumber=1" />
<!--<sysproperty key="appRoot" value="${appRoot}"/>
<sysproperty key="appName" value="${appName}"/>
<sysproperty key="app.module" value="${app.module}"/>
<sysproperty key="app.env" value="${app.env}"/>
<sysproperty key="cloneNumber" value="${cloneNumber}"/>-->
<classpath>
<path refid="CLASSPATH_JUNIT"/>
</classpath>
with system paramter it works fine but takes very long time to execute. with jvmarg it doesnt work. same error with
使用系统参数它可以正常工作,但需要很长时间才能执行。使用 jvmarg 它不起作用。同样的错误
<jvmarg value="-DappRoot=${appRoot}" />
http://ant.apache.org/manual/Tasks/junit.htmldoesn't define any restriction for sys and jvm args.
http://ant.apache.org/manual/Tasks/junit.html没有为 sys 和 jvm 参数定义任何限制。
采纳答案by JB Nizet
The documentation of the junit ant taskshows it:
junit ant 任务的文档显示:
<junit fork="yes" ...>
<jvmarg value="-DappRoot=ECM" />
</junit>
If the appRoot property is passed as a system property to ant, you may access it as an ant property:
如果 appRoot 属性作为系统属性传递给 ant,您可以将其作为 ant 属性访问:
<junit fork="yes" ...>
<jvmarg value="-DappRoot=${appRoot}" />
</junit>
回答by Jason Day
You have set fork="no"
in your batchtest
element, which is overriding the fork
setting in the junit
element. This is causing the junit
task to execute in the same JVM as the ant process, which means the jvmarg
parameters will be ignored.
您已fork="no"
在batchtest
元素中进行设置,这将覆盖元素中的fork
设置junit
。这导致junit
任务与 ant 进程在同一个 JVM 中执行,这意味着jvmarg
参数将被忽略。
I would also recommend using a fork mode of "once"; this will vastly improve the performance.
我还建议使用“一次”的分叉模式;这将大大提高性能。
Try this:
试试这个:
<junit printsummary="on" fork="yes" forkmode="once" maxmemory="512m"
haltonfailure="false" failureproperty="junit.failure"
showoutput="false">
<jvmarg value="-DappRoot=ECM" />
<jvmarg value="-DappName=ESW" />
<jvmarg value="-Dapp.module=FNT" />
<jvmarg value="-Dapp.env=LOC" />
<jvmarg value="-DcloneNumber=1" />
<classpath>
<path refid="CLASSPATH_JUNIT"/>
</classpath>
<batchtest todir="${TEST_BUILD_DIR}">
<fileset dir="${COMP_TEST_SRC}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
<formatter type="xml" />
</junit>
Note the maxmemory
attribute. Adjust the value as necessary.
注意maxmemory
属性。根据需要调整值。