【问题标题】:Passing java system properties to ant tests将 java 系统属性传递给 ant 测试
【发布时间】:2019-08-02 21:33:05
【问题描述】:

我让 ant 使用以下代码执行一个 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)

【问题讨论】:

  • 尝试返回 createFacade(System.getProperty("properties.filename"));
  • 我犯了一个错误,它实际上是 properties.filename 但仍然无法正常工作
  • 你能发布堆栈跟踪吗?
  • 我刚刚做了@user1097489 我什至为该值添加了一个 system.out.println 并且它正在打印 null... 这表明它没有获得该属性
  • 没注意到。尝试执行 System.getProperties() 以查看您拥有哪些属性。您是否尝试过带引号的“properties.filename”?

标签: java ant


【解决方案1】:

这是因为您只为java 目标添加了属性,这是一个独立的运行时环境。 junit 目标指定了一个新环境(当您为其设置一些 JVM 开关时,您也需要指定系统属性)。

试试这个:

<junit ...>
    <sysproperty key="properties.filename"
            value="${basedir}/path/path/path/filename.properties"/>
    ....
</junit>

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

【讨论】:

  • 我已经这样做了,仍然得到一个空指针......也许我也应该发布堆栈跟踪
  • 我必须将该属性作为配置传递给构建文件,因为我需要它完全自动用于 CI 构建
  • 有趣的是,这有效 我之前使用的那个' 没用过
  • 有人可以将我最后的评论作为答案发布我无法发布答案
  • 你没有回答你自己的问题按钮吗?
【解决方案2】:

这很有趣:

 <jvmarg value="-Dproperties.filename=${basedir}/path/path/path/filename.properties"/>

我之前用过的那个:

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

没用过

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2015-03-18
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多