簡體   English   中英

我可以讓maven生成兩個具有不同分類器的測試罐嗎?

[英]Can I get maven to generate two test jars with different classifiers?

我有一個maven項目,我為一些模塊生成一些測試代碼。 我希望這個生成的測試代碼可供其他模塊測試。 通常,如果模塊要使用模塊foo的測試代碼,則foo模塊必須生成foo-tests.jar ,並且模塊添加依賴項,例如:

<dependency>
  <artifactId>foo</artifactId>
  <classifier>tests</classifier>
  <scope>test</scope>
</dependency>

這很好,除了我只想提取生成的foo測試代碼,而不是所有foo的單元測試和輔助類(例如,可能存在意外的類沖突)。 我想定義一個依賴項,例如:

<dependency>
  <artifactId>foo</artifactId>
  <classifier>test-libs</classifier>
  <scope>test</scope>
</dependency>

通常maven-jar-plugin用於生成foo-test工件,所以我希望我可以配置該插件來生成兩個測試工件:一個包含foo-tests中常用的測試代碼(單元測試等)和一個它包含foo-test-libs中生成的代碼,使用兩個不同的分類器,例如:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-test-jar</id>
      <phase>package</package>
      <goals><goal>test-jar</goal></goals>
      <configuration>
        <excludes>... all the generated code ...</excludes>
      </configuration>
    </execution>
    <execution>
      <id>generate-test-libs-jar</id>
      <phase>package</package>
      <goals><goal>test-jar</goal></goals>
      <classifier>test-libs</classifier>
      <configuration>
        <includes>... all the generated code ...</includes>
      </configuration>
    </execution>
  </executions>
</plugin> 

這里的問題是,與jar目標不同, maven-jar-plugin測試jar目標不支持分類器元素。 我假設目標默認使用測試分類器,所以我不能生成兩個具有不同分類器的測試罐。

我想知道是否有一個很好的方法來拆分maven模塊的測試罐? 如果所有其他方法都失敗了,我可以回去在完整的測試jar上添加依賴項,但我希望有一個更優雅的解決方案。

(而且我知道使用分類器是不受歡迎的,但我不確定這里是否可以避免......)

我想我已經弄清楚了,使用maven-antrun-plugin組合創建多個測試jar文件(使用jar任務),以及build-helper-maven-plugin將生成的jar文件作為工件附加到項目中。

  1. 生成測試jar文件。

我使用maven-antrun-plugin結合ant jar任務來分割我的測試代碼:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals><goal>run</goal></goals>
      <configuration>
        <target>
          <!-- The test-libs jar contains all test code in the x.y.z package. -->
          <jar destfile="${project.build.directory}/artifacts/test-libs/${project.build.finalName}.jar"
               basedir="${project.build.directory}/test-classes"
               includes="x/y/z/**/*.class"/>
          <!-- The tests jar contains all test code exception the x.y.z package. -->
          <jar destfile="${project.build.directory}/artifacts/tests/${project.build.finalName}.jar"
               basedir="${project.build.directory}/test-classes"
               excludes="x/y/z/**/*.class"/>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

請注意,我必須為每個生成的jar文件使用相同名稱$ {project.build.finalName} .jar (這在以后很重要),所以我將每個jar文件放入其自己的目錄中:

  • 目標/偽影/測試庫/ foo.jar中
  • 目標/偽影/測試/ foo.jar中

    1. 將jar文件附加到項目。

生成jar文件只是第一步。 需要將jar文件附加到maven項目,以便安裝它們。 為此,需要build-helper-maven-plugin 這允許將文件附加為工件,其中每個文件具有位置,類型和分類器。 這里的類型是jar ,分類器將是testtest-libs

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.9</version>
  <executions>
    <execution>
      <id>attach-test-jars</id>
      <phase>package</phase>
      <goals><goal>attach-artifact</goal></goals>
      <configuration>
        <artifacts>
          <!-- Attach the test-libs artifact. -->
          <artifact>
            <file>${project.build.directory}/artifacts/test-libs/${project.build.finalName}.jar</file>
            <type>jar</type>
            <classifier>test-libs</classifier>
          </artifact>
          <!-- Attach the tests artifact. -->
          <artifact>
            <file>${project.build.directory}/artifacts/tests/${project.build.finalName}.jar</file>
            <type>jar</type>
            <classifier>tests</classifier>
          </artifact>
        </artifacts>
      </configuration>
    </execution>
  </executions>
</plugin>

有了這個我現在看到以下輸出:

[INFO] Installing ...\target\artifacts\test-libs\foo-1.0.jar to ...\foo\1.0\foo-1.0-test-libs.jar
[INFO] Installing ...\target\artifacts\tests\foo-1.0.jar to ...\foo\1.0\foo-1.0-tests.jar
[INFO] Installing ...\target\foo-1.0.jar to ...\foo\1.0\foo-1.0.jar

我的其他項目現在可以根據需要在foo-test.jarfoo-test-lib.jar上添加依賴項。 好哇!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM