2015-02-24 56 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项目重新安装到存储库(跳过签名的构建)以避免编译错误。