【问题标题】:Maven 2 EclipseMaven 2 日食
【发布时间】:2011-05-04 14:42:37
【问题描述】:

使用 maven2eclipse 插件,问题是零文档。当我在 Eclipse '启用依赖管理'中右键单击项目时,它给了我一个选择,我试过了,但除了以下控制台输出之外,还给了我一个相当稀疏的 POM.xml:

04/05/11 14:57:54 BST: Generating sources /BenCode/pom.xml
04/05/11 14:57:54 BST: Build error for /BenCode/pom.xml; org.apache.maven.plugin.PluginResolutionException: Plugin org.apache.maven.plugins:maven-resources-plugin:2.4.3 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.4.3
04/05/11 14:57:54 BST: Failed to determine compiler source setting, assuming default
04/05/11 14:57:54 BST: Failed to determine compiler target setting, assuming default
04/05/11 14:57:54 BST: Failed to determine compiler source setting, assuming default
04/05/11 14:57:54 BST: Failed to determine compiler target setting, assuming default
04/05/11 14:57:54 BST: Failed to determine compiler test inclusions, assuming defaults
04/05/11 14:57:54 BST: Failed to determine compiler test exclusions, assuming defaults
04/05/11 14:57:54 BST: Failed to determine compiler inclusions, assuming defaults
04/05/11 14:57:54 BST: Failed to determine compiler exclusions, assuming defaults
04/05/11 14:57:54 BST: Refreshing [/BenCode/pom.xml] 

在尝试弄清楚要做什么之前没有使用过 Maven 是一个难题。我的印象是 maven 应该使用该信息扫描类路径依赖项来管理项目,但没有这样的运气:(

【问题讨论】:

    标签: eclipse maven-2 eclipse-plugin maven-plugin


    【解决方案1】:

    Maven 将只管理 POM 中指定的依赖项(以及任何传递依赖项(即您直接定义的依赖项的依赖项)。 例如。下面将在测试范围内添加 JUnit 作为依赖项

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <!-- ... -->
    </dependencies>
    

    如果您右键单击 pom.xml,您可以选择 Maven -> 添加依赖项 - 这将搜索默认存储库。

    另请阅读Introduction to the Dependency Mechanism

    【讨论】:

      【解决方案2】:

      documentation 在那里……但你必须阅读它。

      【讨论】:

        【解决方案3】:

        首先我应该说,虽然我确实使用了 Eclipse 的 m2eclipse 插件,但我实际上只使用了“启用依赖管理”功能;我在 pom.xml 文本编辑器中手工编写的所有其他内容。这只是个人喜好,而不是规则。

        要记住关于 maven 的一点是,它是在“约定优于配置”的基础上工作的——这基本上意味着除非你特别告诉它,否则它会做出很多假设。

        第一个假设是您的目录结构符合以下条件:

        Project Dir
            |
            |- src
            |    |- main
            |    |    |- java
            |    |    |- resources
            |    |
            |    |- test
            |    |    |- java
            |    |    |- resources
            |    |
            |    |- site
            |
            |- pom.xml
        

        (有关目录结构的更多详细信息可以在Introduction to the Standard Directory Layout找到)

        如果你的项目不符合这个结构,那么你需要让Maven知道,例如:

          <build>
            <directory>target</directory>
            <outputDirectory>target/classes</outputDirectory>
            <testOutputDirectory>target/test-classes</testOutputDirectory>
            <sourceDirectory>src/main/java</sourceDirectory>
            <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
            <testSourceDirectory>src/test/java</testSourceDirectory>
            <resources>
              <resource>
                <directory>src/main/resources</directory>
              </resource>
            </resources>
            <testResources>
              <testResource>
                <directory>src/test/resources</directory>
              </testResource>
            </testResources>
          </build>
        

        接下来要担心的是依赖关系...这是 Maven 非常强大的地方,但前提是你真的知道你的依赖关系在哪里,这对于刚开始使用 Maven 的人来说可能会很困惑(它很沮丧见鬼!)。这是添加的几个依赖项(JUnit - 仅用于测试阶段 - 和 log4j)

          <dependencies>
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.8.2</version>
              <scope>test</scope>
            </dependency>
            <dependency>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
              <version>1.2.16</version>
            </dependency>
          </dependencies>
        

        Maven应该足够聪明,可以判断指定依赖的依赖等,所以你应该只需要引用你直接使用的依赖即可。

        如果在 Maven 使用的存储库中找不到您需要的依赖项,您将需要添加更多存储库 - 在这种情况下,Google 是您的朋友。

        这应该可以解决您最初的问题。

        根据您的开发团队的规模,您可能发现值得设置一个本地存储库管理器并让 Maven 使用本地存储库作为所有依赖项的源,它会下载任何没有存储在里面,但那是另一罐蠕虫,因为当您更熟悉 Maven 本身时。

        【讨论】:

          猜你喜欢
          • 2015-06-24
          • 1970-01-01
          • 1970-01-01
          • 2011-10-23
          • 2011-03-03
          • 2011-01-19
          • 2011-08-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多