How to display the classpath build in an Ant Junit

2019-08-01 06:48发布

An Ant junit task that has worked for months is suddenly failing with a NoClassDefFoundError for classes that used to be found. Is there a way to display the classpath that is built in the junit task?

<target name="basic-junit-test" description="Run a single JUnit test. ">

    <junit printsummary="yes" fork="no" haltonfailure="yes">
        <classpath>
            <pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/> 
            .
            . many other pathelements
            .
        </classpath>
        <test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
    </junit>
</target>

标签: ant
3条回答
干净又极端
2楼-- · 2019-08-01 07:27

This is not really the answer to my question, Mark O'Connor and Rebse gave excellent answers, instead this is a more thorough explanation of what happened to cause me to ask the question in the first place. I had an ANT Junit task that I had used to develop about 100 controller classes. I used it for the first time in several months and every test failed with a classNotFound exception. The class that was not found was one that I was sure should be on the classpath, it was a locally created jar file that is automatically picked up for the build. I thought that somehow the classpath was at fault so I wanted to display it when a test was run.

After many attempts to figure out what was going on I put a try block around the code that was producing the classNotFound exception and I saw that the local class was not the class that was not being found. This lead me to a search of the lib directory and eventually (after about six hours) I realized that the problem was that I had replaced an older version of slf4j-api with a newer version. There was a dependency on a method that was in the older version but not in the newer.

查看更多
姐就是有狂的资本
3楼-- · 2019-08-01 07:38

I'm a big fan of declaring my Ant paths at the top of my build and using classpath references in the various tasks.

To pathconvert task can be used to print the classpath contents as a property:

<path id="test.path">
    <pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/> 
    .
    . many other pathelements
    .
</path>

<target name="echo-path" description="Echo test path">
    <pathconvert targetos="unix" property="test.path.unix" refid="test.path">
    <echo message="Test path: ${test.path.unix}"/>
</target>

<target name="basic-junit-test" depends="echo-path" description="Run a single JUnit test. ">

    <junit printsummary="yes" fork="no" haltonfailure="yes">
        <classpath>
            <path refid="test.path"/>
        </classpath>
        <test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
    </junit>
</target>

Update

Just occurred to me: an even simpler solution might be to run Ant in debug mode.

查看更多
戒情不戒烟
4楼-- · 2019-08-01 07:45

Mark's answer just helped me, but just a quick note as I needed to close the pathconvert xml element when copying the example in the answer.

<pathconvert targetos="unix" property="test.path.unix" refid="test.path" />
查看更多
登录 后发表回答