Introduction to Animal Sniffer Maven Plugin – 动物嗅探器Maven插件简介

最后修改: 2017年 9月 29日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

While working in Java, there are times when we need to use multiple language versions at the same time.

在用Java工作时,有时我们需要同时使用多个语言版本。

It’s common to need our Java program to be compile-time compatible with one Java version (say – Java 6) but to need to use a different version (say – Java 8) in our development tools and a maybe different version to run the application.

常见的情况是,我们需要我们的Java程序在编译时与一个Java版本(比如说–Java 6)兼容,但在我们的开发工具中需要使用不同的版本(比如说–Java 8),而在运行应用程序时可能又需要不同的版本。

In this quick article, we’ll demonstrate how easy it is to add Java version-based incompatibility safeguards and how the Animal Sniffer plugin can be used to flag these issues at build time by checking our project against previously generated signatures.

在这篇快速文章中,我们将演示如何轻松地添加基于Java版本的不兼容保障措施,以及如何使用Animal Sniffer插件在构建时通过检查我们的项目与先前生成的签名来标记这些问题。

2. Setting -source and -target of the Java Compiler

2.设置Java编译器的-源-目标

Let’s start with a hello world Maven project – where we’re using Java 7 on our local machine but we’d like to deploy the project to the production environment which is still using Java 6.

让我们从一个hello worldMaven项目开始–我们在本地机器上使用Java 7,但我们想把项目部署到仍在使用Java 6的生产环境中。

In this case, we can configure the Maven compiler plugin with source and target fields pointing to Java 6.

在这种情况下,我们可以配置Maven编译器插件,将sourcetarget字段指向Java 6。

The “source” field is used for specifying compatibility with Java language changes and “target” field is used to for specifying compatibility with JVM changes.

“source”字段用于指定与Java语言变化的兼容性,“target”字段则用于指定与JVM变化的兼容性。

Let’s now look at Maven compiler configuration of pom.xml:

现在我们来看看Maven编译器对pom.xml的配置:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
	<configuration>
            <source>1.6</source>
            <target>1.6</target>
	</configuration>
    </plugin>
</plugins>

With Java 7 on our local machine and Java code printing “hello world” to the console, if we go ahead and build this project using Maven, it will build and work correctly on a production box running Java 6.

如果我们在本地机器上使用Java 7,并在控制台打印 “hello world “的Java代码,如果我们继续使用Maven构建这个项目,它将在运行Java 6的生产箱上构建并正常工作。

3. Introducing API Incompatibilities

3.介绍API的不相容性

Let’s now look at how easy it is to introduce API incompatibility by accident.

现在让我们来看看,意外引入API不兼容是多么容易。

Let’s say we start working on some new requirement and we use some API features of Java 7 which were not present in Java 6.

比方说,我们开始研究一些新的需求,我们使用Java 7的一些API功能,这些功能在Java 6中是不存在的。

Let’s look at the updated source code:

让我们来看看更新后的源代码。

public static void main(String[] args) {
    System.out.println("Hello World!");
    System.out.println(StandardCharsets.UTF_8.name());
}

java.nio.charset.StandardCharsets was introduced in Java 7.

java.nio.charset.StandardCharsets是在Java 7中引入的。

If we now go ahead and execute the Maven build, it will still compile successfully but fail at runtime with linkage error on a production box with Java 6 installed.

如果我们现在继续执行Maven构建,它仍然会编译成功,但在安装了Java 6的生产箱上运行时却会出现链接错误。

The Maven documentation mentions this pitfall and recommends to use Animal Sniffer plugin as one of the options.

Maven 文档提到了这个隐患,并建议使用Animal Sniffer插件作为其中一个选项。

4. Reporting API Compatibilities

4.报告API的兼容性

Animal Sniffer plugin provides two core capabilities:

Animal Sniffer插件提供两个核心功能。

  1. Generating signatures of the Java runtime
  2. Checking a project against API signatures

Let’s now modify the pom.xml to include the plugin:

现在让我们修改pom.xml以包括该插件。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>animal-sniffer-maven-plugin</artifactId>
    <version>1.16</version>
    <configuration>
        <signature>
            <groupId>org.codehaus.mojo.signature</groupId>
            <artifactId>java16</artifactId>
            <version>1.0</version>
        </signature>
    </configuration>
    <executions>
        <execution>
            <id>animal-sniffer</id>
            <phase>verify</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Here, the configuration section of Animal Sniffer refers to an existing Java 6 runtime signature. Also, execution section checks and verifies the project source code against the given signature and flags if any issues are found.

在这里,Animal Sniffer的配置部分提到了一个现有的Java 6运行时签名。同时,执行部分根据给定的签名检查和验证项目源代码,如果发现任何问题,则标记。

If we go ahead and build the Maven project, the build will fail with the plugin reporting signature verification error as expected:

如果我们继续构建Maven项目,构建会失败,插件会如期报告签名验证错误。

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:animal-sniffer-maven-plugin:1.16:check 
(animal-sniffer) on project example-animal-sniffer-mvn-plugin: Signature errors found.
Verify them and ignore them with the proper annotation if needed.

5. Conclusion

5.结论

In this tutorial, we explored the Maven Animal Sniffer plugin and how it can be used to report API related incompatibilities if any at build time.

在本教程中,我们探讨了Maven Animal Sniffer插件,以及如何在构建时利用它来报告API相关的不兼容情况。

As always, the full source code is available over on GitHub.

一如既往,完整的源代码可在GitHub上获得