Skip to content

Commit

Permalink
Modernize Gradle build (#183)
Browse files Browse the repository at this point in the history
* Upgrade to Gradle 6.8.3

* Improve the Gradle build

- Make use of a build logic directory for build logic, so that it's not
mixed into the build scripts themselves.
- Upgrade to use the Maven Publish plugin.
- Publish sources+javadocs for the "core" component.
- Make use of lazy APIs.

* Get rid of buildscript block

Make use of the `plugins` block instead.
Commented out repositories have been removed as this is a code
smell: if there's some configuration it should be explicit.

* Enable parallel builds

* Make sure the build runs on Java 8

* Fix project descriptors

* Adjust daemon settings

So that we can run `./gradlew build` without running out of memory.

* Remove build tweaks

This commit gets rid of all the "old" build specifics to only use
standard mechanisms. In particular, the build directories are now
the standard ones.

Publishing location can be configured just by passing the
   -Prepo=....

parameter. For example:

./gradlew publish -Prepo=/tmp/my/repo

* Workaround for AGP bug

This commit adds a daemon option to workaround an AGP bug causing
flakiness when processing the R resource.

javax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

Ref: https://issuetracker.google.com/issues/175338398

This should be reverted once the bug is fixed.

* Reintroduce zip distribution

Co-authored-by: Nicolas Roard <nicolasroard@google.com>
  • Loading branch information
melix and camaelon committed Mar 16, 2021
1 parent 21fba51 commit b681ec2
Show file tree
Hide file tree
Showing 18 changed files with 473 additions and 444 deletions.
22 changes: 21 additions & 1 deletion constraintlayout/README.md
Expand Up @@ -17,14 +17,34 @@ export DOCLAVA_PREBUILTS=$STUDIO_MASTER/external/
export DOCLAVA_ROOTDIR=$STUDIO_MASTER/tools/sherpa/
```

To build the library, run:

```bash
./gradlew build
```

To compile and publish to your local offline M2 repository, you need to run:

```bash
./localBuild.sh
```

To generate the maven artifact, you then need to run:
Alternatively you can set the URI of the repository you want to publish to:

```bash
./gradlew -Drepo=/path/to/repo publish
```

or even an external repository:

```bash
./gradlew -Drepo=https://com.mycompany/repo publish
```

To create the distribution before uploading to the Google repository, run:

```bash
./gradlew dist
```

the resulting files will be found in `build/dist`.
4 changes: 4 additions & 0 deletions constraintlayout/build-logic/build.gradle
@@ -0,0 +1,4 @@
plugins {
id 'groovy-gradle-plugin'
}

@@ -0,0 +1,14 @@
import androidx.constraintlayout.GlobalConfig

extensions.create("globalConfig", GlobalConfig, rootProject.layout)

version = providers.gradleProperty("${project.name}.version").forUseAtConfigurationTime().get()

// Configure the project to use the Java 8 compiler
pluginManager.withPlugin("java-base") {
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
}
@@ -0,0 +1,15 @@
plugins {
id 'java-library'
}

java {
withSourcesJar()
withJavadocJar()
}

tasks.withType(Javadoc).configureEach {
options {
// There are *many* javadoc errors which need to be fixed
addStringOption('Xdoclint:none', '-quiet')
}
}
@@ -0,0 +1,82 @@
import androidx.constraintlayout.GenerateSourcePropsTask

plugins {
id 'androidx.build.base'
id 'maven-publish'
}

group = 'androidx.constraintlayout'
def aid = project.name != 'constraintlayout' ? "constraintlayout-${project.name}" : project.name
def distRepo = rootProject.layout.buildDirectory.dir("repo")

publishing {
repositories {
maven {
name = "maven"
url = globalConfig.repoLocation
}
maven {
name = "dist"
url = distRepo
}
}
}

components.configureEach {component ->
if (component.name in ['release', 'java']) {
publishing.publications {
maven(MavenPublication) {
from component
artifactId aid
}
}
}
}

publishing.publications.configureEach {
pom {
name = globalConfig.pomName
description = globalConfig.pomDescription
url = 'http://tools.android.com'
inceptionYear = '2007'

licenses {
license {
name = 'The Apache Software License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution = 'repo'
}
}

scm {
url = 'https://github.com/androidx/constraintlayout'
connection = 'git@github.com:androidx/constraintlayout.git'
}
developers {
developer {
name = 'The Android Open Source Project'
}
}
}
}

def generateSourceProps = tasks.register("generateSourceProps", GenerateSourcePropsTask) {
outputDirectory.set(layout.buildDirectory.dir("source-props"))
}

def releaseZip = tasks.register("releaseZip", Zip) {
dependsOn "publishMavenPublicationToDistRepository"

String path = "${project.group.replace('.', '/')}/${aid}/${project.version}"
destinationDirectory = rootProject.layout.buildDirectory.dir("dist")
archiveBaseName = "androidx-${aid}"
archiveVersion = globalConfig.buildNumber.orElse(project.version)
from generateSourceProps
into(path) {
from distRepo.map { new File("$it/$path") }
}
}

tasks.register("dist") {
dependsOn releaseZip
}
@@ -0,0 +1,122 @@
/*
* Copyright 2003-2012 the original author or authors.
*
* 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.
*/
package androidx.constraintlayout;

import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ProjectDependency;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.publish.PublicationContainer;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;

@CacheableTask
public abstract class GenerateSourcePropsTask extends DefaultTask {

@Input
abstract Property<String> getGroupId();

@Input
abstract Property<String> getArtifactId();

@Input
abstract Property<String> getVersion();

@Input
abstract Property<String> getPomDescription();

@Input
abstract ListProperty<String> getDependencies();

@OutputDirectory
abstract DirectoryProperty getOutputDirectory();

@Internal
abstract RegularFileProperty getOutputFile();

@Inject
public GenerateSourcePropsTask(ProviderFactory providers) {
Project project = getProject();
getGroupId().convention(providers.provider(() -> findMavenPublication(project).getGroupId()));
getArtifactId().convention(providers.provider(() -> findMavenPublication(project).getArtifactId()));
getVersion().convention(providers.provider(() -> findMavenPublication(project).getVersion()));
getPomDescription().convention(findMavenPublication(project).getPom().getDescription());
getDependencies().convention(providers.provider(this::collectDependencies));
getOutputFile().set(getOutputDirectory().file(providers.provider(() -> {
MavenPublication mavenPublication = findMavenPublication(project);
return mavenPublication.getGroupId().replace('.', '/') + "/" +
mavenPublication.getArtifactId() + "/" +
mavenPublication.getVersion() + "/" +
"source.properties";
})));
}

private List<String> collectDependencies() {
Configuration implementation = getProject().getConfigurations().getByName("implementation");
return implementation.getAllDependencies().stream()
.filter(ProjectDependency.class::isInstance)
.map(ProjectDependency.class::cast)
.map(p -> {
MavenPublication mavenPublication = findMavenPublication(p.getDependencyProject());
return mavenPublication.getGroupId() + ":" + mavenPublication.getArtifactId() + ":" + mavenPublication.getVersion();
})
.collect(Collectors.toList());
}

@TaskAction
public void generate() throws IOException {
String content = "Maven.GroupId=" + getGroupId().get() + "\n" +
"Maven.ArtifactId=" + getArtifactId().get() + "\n" +
"Maven.Version=" + getVersion().get() + "\n" +
"Pkg.Desc=" + getPomDescription().get() + " " + getVersion().get() + "\n" +
"Pkg.Revision=1\n" +
"Extra.VendorId=android\n" +
"Extra.VendorDisplay=Android\n" +
"Maven.Dependencies=" + String.join(",", collectDependencies());
File propertiesFile = getOutputFile().getAsFile().get();
File parentFile = propertiesFile.getParentFile();
if (parentFile.isDirectory() || parentFile.mkdirs()) {
Files.write(getOutputFile().getAsFile().get().toPath(), content.getBytes(StandardCharsets.UTF_8));
}
}

private MavenPublication findMavenPublication(Project p) {
PublicationContainer publications = p.getExtensions().getByType(PublishingExtension.class).getPublications();
return publications.stream()
.filter(MavenPublication.class::isInstance)
.map(MavenPublication.class::cast)
.findFirst()
.orElseThrow(() -> new RuntimeException("No Maven publication found. Did you apply the `androidx.build.publishing` plugin?"));
}
}
@@ -0,0 +1,51 @@
/*
* Copyright 2003-2012 the original author or authors.
*
* 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.
*/
package androidx.constraintlayout;

import org.gradle.api.file.ProjectLayout;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;

import javax.inject.Inject;

public abstract class GlobalConfig {
/**
* We use a String to represent the repository URI instead of a DirectoryProperty
* because we want to support both publishing to a local file repository and a
* remote repository
*/
abstract Property<String> getRepoLocation();

abstract Property<String> getBuildNumber();

abstract Property<String> getPomName();

abstract Property<String> getPomDescription();

@Inject
public GlobalConfig(ProviderFactory providers, ProjectLayout rootProjectLayout) {
getBuildNumber().convention(getEnv(providers, "BUILD"));
getRepoLocation().convention(
providers.gradleProperty("repo").forUseAtConfigurationTime()
.orElse(rootProjectLayout.getBuildDirectory().dir("repo").map(d -> d.getAsFile().getAbsolutePath()))
);
}

private static Provider<String> getEnv(ProviderFactory providers, String name) {
return providers.environmentVariable(name).forUseAtConfigurationTime();
}
}

0 comments on commit b681ec2

Please sign in to comment.