/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.beam.gradle
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.gradle.api.attributes.Category
import org.gradle.api.GradleException
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree
import org.gradle.api.plugins.quality.Checkstyle
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.testing.jacoco.tasks.JacocoReport
import java.net.ServerSocket
/**
* This plugin adds methods to configure a module with Beam's defaults, called "natures".
*
*
The natures available:
*
*
* - Java - Configures plugins commonly found in Java projects
*
- Go - Configures plugins commonly found in Go projects
*
- Docker - Configures plugins commonly used to build Docker containers
*
- Grpc - Configures plugins commonly used to generate source from protos
*
- Avro - Configures plugins commonly used to generate source from Avro specifications
*
*
* For example, see applyJavaNature.
*/
class BeamModulePlugin implements Plugin {
/** Licence header enforced by spotless */
static final String javaLicenseHeader = """/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
"""
static def getRandomPort() {
new ServerSocket(0).withCloseable { socket ->
def port = socket.getLocalPort()
if (port > 0) {
return port
} else {
throw new GradleException("couldn't find a free port.")
}
}
}
/** A class defining the set of configurable properties accepted by applyJavaNature. */
static class JavaNatureConfiguration {
/** Controls whether the spotbugs plugin is enabled and configured. */
boolean enableSpotbugs = true
/** Regexes matching generated classes which should not receive extended type checking. */
List generatedClassPatterns = []
/** Classes triggering Checker failures. A map from class name to the bug filed against checkerframework. */
Map classesTriggerCheckerBugs = [:]
/** Controls whether the dependency analysis plugin is enabled. */
boolean enableStrictDependencies = true
/** Override the default "beam-" + `dash separated path` archivesBaseName. */
String archivesBaseName = null
/**
* List of additional lint warnings to disable.
* In addition, defaultLintSuppressions defined below
* will be applied to all projects.
*/
List disableLintWarnings = []
/** Controls whether tests are run with shadowJar. */
boolean testShadowJar = false
/**
* Controls whether the shadow jar is validated to not contain any classes outside the org.apache.beam namespace.
* This protects artifact jars from leaking dependencies classes causing conflicts for users.
*
* Note that this can be disabled for subprojects that produce application artifacts that are not intended to
* be depended on by users.
*/
boolean validateShadowJar = true
/**
* Controls whether 'jmh' specific configuration is enabled to build a JMH
* focused module.
*
* Add additional dependencies to the implementation configuration.
*
* Note that the JMH annotation processor is enabled by default and that
* a 'jmh' task is created which executes JMH.
*
* Publishing is not allowed for JMH enabled projects.
*/
boolean enableJmh = false
/**
* The set of excludes that should be used during validation of the shadow jar. Projects should override
* the default with the most specific set of excludes that is valid for the contents of its shaded jar.
*
* By default we exclude any class underneath the org.apache.beam namespace.
*/
List shadowJarValidationExcludes = ["org/apache/beam/**"]
/**
* If unset, no shading is performed. The jar and test jar archives are used during publishing.
* Otherwise the shadowJar and shadowTestJar artifacts are used during publishing.
*
* The shadowJar / shadowTestJar tasks execute the specified closure to configure themselves.
*/
Closure shadowClosure
/** Controls whether this project is published to Maven. */
boolean publish = true
/** Controls whether javadoc is exported for this project. */
boolean exportJavadoc = true
/**
* Automatic-Module-Name Header value to be set in MANFIEST.MF file.
* This is a required parameter unless publishing to Maven is disabled for this project.
*
* @see: https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/library-best-practices/JLBP-20.md
*/
String automaticModuleName = null
/**
* The set of additional maven repositories that should be added into published POM file.
*/
List