• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

xvik/gradle-animalsniffer-plugin: Gradle AnimalSniffer plugin

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

xvik/gradle-animalsniffer-plugin

开源软件地址(OpenSource Url):

https://github.com/xvik/gradle-animalsniffer-plugin

开源编程语言(OpenSource Language):

Groovy 96.9%

开源软件介绍(OpenSource Introduction):

gradle-animalsniffer-plugin

License CI Appveyor build status codecov

About

Gradle AnimalSniffer plugin for Java or Groovy projects. AnimalSniffer is used to check compatibility with lower Java versions (when compiling with a newer Java version) or Android (SDK version).

It is implemented the same way as core Gradle quality plugins (Checkstyle, PMD etc): A task is registered for each source set (animalsnifferMain, animalsnifferTest) and attached to the check task.

Advanced features:

Applicability

If you're using JDK 9 or above then you can use the --release flag instead of the plugin:

compileJava {
  options.compilerArgs.addAll(['--release', '7'])
}

Direct release option support available in gradle 6.6 (feature discussion).

The plugin could still be useful:

  • For Android projects to check API compatibility (because Android API signatures are published).
  • To check strong compatibility with some library: you'll need to generate signatures for this library and will be able to use them to check project compatibility (on API level, ofc) with older library versions.
Summary
  • Configuration extensions:
    • animalsniffer - check configuration
    • animalsnifferSignature - signature build configuration (optional)
  • Tasks:
    • check[Main] - check source set task
    • animalsnifferSignature - build signature (active when animalsnifferSignature configuration declared)
    • type:BuildSignatureTask - custom signature build task may be used to merge signatures
    • type:SignatureInfoTask - view signature "contents"
  • Dependencies configuration: signature - signatures for check

Setup

Maven Central Gradle Plugin Portal

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'ru.vyarus:gradle-animalsniffer-plugin:1.5.4'
    }
}
apply plugin: 'ru.vyarus.animalsniffer'

OR

plugins {
    id 'ru.vyarus.animalsniffer' version '1.5.4'
}

Compatibility

IMPORTANT: The plugin only works when the java (or java-library) or groovy plugin is enabled, otherwise nothing will be registered. There is no support for the Android plugin (the java plugin must be used to perform the animalsniffer check).

The plugin is compiled for Java 8, and is compatible with Java 11.

Gradle Version
5-6 1.5.4
4.x 1.4.6

Usage

Additional tasks will be assigned to the check task. So animalsniffer checks will be executed during:

$ gradlew check

Signatures

AnimalSniffer requires a signature file to check against. To define a signature (or multiple signatures) use the signature configuration.

To check Java version compatibility:

repositories { mavenCentral() }
dependencies {
    signature 'org.codehaus.mojo.signature:java16:1.1@signature'
}

Java signatures

To check Android compatibility:

repositories { mavenCentral() }
dependencies {
    signature 'net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature'
}

Android signatures

To check both Java version and Android compatibility:

dependencies {
    signature 'org.codehaus.mojo.signature:java16:1.1@signature'
    signature 'net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature'
}

In the last case animalsniffer will run 2 times for each signature. You may see the same errors two times if a class/method is absent in both signatures. Each error message in the log (and file) will also contain the signature name to avoid confusion.

When no signatures are defined animalsniffer tasks will always pass.

You can also use custom libraries signatures to check version compatibility.

Scope

All project dependencies are excluded from the analysis: only classes from your source set are checked.

By default, all source sets are checked. To only check main sources:

animalsniffer {
    sourceSets = [sourceSets.main]
}

Output

Violations are always printed to the console. Example output:

2 AnimalSniffer violations were found in 1 files. See the report at: file:///myproject/build/reports/animalsniffer/main.text

[Undefined reference] invalid.(Sample.java:9)
  >> int Boolean.compare(boolean, boolean)

[Undefined reference] invalid.(Sample.java:14)
  >> java.nio.file.Path java.nio.file.Paths.get(String, String[])

NOTE: text report file will contain simplified report (error per line):

invalid.Sample:9  Undefined reference: int Boolean.compare(boolean, boolean)
invalid.Sample:14  Undefined reference: java.nio.file.Path java.nio.file.Paths.get(String, String[])

NOTE: when multiple signatures are used, output will contain the signature name in the error message to avoid confusion.

Suppress violations

An annotation could be used to suppress violations: examples

Default annotation

Add dependency on the annotation artifact:

implementation "org.codehaus.mojo:animal-sniffer-annotations:1.16"

Use provided scope if you can. Annotation is configured by default, so you can simply use annotation to suppress violation:

@IgnoreJRERequirement
private Optional param;
Custom annotation

You can define your own annotation:

package com.mycompany

@Retention(RetentionPolicy.CLASS)
@Documented
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
public @interface SuppressSignatureCheck {}

Configure annotation:

animalsniffer {
    annotation = 'com.mycompany.SuppressSignatureCheck'
}

Now check will skip blocks annotated with your annotation:

@SuppressSignatureCheck
private Optional param;

Extend signature

Your project could target multiple Java versions and so reference classes, not present in a signature.

For example, your implementation could try to use Java 7 Paths and if the class is not available, fall back to the Java 6 implementation. In this case Paths could be added to the ignored classes:

animalsniffer {
    ignore 'java.nio.file.Paths'
}

Now usages of Paths will not cause warnings.

Multiple ignored classes could be defined:

animalsniffer {
    ignore 'java.nio.file.Paths', 'some.other.Class'
}

Or

animalsniffer {
    ignore 'java.nio.file.Paths'
    ignore 'some.other.Class'
}

Or by directly assigning collection:

animalsniffer {
    ignore  = ['java.nio.file.Paths', 'some.other.Class']
}

Entire packages could be ignored using asterisk:

animalsniffer {
    ignore 'some.pkg.*'
}

See more info in the documentation.

Configuration

Configuration example:

animalsniffer {
    toolVersion = '1.20'
    sourceSets = [sourceSets.main]
    ignoreFailures = true
    reportsDir = file("$project.buildDir/animalsnifferReports")
    annotation = 'com.mypackage.MyAnnotation'
    ignore = ['java.nio.file.Paths']
}

There are no required configurations - the plugin will generate defaults for all of them.

Property Description Default value
toolVersion AnimalSniffer version 1.20
sourceSets Source sets to check all source sets
ignoreFailures False to stop build when violations found, true to continue false
reportsDir Reports directory file("$project.buildDir/reports/animalsniffer")
annotation Annotation class to avoid check under annotated block
ignore Ignore usage of classes, not mentioned in signature
signatures Signatures to use for check configurations.signature
excludeJars Patterns to exclude jar names from classpath. Required for library signatures usage
cache Cache configuration By default, cache disabled

NOTE: ignore does not exclude your classes from check, it allows you to use classes not mentioned in the signature. See more details above.

Tasks

The animalsniffer task is registered for each source set:

  • animalsnifferMain - run AnimalAniffer for compiled main classes
  • animalsnifferTest - run AnimalSniffer for compiled test classes
  • animalsnifferSourceSet - run AnimalSniffer for compiled SourceSet classes

The check task will depend only on tasks from configured in animalsniffer.sourceSets source sets.

Tasks support text report, enabled by default.

To disable reports for a task:

animalsnifferMain.reports.text.enabled = false

or for all tasks:

tasks.withType(AnimalSniffer) {
    reports.text.enabled = false
}

Animalsniffer task is a SourceTask and may be configured to include/exclude classes from check.

NOTE: The task operates on compiled classes and not sources! Be careful when defining patterns.

For example, to exclude classes in a 'invalid' subpackage from check:

animalsnifferMain {
    exclude('**/invalid/*')
}

Advanced features

Read wiki for advanced features:

Might also like


gradle plugin generator




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
snapcrafters/gradle发布时间:2022-06-18
下一篇:
dave-ellis/gradle-swagger-plugin: A gradle plugin for swagger发布时间:2022-06-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap