maven 發布test jar


很多時候我們自己開發了一個框架(比如查詢引擎),框架自身測試比較復雜(依賴不少核心,而且組件比較多)
同時我們也是暴露core 讓別的開發者可以很好的測試,此時我們可能就需要暴露一個test jar 了(比如dremio等一些重量級的平台工具)
實際上暴露test jar 以及發布test jar 的方法比較簡單

參考demo

  • 代碼結構
 
├── pom.xml
└── src
    ├── main
    ├── java
    └── com
    └── dalong
    └── UserLogin.java
    └── resources
    └── test
        └── java
            └── com
                └── dalong
                    └── TestUserLogin.java 
  • 參考pom.xml
<?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>com.dalong</groupId>
    <artifactId>testpacakge</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

deploy

注意目前是沒有發現類似直接可以通過maven deploy test jar 包的,解決方法是通過mvn deploy:deploy-file 解決
參考

 
mvn deploy:deploy-file -Durl=file://C:\m2-repo \
                       -DrepositoryId=some.id \
                       -Dfile=your-artifact-1.0.jar \
                       [-DpomFile=your-pom.xml] \
                       [-DgroupId=org.some.group] \
                       [-DartifactId=your-artifact] \
                       [-Dversion=1.0] \
                       [-Dpackaging=jar] \
                       [-Dclassifier=test] \
                       [-DgeneratePom=true] \
                       [-DgeneratePom.description="My Project Description"] \
                       [-DrepositoryLayout=legacy]

使用

參考如下:推薦type 以及classifier 都加上

<dependency>
    <groupId>com.dalong</groupId>
    <artifactId>testpacakge</artifactId>
    <version>1.0-SNAPSHOT</version>
    <classifier>tests</classifier>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

說明

以上默認的模式是有一些問題的,比如test 的依賴傳遞,實際上maven 官方推薦是將test 代碼放到src/main/java 中,對於jar 使用一個test 命名
同時對於依賴test 包的項目添加scope 為test,比如dremio 的test 包就存在類似的問題,它在處理test 包的時候就使用了類似的方法,但是對於test 包
處理上傳了,目前暫時還沒找到比較好的方法,如果真的需要將test 包共享,推薦還是參考官方的模式(創建獨立的jar)

參考資料

https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html
https://maven.apache.org/pom.html
https://maven.apache.org/plugins/maven-deploy-plugin/usage.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM