使用maven animal sniffer插件检查自己的API

时间:2015-02-24 08:28:19

标签: java maven animal-sniffer

我想使用动物嗅探器根据我自己的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 个答案:

答案 0 :(得分: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项目重新安装到存储库中(跳过签名的构建)以避免编译错误。