Skip to content

schlan/java-censor

Repository files navigation


Build Status Java Censor Version License

Java Censor is a Gradle plugin that enables developers to publish sources of closed source projects to a Maven repository. It does this by removing implemented code only leaving the signatures of public interfaces, classes, methods and fields behind.

Purpose

Java Censor is built for closed source java libraries, in particular, closed source Android libraries. When working on closed source libraries, it is usually not possible to ship a source artefact when deploying a release. That brings one big downside* for the integrators of your library. Even if you ship a Javadoc artefact, IDEs like Android Studio or IntelliJ don't use that artefact for showing documentation. Instead, they rely solely on the source artefact for showing inline documentation.

That is where Java Censor can help. Java Censor makes it possible to release a source artefact of a closed source library without exposing secret code but still allows IDEs to show inline documentation. It does that when running before the release, by removing any implemented code, only keeping public signatures (interfaces, classes, methods, constructors, fields) and Javadoc.

Usage

For using Java Censor in a Gradle project, it needs to be added to the project first. That is done by adding it as a classpath dependency to the buildscript block and applying the plugin to a module. Applying the plugin to a module introduces a new task type called CensorCopyTask. The CensorCopyTask behaves like the built-in Copy task and has two mandatory configuration options.

from = [set of input directories/files]
Takes a set of files as input.
into = [output directory]
The output directory. (File)

Example:

// Add classpath dependency
buildscript {
  dependencies {
    classpath group: 'com.sebchlan.javacensor', name: 'java-censor', version: '1.2.1'
  }
}

// Apply the plugin
apply plugin: 'com.sebchlan.javacensor'

// Add a new task
tasks.register("censorSource", com.sebchlan.javacensor.CensorCopyTask) { task ->
    inputs.files(sourceSets.main.java.srcDirs)
    destinationDir = file("$buildDir/censored_source")
}

Android

Java Censor is also applicable to Android projects. The installation steps are very similar to the ones above. This is how a configuration can look like:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath group: 'com.sebchlan.javacensor', name: 'java-censor', version: '1.2.1'
    }
}

apply plugin: 'com.sebchlan.javacensor'

afterEvaluate { project ->

    uploadArchives {
        repositories {
            mavenDeployer {
                // ....
            }
        }

        task censorSource(type: com.sebchlan.javacensor.CensorCopyTask) {
            inputs.files(android.sourceSets.main.java.srcDirs)
            destinationDir = file("$buildDir/generated-src")
        }

        task androidSourcesJar(type: Jar) {
            classifier = 'sources'
            from "$buildDir/generated-src/"
            dependsOn censorSource
        }

        artifacts {
            archives androidSourcesJar
        }
        
    }
}

Contribution

Feel free to submit a PR or file an issue. Please make sure, when filing a bug, to provide sufficient reproduction steps or even a failing test.