java org.springframework.test 也无法在存在 maven 依赖的情况下解决

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13321400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:17:49  来源:igfitidea点击:

org.springframework.test cannot be resolved also with maven dependency present

javamavenspring-transactionsspring-test

提问by user1781028

I'm having an issue while trying to build test suites for my Spring application. I'm new to Maven and I can't find what is wrong. I've added to my pom.xml

我在尝试为 Spring 应用程序构建测试套件时遇到问题。我是 Maven 的新手,我找不到问题所在。我已经添加到我的 pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.0.0.RELEASE</version>
    <scope>test</scope> 
</dependency>

but I'm still getting from Eclipse the error: The import org.springframework.test cannot be resolved.

但我仍然从 Eclipse 中得到错误:The import org.springframework.test cannot be resolved.

Following this discussion ( http://appfuse.547863.n4.nabble.com/spring-test-package-not-found-td1596479.html), I tried to add <scope>provided<scope>without success.

在此讨论之后(http://appfuse.547863.n4.nabble.com/spring-test-package-not-found-td1596479.html),我尝试添加<scope>provided<scope>但没有成功。

Here is my complete pom.xml:

这是我的完整内容pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mose</groupId>
<artifactId>emergency-alert-server</artifactId>
<name>emergency-alert-server</name>
<version>1.0.0-BUILD-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.0.5.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.1</org.slf4j-version>
</properties>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>   

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Hymanson JSON Processor -->
    <dependency>
        <groupId>org.codehaus.Hymanson</groupId>
        <artifactId>Hymanson-mapper-asl</artifactId>
        <version>1.7.6</version>
    </dependency>

    <!-- JAXB XML Processor -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.2</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- File Upload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.4</version>
    </dependency>

    <!-- Test-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency> 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.0.0.RELEASE</version>
        <scope>test</scope> 
    </dependency>
    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.10</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.2.2</version>
        <scope>test</scope>
    </dependency>

    <!-- MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>   
    <!-- Hibernate framework -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
    <!-- Google Cloud Messaging for server  -->
    <dependency>
        <groupId>com.google.android.gcm</groupId>
        <artifactId>gcm-server</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

<repositories>
    <!-- For testing against latest Spring snapshots -->
    <repository>
        <id>org.springframework.maven.snapshot</id>
        <name>Spring Maven Snapshot Repository</name>
        <url>http://maven.springframework.org/snapshot</url>
        <releases><enabled>false</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <!-- For developing against latest Spring milestones -->
    <repository>
        <id>org.springframework.maven.milestone</id>
        <name>Spring Maven Milestone Repository</name>
        <url>http://maven.springframework.org/milestone</url>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>
    <!-- For Spring releases -->
    <repository>
        <id>org.springframework.maven.release</id>
        <name>Spring Maven Release Repository</name>
        <url>http://maven.springframework.org/release</url>
        <snapshots><enabled>false</enabled></snapshots>         
    </repository>
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </repository>
    <repository>
        <id>gcm-server-repository</id>
         <url>https://raw.github.com/slorber/gcm-server-repository/master/releases/</url>
    </repository>       
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>apache.snapshots</id>
        <name>Apache Snapshots</name>
        <url>http://people.apache.org/repo/m2-snapshot-repository</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>                
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>sources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <!-- Have to use version 1.2 since version 1.3 does not appear to work with ITDs -->
            <version>1.2</version>
            <dependencies>
                <!-- You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${org.aspectj-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${org.aspectj-version}</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <outxml>true</outxml>
                <source>${java-version}</source>
                <target>${java-version}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.0-SNAPSHOT</version>
            <!-- 
            <configuration>
                <contextFile>src/main/webapp/META-INF/context.xml</contextFile>
            </configuration> -->
            <dependencies>      
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.9</version>
                </dependency>
            </dependencies>
        </plugin>                       
    </plugins>
</build>

Then I tried to run mvn dependency:tree, but i can't see nothing strange:

然后我试着跑mvn dependency:tree,但我看不出有什么奇怪的:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building emergency-alert-server 1.0.0-BUILD-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ emergency-alert-server ---
[INFO] org.mose:emergency-alert-server:war:1.0.0-BUILD-SNAPSHOT
[INFO] +- org.springframework:spring-orm:jar:3.0.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.5.RELEASE:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-core:jar:3.0.5.RELEASE:compile
[INFO] |  |  +- (org.springframework:spring-asm:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.springframework:spring-jdbc:jar:3.0.5.RELEASE:compile
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-tx:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.springframework:spring-tx:jar:3.0.5.RELEASE:compile
[INFO] |     +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |     +- (org.springframework:spring-aop:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-beans:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-context:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-context:jar:3.0.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:3.0.5.RELEASE:compile
[INFO] |  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-asm:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-expression:jar:3.0.5.RELEASE:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.springframework:spring-asm:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.0.5.RELEASE:compile
[INFO] |  +- (org.springframework:spring-asm:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-context-support:jar:3.0.5.RELEASE:compile
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-context:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-expression:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.springframework:spring-web:jar:3.0.5.RELEASE:compile
[INFO] |     +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-beans:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-context:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:3.0.5.RELEASE:compile - omitted for duplicate)
[INFO] +- org.aspectj:aspectjrt:jar:1.6.10:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.6.1:runtime
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.6.1:runtime - omitted for duplicate)
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.1:runtime
[INFO] |  +- (org.slf4j:slf4j-api:jar:1.6.1:runtime - omitted for duplicate)
[INFO] |  \- (log4j:log4j:jar:1.2.16:runtime - omitted for duplicate)
[INFO] +- log4j:log4j:jar:1.2.16:runtime
[INFO] +- javax.inject:javax.inject:jar:1:compile
[INFO] +- org.codehaus.Hymanson:Hymanson-mapper-asl:jar:1.7.6:compile
[INFO] |  \- org.codehaus.Hymanson:Hymanson-core-asl:jar:1.7.6:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.2.2:compile
[INFO] |  +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- javax.servlet.jsp:jsp-api:jar:2.1:provided
[INFO] +- javax.servlet:jstl:jar:1.2:compile
[INFO] +- commons-fileupload:commons-fileupload:jar:1.2.2:compile
[INFO] +- commons-io:commons-io:jar:1.4:compile
[INFO] +- junit:junit:jar:4.7:test
[INFO] +- org.springframework:spring-test:jar:3.0.0.RELEASE:test
[INFO] +- hsqldb:hsqldb:jar:1.8.0.10:test
[INFO] +- commons-dbcp:commons-dbcp:jar:1.2.2:test
[INFO] |  \- commons-pool:commons-pool:jar:1.3:test
[INFO] +- mysql:mysql-connector-java:jar:5.1.9:compile
[INFO] +- org.hibernate:hibernate-core:jar:3.6.0.Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
[INFO] |  |  \- (org.slf4j:slf4j-api:jar:1.5.8:compile - omitted for conflict with 1.6.1)
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.Final:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.6.1:compile - omitted for duplicate)
[INFO] +- javassist:javassist:jar:3.12.1.GA:compile
[INFO] \- com.google.android.gcm:gcm-server:jar:1.0.2:compile
[INFO]    \- com.googlecode.json-simple:json-simple:jar:1.1:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.642s
[INFO] Finished at: Sat Nov 10 11:44:19 CET 2012
[INFO] Final Memory: 6M/81M
[INFO] ------------------------------------------------------------------------  

Any help would be appreciated, thanks in advance.

任何帮助将不胜感激,提前致谢。

回答by Samuel Rossille

The package org.springframework.transactionis provided by the spring-txartifact, not spring-test.

该包org.springframework.transactionspring-tx工件提供,而不是spring-test.

You will probably get away by adding this to your pom.xml

您可能会通过将其添加到您的 pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

回答by kiwiron

If the Maven build succeeds but Eclipse sees errors, it indicates that Eclipse is not reading the same jars as Maven is importing. Right-click the project and select Maven/update and follow this by a Project/clean.

如果 Maven 构建成功但 Eclipse 看到错误,则表明 Eclipse 未读取与 Maven 导入相同的 jar。右键单击该项目并选择 Maven/update 并在后面添加 Project/clean。

Sometimes the Eclipse (Kepler) Maven plugin fails to download a jar. Check that the missing jar is in your local repository. Often there will just be some files that tell Maven not to attempt another download for a fixed time. If so, delete the repository section for that jar, temporarily set the scope to compile, and do a Maven clean and install. If that does not work, open a command window in the directory of the pom.xml file, and issue a mvn -U clean installcommand.

有时 Eclipse (Kepler) Maven 插件无法下载 jar。检查丢失的 jar 是否在您的本地存储库中。通常只会有一些文件告诉 Maven 在固定时间内不要尝试再次下载。如果是这样,请删除该 jar 的存储库部分,临时设置要编译的范围,然后执行 Maven 清理和安装。如果这不起作用,请在 pom.xml 文件的目录中打开一个命令窗口,并发出mvn -U clean install命令。

Also, your pom.xml looks rather large to me. It is good practice to put common stuff like repository definitions, compiler versions etc int a parent pom that can be shared by all the components of your project. I like to rely on transitive dependencies (e.g. spring-mvc will probably suck in the rest of the Spring framework without you specifying it).

此外,您的 pom.xml 对我来说看起来相当大。将存储库定义、编译器版本等常见内容放入可以由项目的所有组件共享的父 pom 中是一种很好的做法。我喜欢依赖传递依赖(例如 spring-mvc 可能会在没有你指定的情况下吸收 Spring 框架的其余部分)。

回答by jastonitas

The most probably cause of it is a caching error on your maven local repository, you can: 1. Review if you are working with an existing version of java in your eclipse configuration and in the build path of your project. 2. go to your local maven repository, the common path for it is C:\Users[your_user].m2\repository and clean the spring framework dependencies then go to eclipse and launch a maven clean install on your project. 3. save all your project sources and changes and then create a new one put all your sources, clean it and run maven clean install -U.

最可能的原因是 Maven 本地存储库中的缓存错误,您可以: 1. 检查您是否在 eclipse 配置和项目的构建路径中使用现有版本的 java。2. 转到您本地的 maven 存储库,它的常用路径是 C:\Users[your_user].m2\repository 并清理 spring 框架依赖项,然后转到 eclipse 并在您的项目上启动 maven 全新安装。3.保存所有项目源和更改,然后创建一个新的,将所有源,清理并运行 maven clean install -U。