如何在Maven项目部署中包含依赖性测试jar?

时间:2011-10-13 09:14:09

标签: java deployment maven dependencies

我在foo群组下有两个项目foo-webcom.examplefoo-web取决于foo

为了能够开发应用程序的UI部分而不依赖于外部服务,虚拟DAO在foo中实现(它们返回静态数据,因此我们不必连接到数据库等)。

我们被要求将虚拟类移动到src/test/java。这意味着他们不会使用foo.jar部署到从Web项目构建的战争中。我在maven网站上找到了 these instructions ,但它们似乎对我不起作用。

foo pom.xml我有:

        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <executions>
            <execution>
              <id>test-jar</id>
              <phase>test-compile</phase>
              <goals>
                <goal>test-jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

mvn install上运行foo-web时,在foo的目标中,我会得到两个广告:foo-1.0.0-SNAPSHOT.jarfoo-1.0.0-SNAPSHOT-tests.jar。它们都在本地maven存储库中安装得很好。

之前,foo-web依赖关系看起来像这样:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

这会在战争中触发foo-1.0.0-SNAPSHOT.jar的部署。现在,我还想部署-tests jar,最好只用于“本地”配置文件。

我尝试了各种方法:

<profile>
    <id>local</id>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>foo</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>test-jar</type>
        </dependency>
    </dependencies>
</profile>

这会导致使用不同名称部署源jar:com.example-foo.jar并且不部署测试jar。我也尝试在依赖项中使用<classifier>而不是<type>,但它仍然会这样做。我尝试在配置文件之外使用上面的依赖项(和其他配置文件一样),但它仍然表现相同。

如果我将<type>添加到主依赖项(不添加其他依赖项),我将部署测试jar(与上面的名称相同),但源代码自然不会部署。 / p>

与文档中所写内容的唯一区别在于未为测试依赖项指定范围。它是否仅适用于test范围?我可以以某种方式以不同方式部署测试类。

我知道这个问题有点令人费解,如果有什么我可以澄清的话,请告诉我。

谢谢!


更新

我在几个方面尝试了它,但它仍然无效。

我在foo项目(依赖项,而不是主要的web项目)的maven-jar-plugin中添加了另一个执行,我希望强制maven在同一个jar中编译测试类。主要的和由不同的分类器引用大捆绑。我无法让它发挥作用:

<execution>
  <id>local-build</id>
  <phase>package</phase>
  <goals>
    <goal>jar</goal>
  </goals>
  <configuration>
    <classifier>batman</classifier>
    <directory>${basedir}/src/test/java</directory> <!-- tried several variations here -->
    <includes>
        <include>**</include>
    </includes>
  </configuration>
</execution>

jar是使用batman分类器生成的,但我找不到任何方法让它在jar目标中包含测试类。

这样做,我意识到这不依赖于test-jar类型/ tests分类器/ test范围关系。当我尝试指定我正在构建的新jar时,我的行为与尝试包含-tests jar时的行为相同。我检查了本地maven存储库,并且依赖项目中的所有jar都安装得很好,所以问题是主项目的依赖项解析。

TL;博士

如果您可以在多个分类器中包含相同的依赖关系,那么一切都归结为问题。从我看到的直到现在,答案是否定的 - 当使用不同的分类器多次指定相同的依赖项时,我总是得到com.example-foo jar。

5 个答案:

答案 0 :(得分:48)

最好在第一个模块中配置maven pom文件

<project>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

此后,mvn安装/发布版还将部署工件foo-1.0.0-SNAPSHOT-tests.jar

然后使用分类器配置对测试jar的依赖性(如在其他响应中建议的那样)

<dependency>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <type>test-jar</type>
    <!-- uncomment if needed in test scope only
         <scope>test</scope>
    -->
</dependency>

答案 1 :(得分:4)

我看到您使用test-jar作为type而您使用的是classifier而不是type,但也可能使用了test-jar ...但您是否尝试过以下

<dependency>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <classifier>tests</classifier>
    <scope>test</scope>
</dependency>

答案 2 :(得分:1)

我发现一个有用的解决方案是使用 build-helper plugin 来获取我正在为本地配置文件构建的程序集中的测试文件夹。这是在依赖项目中:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <!-- used to package the dummy daos when building 
         with the local profile -->
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${extra.sources.dir}</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

该配置文件,也在依赖项目中:

<profile>
    <id>local</id>
    <properties>
        <env.resources.dir>src/test/resources</env.resources.dir>
        <extra.sources.dir>src/test/java</extra.sources.dir>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.test</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</profile>

这样,主项目的pom不会被修改,并且在本地构建时会部署存根。

但我还是没有发现你是否可以部署多个分类器。 :)

答案 3 :(得分:1)

我参加派对有点晚了,但我希望这有助于某人:你可以包含同一依赖项的多个类型。假设您的项目取决于common-artifact-1.0.jar,并且还有一个测试jar common-artifact-1.0-tests.jar

您可以通过以下方式导入jar和测试jar:

<dependencies>
    <dependency>
        <groupId>my.corp</groupId>
        <artifactId>common-artifact</artifactId>
        <version>1.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>my.copr</groupId>
        <artifactId>common-artifact</artifactId>
        <version>1.0</version>
        <type>test-jar</type>
        <scope>test</scope> <!-- the "compile" scope also works here -->
    </dependency>
</dependencies>

答案 4 :(得分:0)

如果您在运行时需要jar,您可能还想尝试以下技巧。这只是在编译时将依赖项直接复制到目标文件夹。我希望在制作包装时它们会被包括在内......我自己没有测试过它

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>             
      <id>copy-dependencies</id>
      <phase>compile</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <artifactSet>
          <includes>
        <include>com.example:foo</include>
          </includes>
        </artifactSet>
        <outputDirectory>target/dependencies</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin> 
相关问题