如何让Maven插件作为在Eclipse中运行JUnit测试的一部分执行?

如何让Maven插件作为在Eclipse中运行JUnit测试的一部分执行?,maven,junit,maven-plugin,eclipse-mars,jpamodelgen,Maven,Junit,Maven Plugin,Eclipse Mars,Jpamodelgen,我正在使用EclipseMars、JUnit4.11、Hibernate4.3.6.Final和JPA2.1。我已经使用m2e插件将一个Maven项目导入Eclipse。我的pom.xml文件中有这个插件 <plugin> <groupId>org.bsc.maven</groupId> <artifac

我正在使用EclipseMars、JUnit4.11、Hibernate4.3.6.Final和JPA2.1。我已经使用m2e插件将一个Maven项目导入Eclipse。我的pom.xml文件中有这个插件

                    <plugin>
                            <groupId>org.bsc.maven</groupId>
                            <artifactId>maven-processor-plugin</artifactId>
                            <version>2.1.1</version>
                            <executions>
                                    <execution>
                                            <id>process</id>
                                            <goals>
                                                    <goal>process</goal>
                                            </goals>
                                            <phase>generate-sources</phase>
                                            <configuration>
                                                    <processors>
                                                            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                                                    </processors>
                                            </configuration>
                                    </execution>
                            </executions>
                            <dependencies>
                                    <dependency>
                                            <groupId>org.hibernate</groupId>
                                            <artifactId>hibernate-jpamodelgen</artifactId>
                                            <version>4.3.10.Final</version>
                                    </dependency>
                            </dependencies>
                    </plugin>

错误所抱怨的类是使用我上面列出的插件生成的,但是由于某种原因,当我运行JUnit测试时(通过在Eclipse编辑器中右键单击类名并选择“RunAs”->“JUnit测试”),插件不会被调用。如何强制我的插件作为在Eclipse中执行JUnit测试的一部分运行?

在maven中不会出现任何错误,因为生成源代码生命周期阶段是在测试之前完成的。maven确保,当您按照配置执行插件时,生成的源代码在编译阶段可用,因此在测试期间将作为编译类可用。只有在修改持久性类中的任何内容时,而不是在执行单元测试时,才需要重新生成。我可以想到一件事:在eclipse中是否正确导入了生成的sources文件夹?我记得,如果文件系统中还不存在源文件夹,eclipse有时无法识别来自maven的源文件夹。可能执行mvn干净编译,然后再次执行m2e。或者手动将生成的源文件夹作为源文件夹添加到eclipse项目中。希望有帮助;-)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyProjectOrganizationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.mainco.subco.myproject.service.MyProjectOrganizationService org.mainco.subco.myproject.controller.MyProjectOrganizationController.m_MyProjectOrgSvc; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyProjectOrgService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.mainco.subco.myproject.repo.MyProjectOrganizationDao org.mainco.subco.myproject.service.MyProjectOrganizationServiceImpl.m_MyProjectOrganizationDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyProjectOrganizationDaoImpl' defined in file [/Users/davea/Documents/sb_workspace/core/target/classes/org/mainco/subco/myproject/repo/MyProjectOrganizationDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.mainco.subco.myproject.repo.MyProjectOrganizationDaoImpl]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: 
    The import org.mainco.subco.myproject.domain.MyProjectOrganization_ cannot be resolved
    The import org.mainco.subco.organization.domain.Organization_ cannot be resolved
    The import org.mainco.subco.url.domain.Url_ cannot be resolved
    MyProjectOrganization_ cannot be resolved to a variable
    MyProjectOrganization_ cannot be resolved to a variable
    MyProjectOrganization_ cannot be resolved to a variable
    MyProjectOrganization_ cannot be resolved to a variable
    Organization_ cannot be resolved to a variable
    Url_ cannot be resolved to a variable
    Organization_ cannot be resolved to a variable
    MyProjectOrganization_ cannot be resolved to a variable
    MyProjectOrganization_ cannot be resolved to a variable

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:102)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:246)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 25 more