传递Java系统属性到Ant测试

5

我正在尝试使用以下代码执行一个jar包

  <target name="start.my.jar" description="start my jar">
  <echo message="Starting the jar" />
  <java jar="${jars.dir}/${my.stub.jar}" fork="true" dir="${jars.dir}" spawn="true">
            <sysproperty key="properties.filename" value="${basedir}/path/path/path/filename.properties"/>
        <arg value="start" />
    </java>
</target>

在这个jar包里面有一个类,它的代码如下:
public static MyFacade createFacade() throws FileNotFoundException, IOException {
    return createFacade(System.getProperty(properties.filename));
}    

然后有一个ant测试目标,配置如下

 <target name="test" depends="compile, support" description="perform unit tests">
    <mkdir dir="${build.test}" />
    <mkdir dir="${test-classes.dir}" />
    <javac srcdir="${build.test}" destdir="${test-classes.dir}" debug="${debug}" nowarn="${nowarn}" includeantruntime="false" deprecation="${deprecation}">
        <classpath refid="main.classpath" />
    </javac>
    <junit printsummary="yes" haltonfailure="yes" showoutput="true" dir="${test-classes.dir}" fork="true" forkmode="perBatch" failureproperty="junit.failure" errorproperty="junit.error" haltonerror="no">
        <jvmarg value="-Xmx1G" />
        <jvmarg value="-Dcom.sun.management.jmxremote" />

        <classpath>
            <pathelement location="${test-classes.dir}" />
            <pathelement location="${classes.dir}" />
            <!--
            For module restful_api
            -->
            <pathelement location="${build.deploy}" />
            <pathelement location="${classes.dir}" />
            <fileset dir="${build.lib}">
                <include name="*.jar" />
            </fileset>
        </classpath>
        <formatter type="plain" />
        <batchtest fork="yes" todir="${build.test}">
            <fileset dir="${build.test}">
                <include name="**/*AllTests.java" />
                <include name="**/*TestCase.java" />
            </fileset>
        </batchtest>
    </junit>
    <fail message="Unittest failures - please check" if="junit.failure" />
    <fail message="Unittest errors - please check" if="junit.error" />
</target>

在这个测试目标模块中,我的测试未能获取我在start.my.jar目标上指定的文件属性。我做错了什么吗?

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
[junit] java.lang.NullPointerException
[junit]     at java.io.File.<init>(File.java:222)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.getInputStream(MyFacadeFactory.java:47)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.loadFacade(MyFacadeFactory.java:43)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.createFacade(MyFacadeFactory.java:32)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.createFacade(MyFacadeFactory.java:28)
[junit]     at com.mycompany.myproduct.sdk.resources.impl.TransactionResourceImpl.<init>(TransactionResourceImpl.java:70)
[junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
[junit]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
[junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
[junit]     at com.sun.jersey.server.spi.component.ResourceComponentConstructor._construct(ResourceComponentConstructor.java:191)

1
尝试返回createFacade(System.getProperty("properties.filename")); - user1097489
我犯了一个错误,实际上应该是properties.filename,但仍然无法工作。 - ThaSaleni
你能贴出堆栈跟踪吗? - user1097489
我刚刚执行了@user1097489,甚至为该值添加了system.out.println,但它打印出null...这表明它没有获取到该属性。 - ThaSaleni
1
没有注意到。尝试使用System.getProperties()查看您拥有哪些属性。您是否尝试过带引号的"properties.filename"? - user1097489
2个回答

4
这是因为你只为独立的运行时环境java目标添加了属性。而junit目标指定了一个新的环境(由于你设置了一些JVM开关,你需要指定系统属性)。
尝试这样做:
<junit ...>
    <sysproperty key="properties.filename"
            value="${basedir}/path/path/path/filename.properties"/>
    ....
</junit>

另一种选择是每次使用-Dproperties.filename=...键运行ant任务(您可以在Eclipse的外部运行配置中设置)。然而,缺点是您必须每次记住这个操作(例如,在CI构建或新的检出中运行任务)。


我已经做过了,但仍然得到了一个空指针……也许我应该贴出堆栈跟踪。 - ThaSaleni
我必须将属性作为配置传递给构建文件,因为我需要它在CI构建中完全自动化。 - ThaSaleni
有趣的是,这个方法可行。 <jvmarg value="-Dproperties.filename=${basedir}/path/path/path/filename.properties"/> 我之前用过的那个方法从来都不起作用。 - ThaSaleni
有人能把我的最后一条评论发表为答案吗?我无法发布答案。 - ThaSaleni
你们没有一个“回答自己的问题”按钮吗? - rlegendi
每当我尝试回答任何问题时,都会收到以下消息:糟糕!无法提交您的答案,因为:抱歉,我们不再接受来自此帐户的答案。请访问http://goo.gl/C1Kwu了解更多信息。 - ThaSaleni

1
有趣的是,这个有效:
 <jvmarg value="-Dproperties.filename=${basedir}/path/path/path/filename.properties"/>

我之前使用的那个:

 <sysproperty key="" value=""/>

从未工作过


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接