Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Revapi #3974

Merged
merged 7 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/modules/ROOT/pages/javalib/publishing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ This page will discuss common topics around publishing your Java projects for ot

include::partial$example/javalib/publishing/2-publish-module.adoc[]

== Checking API compatibility

Mill provides the ability to check API changes with the https://revapi.org/revapi-site/main/index.html[Revapi] analysis and change tracking tool.

include::partial$example/javalib/publishing/3-revapi.adoc[]

CAUTION: The `revapi` task does not fail if incompatibilities are reported. You should fix these, and verify by re-running `revapi`, before a release.

include::partial$Publishing_Footer.adoc[]
31 changes: 31 additions & 0 deletions example/javalib/publishing/3-revapi/bar/conf/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--

Copyright 2014-2017 Lukas Krejci
and other contributors as indicated by the @author tags.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>

<!-- change to "trace" if AST debugging is needed -->
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
8 changes: 8 additions & 0 deletions example/javalib/publishing/3-revapi/bar/conf/revapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"extension": "revapi.reporter.text",
"configuration": {
"minSeverity": "BREAKING"
}
}
]
27 changes: 27 additions & 0 deletions example/javalib/publishing/3-revapi/bar/src/Visibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2014-2017 Lukas Krejci
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class Visibility {
public int f;

private class SuperClass {
public int f;
}

public class SubClass extends SuperClass {
private int f2;
}
}
53 changes: 53 additions & 0 deletions example/javalib/publishing/3-revapi/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// SNIPPET:BUILD
package build
import mill._, javalib._, publish._, revapi._

object bar extends JavaModule with RevapiModule {
def publishVersion = "0.0.1"

def pomSettings = PomSettings(
description = "Hello",
organization = "com.lihaoyi",
url = "https://github.com/lihaoyi/example",
licenses = Seq(License.MIT),
versionControl = VersionControl.github("lihaoyi", "example"),
developers = Seq(Developer("lihaoyi", "Li Haoyi", "https://github.com/lihaoyi"))
)

override def revapiConfigFiles: T[Seq[PathRef]] =
// add Revapi config JSON file(s)
Task.Sources(millSourcePath / "conf/revapi.json")

override def revapiClasspath: T[Agg[PathRef]] = T {
// add folder containing logback.xml
super.revapiClasspath() ++ Seq(PathRef(millSourcePath / "conf"))
}
}

// This example uses the `revapi` task, provided by the `RevapiModule`, to run an
// analysis on old and new archives of a module to identify incompatibilities.
//
// NOTE: For demonstration purposes, an archive, to compare against, is published locally.
// In real usage, the old version would be downloaded from the publish repository.

/** Usage

> mill bar.publishLocal
Publishing Artifact(com.lihaoyi,bar,0.0.1) to ivy repo...

> cp dev/src/Visibility.java bar/src/Visibility.java

> mill bar.revapi
Starting analysis
Analysis results
----------------
old: field Visibility.SuperClass.f @ Visibility.SubClass
new: <none>
java.field.removed: Field removed from class.
... BREAKING
old: field Visibility.f
new: field Visibility.f
java.field.visibilityReduced: Visibility was reduced from 'public' to 'protected'.
... BREAKING
ajaychandran marked this conversation as resolved.
Show resolved Hide resolved
Analysis took ...ms.
*/
27 changes: 27 additions & 0 deletions example/javalib/publishing/3-revapi/dev/src/Visibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2014-2017 Lukas Krejci
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class Visibility {
protected int f;

private class SuperClass {
private int f;
}

public class SubClass extends SuperClass {
public int f2;
}
}
121 changes: 121 additions & 0 deletions scalalib/src/mill/javalib/revapi/RevapiModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package mill.javalib.revapi

import mill._
import mill.javalib._
import mill.javalib.revapi.RevapiModule.optional
import mill.scalalib.publish.Artifact
import mill.util.Jvm

/**
* Adds support for [[https://revapi.org/revapi-site/main/index.html Revapi checker]] to enable API analysis and change tracking.
*/
@mill.api.experimental // until Revapi has a stable release
trait RevapiModule extends PublishModule {

/**
* Runs [[revapiCliVersion Revapi CLI]] on this module's archives.
*/
def revapi(args: String*): Command[Unit] = Task.Command {
val workingDir = millSourcePath
ajaychandran marked this conversation as resolved.
Show resolved Hide resolved

val oldFiles = revapiOldFiles()
val oldFile = oldFiles.head
val oldSupFiles = oldFiles.tail
Comment on lines +24 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and below) can be written more concisely as val oldFile +: oldSupFiles = revapiOldFiles()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for Agg since it does not extend Seq.


val newFiles = revapiNewFiles()
val newFile = newFiles.head
val newSupFiles = newFiles.tail

val mainClass = "org.revapi.standalone.Main"
val mainArgs =
Seq.newBuilder[String]
// https://github.com/revapi/revapi/blob/69445626881347fbf7811a4a78ff230fe152a2dc/revapi-standalone/src/main/java/org/revapi/standalone/Main.java#L149
.++=(Seq(mainClass, workingDir.toString()))
// https://github.com/revapi/revapi/blob/69445626881347fbf7811a4a78ff230fe152a2dc/revapi-standalone/src/main/java/org/revapi/standalone/Main.java#L97
.++=(Seq("-e", revapiExtensions().mkString(",")))
.++=(Seq("-o", oldFile.path.toString()))
.++=(optional("-s", oldSupFiles.iterator.map(_.path)))
.++=(Seq("-n", newFile.path.toString()))
.++=(optional("-t", newSupFiles.iterator.map(_.path)))
.++=(optional("-c", revapiConfigFiles().iterator.map(_.path)))
.++=(Seq("-d", revapiCacheDir().path.toString()))
.++=(optional("-r", revapiRemoteRepositories()))
.++=(args)
.result()

T.log.info("running revapi cli")
Jvm.runSubprocess(
mainClass = mainClass,
classPath = revapiClasspath().map(_.path),
jvmArgs = revapiJvmArgs(),
mainArgs = mainArgs,
workingDir = workingDir
)
}

/**
* List of Maven GAVs of Revapi extensions
*
* @note Must be non-empty.
*/
def revapiExtensions: T[Seq[String]] = Seq(
"org.revapi:revapi-java:0.28.1",
"org.revapi:revapi-reporter-text:0.15.0"
)

/** API archive and supplement files (dependencies) to compare against */
def revapiOldFiles: T[Agg[PathRef]] = T {
val Artifact(group, id, version) = publishSelfDependency()
defaultResolver().resolveDeps(
Seq(ivy"$group:$id:$version"),
artifactTypes = Some(revapiArtifactTypes())
)
}

/** API archive and supplement files (dependencies) to compare */
def revapiNewFiles: T[Agg[PathRef]] = T {
Agg(jar()) ++
T.traverse(recursiveModuleDeps)(_.jar)() ++
defaultResolver().resolveDeps(
transitiveIvyDeps(),
artifactTypes = Some(revapiArtifactTypes())
)
}

/** List of configuration files */
def revapiConfigFiles: T[Seq[PathRef]] = Seq.empty[PathRef]

/** Location of local cache of extensions to use to locate artifacts */
def revapiCacheDir: T[PathRef] = T { PathRef(T.dest) }

/** URLs of remote Maven repositories to use for artifact resolution */
def revapiRemoteRepositories: T[Seq[String]] = T {
repositoriesTask()
.collect { case repo: coursier.MavenRepository => repo.root }
}

/** Classpath containing the Revapi [[revapiCliVersion CLI]] */
def revapiClasspath: T[Agg[PathRef]] = T {
defaultResolver().resolveDeps(
Agg(ivy"org.revapi:revapi-standalone:${revapiCliVersion()}")
)
}

/** [[https://revapi.org/revapi-standalone/0.12.0/index.html Revapi CLI]] version */
def revapiCliVersion: T[String] = "0.12.0"

/** JVM arguments for the Revapi [[revapiCliVersion CLI]] */
def revapiJvmArgs: T[Seq[String]] = Seq.empty[String]

/** Artifact types to resolve archives and supplement files (dependencies) */
def revapiArtifactTypes: T[Set[coursier.Type]] = Set(coursier.Type.jar)
}
@mill.api.experimental
object RevapiModule {

private def optional[T](name: String, values: IterableOnce[T]): Seq[String] = {
val it = values.iterator
if (it.isEmpty) Seq.empty
else Seq(name, it.mkString(","))
}
}
31 changes: 31 additions & 0 deletions scalalib/test/resources/javalib/revapi/conf/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--

Copyright 2014-2017 Lukas Krejci
and other contributors as indicated by the @author tags.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>

<!-- change to "trace" if AST debugging is needed -->
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
8 changes: 8 additions & 0 deletions scalalib/test/resources/javalib/revapi/conf/revapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"extension": "revapi.reporter.text",
"configuration": {
"minSeverity": "BREAKING"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2014-2017 Lukas Krejci
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
public @interface InheritedAnnotation {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2014-2017 Lukas Krejci
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class TypeParams {

public static class Base<T> {}

public static class Class<A, B extends String, T> extends Base<T> {}
}
Loading
Loading