[英]Check an own API with maven animal sniffer plugin
我想使用動物嗅探器根據我自己的API檢查一個類,該API僅包含一個類和一個方法:
package sniffertestapi;
public class MainInterface
{
public static void testMethod(String testString)
{
System.out.println(testString);
}
}
以下簡單的POM用於構建項目:
<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>TestAPI</groupId>
<artifactId>TestAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.13</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<javaHome>C:\Program Files\Java\jdk1.7.0_51</javaHome>
</configuration>
</plugin>
</plugins>
</build>
</project>
構建運行良好,它將TestAPI-0.0.1-SNAPSHOT.signature和jar安裝到我的maven存儲庫中。
接下來,我想添加TestAPI作為依賴項,並使用另一個項目中的testMethod
。
package sniffertest;
import sniffertestapi.MainInterface;
public class Tester
{
public Tester()
{
MainInterface.testMethod("Hi");
}
}
在這個項目中,我添加了帶有另一個目標的動物嗅探器插件:
<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>TestTester</groupId>
<artifactId>TestTester</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.13</version>
<configuration>
<signature>
<groupId>TestAPI</groupId>
<artifactId>TestAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
</signature>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>TestAPI</groupId>
<artifactId>TestAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
當然,此構建也可以成功運行。 現在,我將testMethod
更改為具有兩個參數:
public static void testMethod(String testString, String testString2)
{
System.out.println(testString);
}
並在第二個項目中使用它:
MainInterface.testMethod("Hell", "o");
這次我希望第二個項目的構建失敗,因為簽名已更改。 它不同於保存在簽名文件中的那個。 但是構建成功,並且動物嗅探器插件僅輸出以下兩行:
[INFO] --- animal-sniffer-maven-plugin:1.13:check (default) @ TestTester ---
[INFO] Checking unresolved references to TestAPI:TestAPI:0.0.1-SNAPSHOT
即使我調用了API中未定義的內容,對於一個實例,構建還是成功的(調用mvn test
):
MainInterface.undefinedMethod(1,2,3,4,5);
我用錯了用例,還是因為POM配置錯誤?
感謝@ user944849的提示。 插件將其配置打印到調試日志中:
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal: org.codehaus.mojo:animal-sniffer-maven-plugin:1.13:check (default-cli)
[DEBUG] Style: Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<ignoreDependencies default-value="true"/>
<localRepository>${localRepository}</localRepository>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<project>${project}</project>
<signature>
<groupId>TestAPI</groupId>
<artifactId>TestAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
</signature>
<skip default-value="false">${animal.sniffer.skip}</skip>
</configuration>
[DEBUG] =======================================================================
事實證明,由於存在設置<ignoreDependencies default-value="true"/>
因此忽略了依賴性。 將<ignoreDependencies>true</ignoreDependencies>
放入pom的插件配置中解決了我的問題。
我還必須將修改后的API項目重新安裝到存儲庫中(跳過簽名的構建),以避免編譯錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.