1- // noinspection GroovyAssignabilityCheck
2- plugins {
3- id ' maven-publish'
4- }
1+ // file:noinspection GroovyAssignabilityCheck
52
63apply from : " ${ parent.projectDir} /gradle/include/jvm-project.gradle"
74
8- // noinspection GroovyAssignabilityCheck
95configurations {
10- mavenEmbedder // it is used to run maven tasks from gradle
6+ mavenEmbedder // maven embeddable component, with CLI and logging support
117}
128
139dependencies {
14- // `compile` because `api` dependencies are not included in pom.xml by `install` task
1510 implementation project(' :utbot-framework' )
1611
1712 implementation " org.apache.maven:maven-core:$maven_plugin_api_version "
@@ -35,73 +30,36 @@ dependencies {
3530
3631/**
3732 * We should run the maven task `install` to build & publish this plugin.
38- * But `utbot-maven` is the Gradle module (not Maven), so we have to
39- * manually generate the pom.xml file and the plugin descriptor file.
40- */
41-
42- def buildDirectory = buildDir. canonicalPath
43- def outputDirectory = compileKotlin. destinationDir. canonicalPath
44- def pomFile = new File (" $buildDir /pom.xml" )
45- def pluginDescriptorFile = new File (outputDirectory, ' META-INF/maven/plugin.xml' )
46-
47- /**
48- * Generates the pom.xml file and saves it to the [pomFile].
33+ * But `utbot-maven` is the Gradle module (not Maven), so there is no `install` task
34+ * and we have to manually generate the pom.xml file and the plugin descriptor file.
35+ *
36+ * The pom.xml file is in the src/main/resources.
37+ * It should contain all the information needed at runtime.
38+ *
39+ * The plugin descriptor file is generated automatically by [generatePluginDescriptor].
4940 */
50- task generatePomFile (dependsOn : compileKotlin) {
51- outputs. file pomFile
5241
53- doLast {
54- install. repositories. mavenInstaller. pom. with {
55- groupId = project. group
56- artifactId = project. name
57- version = project. version
58- packaging = ' maven-plugin'
59-
60- withXml {
61- asNode(). with {
62- appendNode(' build' ). with {
63- appendNode(' directory' , buildDirectory)
64- appendNode(' outputDirectory' , outputDirectory)
65- }
66- def repositoriesNode = appendNode(' repositories' )
67- // `this.project` is the project from Gradle, but `project` is the project from Maven
68- this . project. repositories. indexed(). forEach { index , repository ->
69- repositoriesNode. with {
70- appendNode(' repository' ). with {
71- // `index` is needed for the uniqueness of the IDs
72- appendNode(' id' , " ${ repository.name} _${ index} " )
73- appendNode(' url' , repository. url)
74- }
75- }
76- }
77- }
78- }
79- }
80- install. repositories. mavenInstaller. pom. writeTo(pomFile)
81-
82- assert pomFile. file, " ${ pomFile.canonicalPath} : was not generated"
83- logger. info(" POM is generated in ${ pomFile.canonicalPath} " )
84- }
85- }
42+ def pomFile = file(" ./src/main/resources/pom.xml" )
43+ def outputDirectory = project. buildDir. toPath(). resolve(" classes/kotlin/main" )
44+ def pluginDescriptorFile = new File (" $outputDirectory /META-INF/maven/plugin.xml" )
8645
8746/**
8847 * Generates the plugin descriptor file and saves it to the [pluginDescriptorFile].
8948 */
90- task generatePluginDescriptor (type : JavaExec , dependsOn : generatePomFile ) {
49+ task generatePluginDescriptor (type : JavaExec , dependsOn : compileKotlin ) {
9150 inputs. files project. compileKotlin. outputs. files
9251 outputs. file pluginDescriptorFile
9352
9453 workingDir projectDir
95- mainClass. set(' org.apache.maven.cli.MavenCli' )
9654 classpath = configurations. mavenEmbedder
97- // noinspection GroovyAssignabilityCheck
55+ mainClass . set( ' org.apache.maven.cli.MavenCli ' )
9856 systemProperties[' maven.multiModuleProjectDirectory' ] = projectDir
9957 args = [
100- ' --errors' ,
101- ' --batch-mode' ,
102- ' --file' , " ${ pomFile.path} " ,
103- ' org.apache.maven.plugins:maven-plugin-plugin:3.6.0:descriptor' ,
104- ' -Dproject.build.sourceEncoding=UTF-8'
58+ ' --errors' ,
59+ ' --batch-mode' ,
60+ ' --file' , " ${ pomFile.path} " ,
61+ ' org.apache.maven.plugins:maven-plugin-plugin:3.6.0:descriptor' ,
62+ ' -Dproject.build.sourceEncoding=UTF-8'
10563 ]
10664
10765 doLast {
@@ -110,6 +68,44 @@ task generatePluginDescriptor(type: JavaExec, dependsOn: generatePomFile) {
11068 }
11169}
11270
113- project. publishToMavenLocal. dependsOn(generatePluginDescriptor)
71+ publishing {
72+ publications {
73+ pluginMaven(MavenPublication ) {
74+ from components. java
75+ }
76+ }
77+ }
78+
79+ /**
80+ * `publishToMavenLocal` task generates pom.xml file, but that's not what we need.
81+ * Therefore, we have to override the generated file with our own, stored in resources.
82+ */
83+ generatePomFileForPluginMavenPublication. doLast {
84+ def ourOwnPomXml = new XmlParser (). parse(pomFile)
85+ def generatedPomFile = new File (" ./build/publications/pluginMaven/pom-default.xml" )
86+ def printer = new XmlNodePrinter (new PrintWriter (new FileWriter (generatedPomFile)))
87+ printer. with {
88+ // pretty print
89+ preserveWhitespace = true
90+ expandEmptyElements = true
91+ }
92+ printer. print (ourOwnPomXml)
93+ }
11494
115- // Please, use `utbot-maven/other/install` task for publishing
95+ // the plugin jar file should contain the plugin descriptor file
96+ jar. dependsOn generatePluginDescriptor
97+
98+ generatePluginDescriptor. dependsOn([
99+ project(' :utbot-api' ),
100+ project(' :utbot-core' ),
101+ project(' :utbot-instrumentation' ),
102+ project(' :utbot-framework' ),
103+ project(' :utbot-framework-api' ),
104+ project(' :utbot-fuzzers' ),
105+ project(' :utbot-rd' ),
106+ project(' :utbot-summary' )
107+ ]* . tasks. publishToMavenLocal)
108+
109+ inspectClassesForKotlinIC. enabled = false
110+ publishJarPublicationToMavenLocal. enabled = false
111+ publishPluginMavenPublicationToMavenLocal. enabled = true
0 commit comments