Skip to content

Commit 032b4d3

Browse files
authored
Add generated code to sources jar (#50)
1 parent 0e04ecf commit 032b4d3

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

buildSrc/src/main/kotlin/RemotePublishing.kt

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16-
import com.toasttab.protokt.shared.kotlin
17-
import com.toasttab.protokt.shared.main
1816
import io.codearte.gradle.nexus.NexusStagingExtension
1917
import org.gradle.api.Project
20-
import org.gradle.api.plugins.JavaPluginConvention
18+
import org.gradle.api.plugins.JavaPluginExtension
2119
import org.gradle.api.publish.PublishingExtension
2220
import org.gradle.api.publish.maven.MavenPublication
2321
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
@@ -108,11 +106,8 @@ fun Project.enablePublishing(defaultJars: Boolean = true) {
108106
}
109107

110108
if (defaultJars) {
111-
tasks.register<Jar>("sourcesJar") {
112-
dependsOn("classes")
113-
114-
from(this@enablePublishing.the<JavaPluginConvention>().sourceSets.main.kotlin)
115-
archiveClassifier.set("sources")
109+
configure<JavaPluginExtension> {
110+
withSourcesJar()
116111
}
117112

118113
tasks.register<Jar>("javadocJar") {
@@ -124,10 +119,9 @@ fun Project.enablePublishing(defaultJars: Boolean = true) {
124119
publications {
125120
create<MavenPublication>("sources") {
126121
from(components.getByName("java"))
127-
artifact(tasks.getByName("sourcesJar"))
128122
artifact(tasks.getByName("javadocJar"))
129123
artifactId = project.name
130-
version = "$version"
124+
version = "${project.version}"
131125
groupId = "$group"
132126
}
133127
}

buildSrc/src/main/kotlin/com/toasttab/protokt/shared/ProtobufBuild.kt

+20-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ import com.google.protobuf.gradle.plugins
2222
import com.google.protobuf.gradle.protobuf
2323
import com.google.protobuf.gradle.protoc
2424
import com.google.protobuf.gradle.remove
25+
import java.io.File
2526
import org.gradle.api.Project
2627
import org.gradle.api.plugins.JavaPluginConvention
28+
import org.gradle.api.tasks.bundling.Jar
2729
import org.gradle.internal.os.OperatingSystem
2830
import org.gradle.kotlin.dsl.apply
31+
import org.gradle.kotlin.dsl.named
2932
import org.gradle.kotlin.dsl.the
3033

3134
const val KOTLIN_EXTRA_CLASSPATH = "kotlin_extra_classpath"
@@ -37,8 +40,7 @@ internal fun configureProtobufPlugin(project: Project, ext: ProtoktExtension, bi
3740
project.protobuf {
3841
generatedFilesBaseDir = "${project.buildDir}/generated-sources"
3942

40-
project.the<JavaPluginConvention>().sourceSets.main.kotlin
41-
.srcDirs.add(project.file("$generatedFilesBaseDir/main/protokt"))
43+
configureSources(project, generatedFilesBaseDir)
4244

4345
protoc {
4446
artifact = "com.google.protobuf:protoc:${ext.protocVersion}"
@@ -75,6 +77,22 @@ internal fun configureProtobufPlugin(project: Project, ext: ProtoktExtension, bi
7577
}
7678
}
7779

80+
private fun configureSources(project: Project, generatedSourcesPath: String) {
81+
val protoktDir = "$generatedSourcesPath/main/protokt"
82+
83+
project.the<JavaPluginConvention>()
84+
.sourceSets.main.kotlin.srcDirs
85+
.add(File(protoktDir))
86+
87+
project.afterEvaluate {
88+
if (project.tasks.findByName("sourcesJar") != null) {
89+
tasks.named<Jar>("sourcesJar").configure {
90+
from(protoktDir)
91+
}
92+
}
93+
}
94+
}
95+
7896
private fun normalizePath(binaryPath: String) =
7997
if (OperatingSystem.current().isWindows) {
8098
// on windows, protoc expects a full, /-separated path to the binary

gradle/wrapper/gradle-wrapper.jar

216 Bytes
Binary file not shown.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

gradlew

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ esac
8282

8383
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
8484

85+
8586
# Determine the Java command to use to start the JVM.
8687
if [ -n "$JAVA_HOME" ] ; then
8788
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
@@ -129,6 +130,7 @@ fi
129130
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
130131
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
131132
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133+
132134
JAVACMD=`cygpath --unix "$JAVACMD"`
133135

134136
# We build the pattern for arguments to be converted via cygpath

gradlew.bat

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set CMD_LINE_ARGS=%*
8484

8585
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
8686

87+
8788
@rem Execute Gradle
8889
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
8990

0 commit comments

Comments
 (0)