Skip to content

Commit 54eae40

Browse files
committed
Gradle: Added buildSrc (1/3)
Added library conventions and language based configurations. - Added `doclets` to `buildSrc` for the `javadocs` - Added `conventions` to centralize configuration of gradle plugins - Added `projects` to handle project level conventions JAVA-5758 JAVA-5757
1 parent 438e640 commit 54eae40

33 files changed

+1703
-0
lines changed

buildSrc/build.gradle.kts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import dev.panuszewski.gradle.pluginMarker
17+
18+
plugins {
19+
id("java-library")
20+
`kotlin-dsl`
21+
alias(libs.plugins.spotless)
22+
alias(libs.plugins.detekt) apply false
23+
}
24+
25+
repositories {
26+
gradlePluginPortal()
27+
mavenCentral()
28+
google()
29+
}
30+
31+
// Dependencies needed for the configuration of the plugins
32+
// Uses `pluginMarker` from the `typesafe-conventions` plugin, see `settings.gradle.kts`
33+
dependencies {
34+
implementation(pluginMarker(libs.plugins.bnd))
35+
implementation(pluginMarker(libs.plugins.detekt))
36+
implementation(pluginMarker(libs.plugins.dokka))
37+
implementation(pluginMarker(libs.plugins.kotlin.gradle))
38+
implementation(pluginMarker(libs.plugins.spotbugs))
39+
implementation(pluginMarker(libs.plugins.spotless))
40+
implementation(pluginMarker(libs.plugins.test.logger))
41+
}
42+
43+
// Spotless configuration for `buildSrc` code.
44+
spotless {
45+
kotlinGradle {
46+
target("**/*.gradle.kts")
47+
ktfmt("0.39").dropboxStyle().configure { it.setMaxWidth(120) }
48+
trimTrailingWhitespace()
49+
indentWithSpaces()
50+
endWithNewline()
51+
licenseHeaderFile(
52+
"../config/mongodb.license", "(package|group|plugins|import|buildscript|rootProject|@Suppress)")
53+
}
54+
55+
kotlin {
56+
target("**/*.kt")
57+
ktfmt().dropboxStyle().configure { it.setMaxWidth(120) }
58+
trimTrailingWhitespace()
59+
indentWithSpaces()
60+
endWithNewline()
61+
licenseHeaderFile(rootProject.file("../config/mongodb.license"))
62+
}
63+
64+
java {
65+
palantirJavaFormat()
66+
target("src/*/java/**/*.java")
67+
removeUnusedImports()
68+
trimTrailingWhitespace()
69+
indentWithSpaces()
70+
endWithNewline()
71+
licenseHeaderFile(rootProject.file("../config/mongodb.license"))
72+
}
73+
}
74+
75+
java { toolchain { languageVersion.set(JavaLanguageVersion.of("17")) } }
76+
77+
tasks.findByName("check")?.dependsOn("spotlessCheck")

buildSrc/settings.gradle.kts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
plugins {
17+
// Add support for `libs.versions.toml` within `buildSrc`
18+
// https://github.com/radoslaw-panuszewski/typesafe-conventions-gradle-plugin
19+
// https://github.com/gradle/gradle/issues/15383
20+
id("dev.panuszewski.typesafe-conventions") version "0.4.1"
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.doclet;
17+
18+
public final class AtlasManualTaglet extends DocTaglet {
19+
@Override
20+
public String getName() {
21+
return "mongodb.atlas.manual";
22+
}
23+
24+
@Override
25+
protected String getHeader() {
26+
return "MongoDB Atlas documentation";
27+
}
28+
29+
@Override
30+
protected String getBaseDocURI() {
31+
return "https://www.mongodb.com/docs/atlas/";
32+
}
33+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.doclet;
17+
18+
import static java.util.Arrays.asList;
19+
import static jdk.javadoc.doclet.Taglet.Location.CONSTRUCTOR;
20+
import static jdk.javadoc.doclet.Taglet.Location.FIELD;
21+
import static jdk.javadoc.doclet.Taglet.Location.METHOD;
22+
import static jdk.javadoc.doclet.Taglet.Location.OVERVIEW;
23+
import static jdk.javadoc.doclet.Taglet.Location.PACKAGE;
24+
import static jdk.javadoc.doclet.Taglet.Location.TYPE;
25+
26+
import com.sun.source.doctree.DocTree;
27+
import com.sun.source.doctree.UnknownBlockTagTree;
28+
import java.util.HashSet;
29+
import java.util.List;
30+
import java.util.Set;
31+
import javax.lang.model.element.Element;
32+
import jdk.javadoc.doclet.Taglet;
33+
34+
public abstract class DocTaglet implements Taglet {
35+
36+
@Override
37+
public Set<Location> getAllowedLocations() {
38+
return new HashSet<>(asList(CONSTRUCTOR, METHOD, FIELD, OVERVIEW, PACKAGE, TYPE));
39+
}
40+
41+
@Override
42+
public boolean isInlineTag() {
43+
return false;
44+
}
45+
46+
@Override
47+
public String toString(List<? extends DocTree> tags, Element element) {
48+
if (tags.size() == 0) {
49+
return null;
50+
}
51+
52+
StringBuilder buf =
53+
new StringBuilder(String.format("<dl><dt><span class=\"strong\">%s</span></dt>", getHeader()));
54+
for (DocTree tag : tags) {
55+
String text = ((UnknownBlockTagTree) tag).getContent().get(0).toString();
56+
buf.append("<dd>").append(genLink(text)).append("</dd>");
57+
}
58+
return buf.toString();
59+
}
60+
61+
protected String genLink(final String text) {
62+
String relativePath = text;
63+
String display = text;
64+
65+
int firstSpace = text.indexOf(' ');
66+
if (firstSpace != -1) {
67+
relativePath = text.substring(0, firstSpace);
68+
display = text.substring(firstSpace).trim();
69+
}
70+
71+
return String.format("<a href='%s%s'>%s</a>", getBaseDocURI(), relativePath, display);
72+
}
73+
74+
protected abstract String getHeader();
75+
76+
protected abstract String getBaseDocURI();
77+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.doclet;
17+
18+
public class DochubTaglet extends DocTaglet {
19+
20+
@Override
21+
public String getName() {
22+
return "mongodb.driver.dochub";
23+
}
24+
25+
@Override
26+
protected String getHeader() {
27+
return "MongoDB documentation";
28+
}
29+
30+
@Override
31+
protected String getBaseDocURI() {
32+
return "https://dochub.mongodb.org/";
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.doclet;
17+
18+
public class ManualTaglet extends DocTaglet {
19+
20+
@Override
21+
public String getName() {
22+
return "mongodb.driver.manual";
23+
}
24+
25+
@Override
26+
protected String getHeader() {
27+
return "MongoDB documentation";
28+
}
29+
30+
@Override
31+
protected String getBaseDocURI() {
32+
return "https://www.mongodb.com/docs/manual/";
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.doclet;
17+
18+
public class ServerReleaseTaglet extends DocTaglet {
19+
20+
@Override
21+
public String getName() {
22+
return "mongodb.server.release";
23+
}
24+
25+
@Override
26+
protected String getHeader() {
27+
return "Since server release";
28+
}
29+
30+
@Override
31+
protected String getBaseDocURI() {
32+
return "https://www.mongodb.com/docs/manual/release-notes/";
33+
}
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import org.gradle.api.Project
17+
import org.gradle.api.java.archives.Manifest
18+
import org.gradle.api.publish.maven.MavenPublication
19+
import org.gradle.api.tasks.bundling.Jar
20+
import org.gradle.kotlin.dsl.named
21+
import org.gradle.kotlin.dsl.withType
22+
23+
object ProjectExtensions {
24+
25+
/**
26+
* Extension function to get and validate the current scala version
27+
*
28+
* See: gradle.properties for `supportedScalaVersions` and `defaultScalaVersion`
29+
*/
30+
fun Project.scalaVersion(): String {
31+
val supportedScalaVersions = (project.property("supportedScalaVersions") as String).split(",")
32+
val scalaVersion: String =
33+
(project.findProperty("scalaVersion") ?: project.property("defaultScalaVersion")) as String
34+
35+
if (!supportedScalaVersions.contains(scalaVersion)) {
36+
throw UnsupportedOperationException(
37+
"""Scala version: $scalaVersion is not a supported scala version.
38+
|Supported versions: $supportedScalaVersions
39+
"""
40+
.trimMargin())
41+
}
42+
43+
return scalaVersion
44+
}
45+
46+
/** Extension function to configure the maven publication */
47+
fun Project.configureMavenPublication(configure: MavenPublication.() -> Unit = {}) {
48+
val publishing = extensions.getByName("publishing") as org.gradle.api.publish.PublishingExtension
49+
publishing.publications.named<MavenPublication>("maven") { configure() }
50+
}
51+
52+
/** Extension function to configure the jars manifest */
53+
fun Project.configureJarManifest(configure: Manifest.() -> Unit = {}) {
54+
tasks.withType<Jar> { manifest { configure() } }
55+
}
56+
}

0 commit comments

Comments
 (0)