如何在Intellij IDEA 11中构建maven导入的项目?

时间:2013-01-08 12:06:45

标签: maven intellij-idea

我通过导入maven项目创建了一个新的IntelliJ项目。哪个工作正常: enter image description here

我可以从Maven Projects窗格(IDE屏幕左侧)执行maven目标。例如,我可以使用编译目标编译项目。

现在我想使用 Build | Make Project 选项从UI构建项目。但是,它不起作用。它抱怨丢失的应用程序包,好像它没有看到依赖项。这些依赖项在相应的pom.xml文件中定义,但是从IDE中创建项目似乎忽略它。

我需要做些什么才能让它发挥作用?

修改

好的,我已从家中删除了 .m2 目录,并从Maven Projects弹出窗口启动了编译目标。我可以在控制台窗格中看到maven从Maven Central下载依赖项(我只给出了几行日志,它很长):

"C:\Program Files (x86)\Java\jdk1.6.0_29\bin\java" -Dclassworlds.conf=O:\java\apache-maven-3.0.4\bin\m2.conf -Dmaven.home=O:\java\apache-maven-3.0.4 -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 11.1.2\bin" -Dfile.encoding=UTF-8 -classpath "O:\java\apache-maven-3.0.4\boot\plexus-classworlds-2.4.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 11.1.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher --no-plugin-registry --fail-fast --strict-checksums --update-snapshots -f C:\dev\shunra\Application\Builder\build\vcat\pom.xml compile
[WARNING] Command line option -npr is deprecated and will be removed in future Maven versions.
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.shunra:VCat-build:pom:1.0.0.SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 7, column 11
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] util.logging
[INFO] Infra
[INFO] dtos
[INFO] commons
[INFO] license
[INFO] core
[INFO] vcat
[INFO] VCat-build
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building util.logging 0.0.1
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.pom (7 KB at 7.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/19/maven-plugins-19.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/19/maven-plugins-19.pom (11 KB at 38.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/19/maven-parent-19.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/19/maven-parent-19.pom (25 KB at 70.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/apache/9/apache-9.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/apache/9/apache-9.pom (15 KB at 63.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.jar (26 KB at 63.1 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/2.3.2/maven-compiler-plugin-2.3.2.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/2.3.2/maven-compiler-plugin-2.3.2.pom (8 KB at 29.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/18/maven-plugins-18.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/18/maven-plugins-18.pom (13 KB at 53.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom (23 KB at 66.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/apache/7/apache-7.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/apache/7/apache-7.pom (15 KB at 59.5 KB/sec)

所以,我认为连接到Maven Central没有问题。

EDIT2

请观察: enter image description here 注意问题 - 从Maven导入了两个模块 - Common Core 。他们的Maven工件ID分别是 commons core 。 IntelliJ似乎在工件Id和模块名称之间混淆,因为:

  1. “项目”弹出按钮显示模块名称,后跟括号中相应的maven工件ID。
  2. 在“项目设置”对话框中,模块仅由相应的maven工件Id提供。
  3. 但在模块依赖项中,它仅列出模块名称。
  4. Core 模块(maven artifact Id 核心)应该会看到依赖 Common 模块(maven artifact Id commons ),但它没有,这就是问题。

    EDIT3

    这是最顶层的pom.xml:

    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.shunra</groupId>
        <name>VCat-build</name>
        <version>1.0.0.SNAPSHOT</version>
        <artifactId>VCat-build</artifactId>
        <packaging>pom</packaging>
        <modules>
            <module>../../../util.logging</module>
            <module>../../../Infra</module>
            <module>../../../DTOs</module>
            <module>../../../ShunraLicense</module>
            <module>../../../Common</module>
            <module>../../../Core</module>
            <module>../../../VCat</module>
        </modules>
        <build>
            <plugins>
            </plugins>
        </build>
    </project>
    

    以下是Common模块的一个:

    <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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.shunra</groupId>
      <artifactId>commons</artifactId>
      <version>0.0.1</version>
      <dependencies>
            <dependency>
                <groupId>com.sun.jna</groupId>
                <artifactId>jna</artifactId>
                <version>3.0.9</version>
            </dependency>
    
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>1.4</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-math</artifactId>
                <version>2.2</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.9.4</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.2.7</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                    <groupId>com.shunra</groupId>
                    <artifactId>util.logging</artifactId>
                    <version>0.0.1</version>
            </dependency>
            <dependency>
                    <groupId>com.google.code.gson</groupId>
                    <artifactId>gson</artifactId>
                    <version>1.6</version>
                    <scope>compile</scope>
            </dependency>
            <dependency>
                    <groupId>commons-codec</groupId>
                    <artifactId>commons-codec</artifactId>
                    <version>1.6</version>
                    <scope>compile</scope>
            </dependency>
            <dependency>
                  <groupId>org.restlet.jse</groupId>
                  <artifactId>org.restlet</artifactId>
                  <version>2.0.10</version>
            </dependency>
            <dependency>
              <groupId>javax.inject</groupId>
              <artifactId>javax.inject</artifactId>
              <version>1</version>
              <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>commons-configuration</groupId>
                <artifactId>commons-configuration</artifactId>
                <version>1.8</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>1.8.3</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
    <!--        <dependency>
                <groupId>com.shunra</groupId>
                <artifactId>license</artifactId>
                <version>0.0.1</version>
            </dependency> -->
            <dependency>
                  <groupId>com.shunra</groupId>
                  <artifactId>dtos</artifactId>
                  <version>0.0.1</version>
            </dependency>
      </dependencies>
        <repositories>
        <repository>
           <id>maven-restlet</id>
           <name>Public online Restlet repository</name>
           <url>http://maven.restlet.org</url>
        </repository> 
       </repositories>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
          <resource>
            <directory>src</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
              <source/>
              <target/>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    以下是Core模块的一个:

    <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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.shunra</groupId>
      <artifactId>core</artifactId>
      <version>0.0.1</version>
      <dependencies>
        <dependency>
          <groupId>com.shunra</groupId>
          <artifactId>commons</artifactId>
          <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.shunra</groupId>
            <artifactId>license</artifactId>
            <version>0.0.1</version>
        </dependency>
      </dependencies>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
          <resource>
            <directory>src</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
              <source/>
              <target/>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    EDIT4

    终于解决了。删除了所有Intellij文件并重新导入。我猜pom.xml文件的变化方式让IntelliJ感到困惑。

1 个答案:

答案 0 :(得分:5)

查看屏幕截图,您似乎没有离线工作,这是导致此问题的最常见原因。

  1. 尝试从Maven Projects弹出窗口下载源代码和文档(它是左侧的第三个图标)。这通常会唤醒它并导致下载。

  2. 使用运行时配置执行手动Maven构建。选择编辑配置| Maven然后确保您的工作目录是您的项目根目录,并选择“清理包”的目标。这将迫使Intellij直接通过Maven重建,这可能会唤醒内部后台构建过程。

  3. 检查您的设置并确保Intellj的Maven已正确读取POM中的存储库信息,并能够找到Maven Central。您的本地settings.xml可能存在阻止下载的问题。

  4. 您还可以删除.idea和.iml文件以强制重新导入项目。

  5. 重启Intellij并希望最好......

  6. 作为最后的手段,通过File |使您的缓存无效使缓存无效。这可能会使您的开发速度降低一些,直到通过使用重建它们为止。

  7. 修改以涵盖更新的问题

    看来您的POM文件有点混乱。它们不遵循标准Maven directory layout,因此Intellij可能会在您的目录名称和内部artifactId条目之间混淆。通常,父POM(包含模块)放在项目的根目录中,模块在下面形成目录层次结构。

    通过一些广泛的设置配置,您可以强制Intellij使用现有结构,但更好的解决方案是重命名目录以匹配artifactIds并移动模块,以便它们形成Maven反应器项目。

    根据评论进一步修改

    似乎是Intellij项目同步。删除.idea目录和.iml文件就可以了。我已相应更新了列表。