【问题标题】:Using dependencies of some dependency使用某些依赖项的依赖项
【发布时间】:2018-04-13 02:01:36
【问题描述】:

我有一个 project-test,其中包含在其他项目的 JUnit 测试中使用的所有类(例如构建器测试)。在 project-test 的 pom.xml 中,我添加了 Mockito 和 Junit。

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>br.com.company</groupId>
    <artifactId>project-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>central</id>
            <name>Maven Central</name>
            <url>http://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

注意很多项目需要使用project-test来导入JUnit和Mockito依赖。 Foo 项目 有一个带有项目测试依赖项的 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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>br.com.company</groupId>
    <artifactId>foo</artifactId>
    <packaging>war</packaging>
    <name>foo</name>
    <version>1.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <!-- <defaultGoal>install</defaultGoal> -->
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <version>3.5.1</version>
            </plugin>

        </plugins>
    </build>

    <repositories>
        <repository>
            <!-- Nexus config -->
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>br.com.company</groupId>
            <artifactId>project-test</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

它需要导入一些包,例如:

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

我执行了 mvn clean install/home/danielamorais/.m2/repository/br/com/company/project-test/1.0-SNAPSHOT 中的 .jar存在。为什么我的 Foo 项目中无法导入 Junit 和 Mockito?

错误:(8, 17) java: 包 org.junit 不存在

【问题讨论】:

  • 因为它们是您的项目测试项目的测试依赖项。所以这意味着它们应该只用于测试这个项目,因此不是项目测试 jar 文件的类的依赖项。顺便说一句,我想知道你是如何在最开始编译这些项目测试类的,因为它们应该依赖于 junit 和 mockito,但这些只在测试类路径中。
  • 非常感谢@JBNizet。项目测试类不导入 JUnit 或 Mockito。其实这个类就是这个建设者:natpryce.com/articles/000714.html(我觉得我描述的不对)

标签: java maven


【解决方案1】:

这些是测试范围依赖项,仅在运行测试时适用。

我希望您通过执行mvn test 来测试它。

mvn -Dtest=YourTestCase test

【讨论】:

    【解决方案2】:

    就像 JB NizetNrj 提到的,这是 范围 的问题。检查Maven documentation on the subject

    我只是想补充一点,而不是创建一个全新的项目来对您的常见依赖项进行分组,一个更简单且常见的做法是创建一个 父 POM,然后您的其他 POM 可以从它继承。阅读更多关于它的信息here,还有一对examples

    【讨论】:

      【解决方案3】:

      作为一个工件将另一个工件声明为依赖项,具有test 范围的依赖项不会在声明的工件中继承。

      如果您想要为多个工件定义公共依赖项(和相同的公共配置),请使用旨在:父 pom 的功能。
      定义一个包含通用配置的父 pom,并使您的两个工件成为它的一些子模块。
      如果需要,您还可以将父 pom 设为聚合器/多模块 pom。

      父 pom

      <?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/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <groupId>br.com.company</groupId>
          <artifactId>foo-parent</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <repositories>
              <repository>
                  <id>central</id>
                  <name>Maven Central</name>
                  <url>http://repo1.maven.org/maven2/</url>
              </repository>
          </repositories>
      
          <dependencies>
              <dependency>
                  <groupId>org.mockito</groupId>
                  <artifactId>mockito-all</artifactId>
                  <version>1.10.19</version>
                  <scope>test</scope>
              </dependency>
      
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      
      </project>
      

      项目测试(jar)

      <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>
      
          <parent>
            <groupId>br.com.company</groupId>
            <artifactId>foo-parent</artifactId>
            <version>1.0</version>
          </parent>
      
          <artifactId>project-test</artifactId>
      
      </project>
      

      foo(战争应用)

      <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>
      
          <parent>
            <groupId>br.com.company</groupId>
            <artifactId>foo-parent</artifactId>
            <version>1.0</version>
          </parent>
      
          <packaging>war</packaging>
          <artifactId>foo</artifactId>
      
      
      </project>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-05
        • 2019-04-11
        • 2014-03-18
        • 1970-01-01
        • 1970-01-01
        • 2014-11-19
        • 2021-03-22
        相关资源
        最近更新 更多