github.com/apache/beam/sdks/v2@v2.48.2/java/bom/build.gradle (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * License); you may not use this file except in compliance
     8   * with the License.  You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an AS IS BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  // beam-sdks-java-bom generates a BOM (Bill of Materials) in the form of
    20  // a pom.xml file, which enables users to easily import multiple modules
    21  // from Beam at the same version. See
    22  // https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/library-best-practices/JLBP-15.md
    23  // for more details on BOMs.
    24  
    25  plugins {
    26    id 'org.apache.beam.module'
    27    id 'java'
    28    id 'maven-publish'
    29    id 'net.linguica.maven-settings'
    30  }
    31  
    32  def isRelease(Project project) {
    33    return project.hasProperty('isRelease')
    34  }
    35  
    36  ext {
    37    mavenJavaDir = "$project.buildDir/publications/mavenJava"
    38    mavenJavaBomOutputFile = file(mavenJavaDir + "/pom-default.xml")
    39  }
    40  
    41  for (p in rootProject.subprojects) {
    42    if (!p.path.startsWith(project.path)) {
    43      evaluationDependsOn(p.path)
    44    }
    45  }
    46  
    47  def bomModuleNames = new ArrayList<>()
    48  for (p in rootProject.subprojects) {
    49    def subproject = p // project(':' + p.name)
    50    if (subproject.ext.properties.containsKey('includeInJavaBom') &&
    51        subproject.ext.properties.includeInJavaBom) {
    52      bomModuleNames.add(subproject.archivesBaseName)
    53    }
    54  }
    55  
    56  // Copy our pom.xml to the location where a generated POM would go
    57  task copyPom(type: Copy) {
    58    from 'pom.xml.template'
    59    into mavenJavaDir
    60    rename 'pom.xml.template', 'pom-default.xml'
    61    expand(version: project.version, modules: bomModuleNames)
    62  }
    63  
    64  assemble.dependsOn copyPom
    65  
    66  // We want to use our own pom.xml instead of the generated one, so we disable
    67  // the pom.xml generation and have the publish tasks depend on `copyPom` instead.
    68  tasks.whenTaskAdded { task ->
    69    if (task.name == 'generatePomFileForMavenJavaPublication') {
    70      // Ensures the pom file is signed later if we are performing a release (see BEAM-11068)
    71      task.doLast {
    72        copy {
    73          from 'pom.xml.template'
    74          into mavenJavaDir
    75          rename 'pom.xml.template', 'pom-default.xml'
    76          expand(version: project.version, modules: bomModuleNames)
    77        }
    78      }
    79    } else if (task.name.startsWith('publishMavenJavaPublication')) {
    80      task.dependsOn copyPom
    81    }
    82  }
    83  
    84  // Starting in Gradle 6.2 a sanity check is performed before uploading. The
    85  // check fails without generating the jar.
    86  jar.enabled = true
    87  
    88  // Starting in Gradle 6.0, the Gradle module metadata is generated automatically
    89  // Disable generating the metadata until this project uses java-platform to
    90  // publish the BOM (see BEAM-11709)
    91  tasks.withType(GenerateModuleMetadata) {
    92    enabled = false
    93  }
    94  
    95  // Remove the default jar archive which is added by the 'java' plugin.
    96  configurations.archives.artifacts.with { archives ->
    97    def artifacts = []
    98    archives.each {
    99      if (it.file =~ 'jar') {
   100        // We can't just call `archives.remove(it)` here because it triggers
   101        // a `ConcurrentModificationException`, so we add matching artifacts
   102        // to another list, then remove those elements outside of this iteration.
   103        artifacts.add(it)
   104      }
   105    }
   106    artifacts.each {
   107      archives.remove(it)
   108    }
   109  }
   110  
   111  artifacts {
   112    archives(mavenJavaBomOutputFile) {
   113      builtBy copyPom
   114    }
   115  }
   116  
   117  afterEvaluate {
   118    // We can't use the `publishing` section from applyJavaNature because
   119    // we don't want all the Java artifacts, and we want to use our own pom.xml
   120    // instead of the generated one.
   121    publishing {
   122      publications {
   123        mavenJava(MavenPublication) {
   124          groupId = project.mavenGroupId
   125          artifactId = archivesBaseName
   126          version = project.version
   127  
   128          // Ensures the published components are included in the maven metadata.
   129          // Gradle 6.6.1 changed the way metadata was generated and this ensures
   130          // the proper snapshot versions are present for the nightly build.
   131          from components.java
   132        }
   133      }
   134      repositories project.ext.repositories
   135    }
   136  
   137    // Only sign artifacts if we are performing a release
   138    if (isRelease(project) && !project.hasProperty('noSigning')) {
   139      apply plugin: "signing"
   140      signing {
   141        useGpgCmd()
   142        sign publishing.publications
   143      }
   144    }
   145  }