java使用maven动物嗅探器插件检查自己的API
我想使用animal sniffer根据我自己的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。签名并放入我的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 楼答案
谢谢@user944849的提示。插件将其配置打印到调试日志:
事实证明,由于有一个设置
<ignoreDependencies default-value="true"/>
,依赖项被忽略了。将<ignoreDependencies>true</ignoreDependencies>
放入pom中的插件配置解决了我的问题我还必须将修改后的API项目重新安装到存储库中(跳过签名的构建),以避免编译错误