如何在Intellij IDEA 12中设置一个maven项目

8

我遇到了一个问题,无法将Netbeans创建的Maven项目导入IDEA 12中。在pom文件中写的依赖项无法正常工作。它告诉我在“import org.springframework.xxx;”处出现“无法解析符号'springframework'”。当我运行它时,它说org.springframework.xxx不存在。

这是pom文件:

<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>cn.edu.seu.cose.jellyjolly</groupId>
<artifactId>jellyjolly-openshift-dist</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>Jelly Jolly Openshift Distribution</name>
<organization>
    <name>College of Software Engineering, Southeast University</name>
    <url>http://cose.seu.edu.cn/</url>
</organization>
<repositories>
    <repository>
        <id>eap</id>
        <url>http://maven.repository.redhat.com/techpreview/all</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>maven-restlet</id>
        <name>Public online Restlet repository</name>
        <url>http://maven.restlet.org</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>eap</id>
        <url>http://maven.repository.redhat.com/techpreview/all</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <netbeans.hint.license>gpl30</netbeans.hint.license>
</properties>
<dependencies>
    <dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-6.0</artifactId>
        <version>3.0.0.Final-redhat-1</version>
        <type>pom</type>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.3.RELEASE</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.restlet.jee</groupId>
        <artifactId>org.restlet</artifactId>
        <version>2.0.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.restlet.jee</groupId>
        <artifactId>org.restlet.ext.xml</artifactId>
        <version>2.0.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>net.java.dev.rome</groupId>
        <artifactId>rome</artifactId>
        <version>1.0.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>
</dependencies>
<profiles>
    <profile>
        <!-- When built in OpenShift the 'openshift' profile will be used when
                    invoking mvn. -->
        <!-- Use this profile for any OpenShift specific customization your app
                    will need. -->
        <!-- By default that is to put the resulting archive into the 'webapps'
                    folder. -->
        <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
        <id>openshift</id>
        <build>
            <finalName>jellyjolly</finalName>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <outputDirectory>webapps</outputDirectory>
                        <warName>ROOT</warName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>


应该很简单,独立于Netbeans。可以检查一下通用的Maven设置(文件->设置->Maven),尝试重新导入项目(右键单击项目->Maven->重新导入)。同时,请检查一下pom.xml文件中是否有一些“错误”。也许有一个“错误”被Netbeans忽略了,但是在IDEA中却没有被忽略。 - Adrian
实际上,pom文件可能存在错误。提示是“无法获取依赖项信息:无法读取工件'javax.activation:activation:jar'的元数据文件:无法解析依赖项'c3p0:c3p0:jar'的版本'[0.9.1.2,0.9.1.2-redhat-1]':范围违反版本排序:[0.9.1.2,0.9.1.2-redhat-1],来自指定的远程存储库:central、eap等...依赖路径xxx:xxx:war...”。这是否意味着某些工件无法从列出的存储库中检索到? - Wenhao Ji
非常奇怪的行为。请重新检查IDEA使用的Maven版本,也许您可以尝试使用外部版本覆盖内部版本。 - Adrian
另外,最好使用具体的版本号代替范围表示法,例如使用0.9.1.2而不是[0.9.1.2, 0.9.1.2-redhat-1]等。 - Adrian
在IDEA的Maven设置中启用Maven 3是否有帮助? - CrazyCoder
4个回答

9

7
如果项目在命令行运行(mvn clean install),那么它在IntelliJ 12上的工作机会就有100%了 :-)
在 "文件->项目导入->从外部Maven模型" 后,您应该可以看到模块。
我会检查IntelliJ设置中maven主目录的位置("文件->设置->Maven")以及其读取的settings.xml文件(或未读取的文件)。
IDE中的mvn install不应与命令行上发生的情况有所不同。如果是这样,可以比较命令行和IntelliJ(也许还包括Netbeans)的mvn help:effective-settings输出。
如果您只看不到所有模块,请右键单击parent-pom.xml并选择maven->重新导入。要解决依赖项,您必须执行一次安装。它不会在所有情况下自动解决依赖项。

尽管在pom文件上出现了一个错误提示,指出“无法获取依赖信息:无法读取工件'javax.activation:activation:jar'的元数据文件:无法解析依赖项'c3p0:c3p0:jar'的版本'[0.9.1.2,0.9.1.2-redhat-1]'... 依赖路径xxx:xxx:war ...”,但mvn clean install进程成功完成,并以退出代码0结束。我想知道这些工件是否被从远程mvn存储库下载。看来这些工件在这些存储库中找不到。 - Wenhao Ji
可能它们是有问题的。因此,我建议删除本地仓库并重试。同时使用 help:effective-pom 和 help:effective-settings 命令检查 Maven 中央仓库是否在你的仓库列表中。 - wemu
谢谢你的帮助。正如你所说,本地仓库中的构件已损坏。Spring框架和其他依赖已经解决,但是不在中央仓库中的构件的依赖关系尚未解决。非中央仓库是否应该在pom.xml和settings.xml中声明? - Wenhao Ji
1
你使用于多个项目的额外仓库应该放入settings.xml文件中。大多数情况下,最好不要将仓库添加到pom.xml文件中,因为这与特定项目相关。由于仓库是基础设施,所以settings.xml是更好的选择。一旦您使用maven代理,您只需要更新settings.xml文件而不是pom.xml文件。您只需要将它们添加到settings.xml文件或pom.xml文件中 - 不要在两个位置都添加。 - wemu
我刚刚移除了jboss-javaee-6.0的依赖,现在它可以正常工作了。谢谢。 - Wenhao Ji

0

我使用 mvn -U idea:idea 命令生成了带有依赖项的 IntelliJ 专用文件。


0

遇到了相同的问题, 你可以直接右键点击代码,然后选择Maven->重新导入,这样就可以解决问题了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接