java - 将 java 系统属性传递给 ant 测试

标签 java ant

我让 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)

最佳答案

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

试试这个:

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

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

关于java - 将 java 系统属性传递给 ant 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13451045/

相关文章:

java - 主要方法是空的?

java - Spring Batch ItemReader使用PostgreSQL函数游标错误

java - 应该在哪里进行身份验证 : "Webserver or AppServer"?

java - 为什么在 HQL 中不允许插入显式值?

javascript - Ant构建如何排除未缩小的js并包含缩小的js文件

ant - 遍历 Ant 目录中的所有文件名

java - 如果从同一文件调用,服务将被销毁,但如果创建该服务的对象(并从单独的文件调用),则服务不会被销毁

java - 在 ant 脚本中包含外部 JAR 时出错

gwt - 加快gwt编译过程

regex - Ant 正则表达式(regexp 元素)模式中的转义字符是什么