2015-02-24 26 views
2

我想用動物嗅探器來檢查一類對我自己的API,其中只包含一個類和一個方法:請與Maven的動物嗅探器插件的自己的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的配置錯誤?

+1

您是否試過用'-X'運行?插件的調試信息可能會幫助您確定發生了什麼。 – user944849 2015-02-24 21:51:39

回答

0

感謝@ 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項目重新安裝到存儲庫(跳過簽名的構建)以避免編譯錯誤。