[英]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文件作為工件附加到項目中。
我使用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中
生成jar文件只是第一步。 需要將jar文件附加到maven項目,以便安裝它們。 為此,需要build-helper-maven-plugin 。 這允許將文件附加為工件,其中每個文件具有位置,類型和分類器。 這里的類型是jar ,分類器將是test或test-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.jar或foo-test-lib.jar上添加依賴項。 好哇!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.