@带有SpEL的CacheReceive注释在Eclipse编译之后工作,但在Ant编译之后不工作 问题

@带有SpEL的CacheReceive注释在Eclipse编译之后工作,但在Ant编译之后不工作 问题,eclipse,spring,ant,Eclipse,Spring,Ant,我有一个使用Spring@cacheexecute注释和嵌入式Spring表达式语言的类。当我允许Eclipse自动编译这个类时,一切都很好。但是,当我使用Ant任务(通过Eclipse或从命令行)编译时,生成的.class文件不起作用,并抛出一个异常,这看起来像是在转移注意力 我的问题:如何配置Ant构建,使其生成工作类工件(以便其他开发人员可以在不需要Eclipse的情况下构建我的项目)?Eclipse和Ant之间的配置似乎是相同的,但我肯定在某些地方缺少了一些属性 版本 Ant(与Ecl

我有一个使用Spring@cacheexecute注释和嵌入式Spring表达式语言的类。当我允许Eclipse自动编译这个类时,一切都很好。但是,当我使用Ant任务(通过Eclipse或从命令行)编译时,生成的.class文件不起作用,并抛出一个异常,这看起来像是在转移注意力

我的问题:如何配置Ant构建,使其生成工作类工件(以便其他开发人员可以在不需要Eclipse的情况下构建我的项目)?Eclipse和Ant之间的配置似乎是相同的,但我肯定在某些地方缺少了一些属性

版本
  • Ant(与Eclipse捆绑):org.apache.Ant_1.8.3.v20120321-1730
  • Eclipse:Juno服务版本120120920-0800
  • JDK:1.7.0_17
  • JUnit:4.10(in/lib/of项目)
  • 操作系统:Windows 7 64位
  • 春季:3.2.2
支持文件 因为如果没有你面前的项目,这个问题很难描述,所以我将我的项目归结为再现这个问题所需的最少文件:

/src/sample/SampleAction.java:包含使用@cacheexecute注释和SpEL的方法

package sample;
import org.springframework.cache.annotation.CacheEvict;

public class SampleAction {
    @CacheEvict(value = "sampleCache", allEntries = true, condition = "#string.bytes != null")
    public void triggerCache(String string) {
        System.out.println("triggerCache(" + string + ")");
    }
}
/src/sample/SampleActionTest.java:一种单元测试,可用于Eclipse工件,但无法用于Ant工件

package sample;
import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"application.xml"})
public class SampleActionTest {
    @Autowired
    private SampleAction sampleAction;

    @Test
    public void testCacheMethod() {
        sampleAction.triggerCache("Definitely not a null string.");
    }
}
/src/sample/application.xml:缓存和操作类的Spring定义文件

<cache:annotation-driven />
<bean id="sampleAction" class="sample.SampleAction" />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="sampleCache" />
        </set>
    </property>
</bean>
/build.xml:清除、编译并运行失败的单元测试的Ant构建

<path id="classpath.main">
    <fileset dir="lib" includes="*.jar" />
</path>
<path id="classpath.test">
    <path refid="classpath.main" />
    <pathelement location="output/classes" />
</path>

<target name="compileFailure" description="Cleans, compiles, and runs a unit test, which fails.">
    <delete quiet="true" dir="output/classes" />
    <mkdir dir="output/classes" />      
    <copy tofile="output/classes/sample/application.xml" file="src/sample/application.xml" />
    <javac srcdir="src" destdir="output/classes"
        classpathref="classpath.main" source="1.6" target="1.6" includeantruntime="false" />
    <junit printsummary="yes">
        <classpath refid="classpath.test" />
        <formatter type="plain" usefile="false"/>
        <test name="sample.SampleActionTest" outfile="result" />
    </junit>
</target>
此存根项目还可以作为ZIP存档提供,包括Spring和JUnit的支持JAR文件:

复制步骤
  • 在Eclipse中打开“compilerDebug”项目
  • 允许Eclipse在“sample”包中自动构建类。要么执行项目清理/构建,要么打开Java文件,编辑空白,然后重新保存它们。使用Eclipse编译时,output/classes/sample/SampleAction.class为911字节
  • 右键单击“SampleActionTest”并选择“作为…JUnit测试运行”测试成功通过。
  • 在Ant视图中打开build.xml文件。右键单击“compileFailure”目标并选择“以…Ant Build运行”测试失败“EL1007E:(位置0):在null”堆栈跟踪上找不到字段或属性“bytes”。使用Ant编译时,output/classes/sample/SampleAction.class为694字节
  • 其他意见
  • 我曾尝试将compiler=“org.eclipse.jdt.core.JDTCompilerAdapter”添加到“javac”任务中,以强制Ant使用eclipse编译器,但在这一更改之后,单元测试仍然失败。SampleAction.class在这里是696字节
  • 如果我从@cacheexecute注释中删除该条件,那么两个编译器都会通过单元测试
  • 将源/目标级别设置为1.5或1.7无效
  • 强制Ant任务“在与工作区相同的JRE中运行”没有效果
  • 下载独立的Ant发行版(1.9.1)并运行完全与Eclipse隔离的Ant构建没有任何效果
  • 用声明性XML配置(使用aspectjtools)替换@CacheRecit注释没有效果
  • 提前感谢您帮助我避免了不可避免的精神错乱

    更新 见下面提供的答案。使用javac编译时,Ant没有显式地将调试模式设置为true。SpEL要求在类文件中包含额外的调试信息,以便正确处理注释。将debug设置为true立即更正了该问题

    <javac debug="true" [...] />
    
    
    
    似乎SpEL需要java编译器来设置类文件中的调试信息才能工作。在启用maven build中生成的调试信息后,我遇到了一个问题。Eclipse编译器会自动执行此操作。

    似乎SpEL需要java编译器来设置类文件中的调试信息才能正常工作。在启用maven build中生成的调试信息后,我遇到了一个问题。Eclipse编译器会自动执行此操作。

    这正是问题所在!这正是问题所在!
    com.springsource.org.aopalliance-1.0.0.jar
    commons-logging-1.1.3.jar
    junit-4.10.jar
    spring-aop-3.2.2.jar
    spring-beans-3.2.2.jar
    spring-context-3.2.2.jar
    spring-context-support-3.2.2.jar
    spring-core-3.2.2.jar
    spring-expression-3.2.2.jar
    spring-test-3.2.2.jar
    
    <javac debug="true" [...] />