我想使用动物嗅探器根据我自己的 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");
这次我预计第二个项目的构建会失败,因为签名已经改变。它不同于保存在签名文件中的那个。但是构建成功,并且animal-sniffer-plugin仅输出以下两行:
[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 配置错误?