Skip to content

Commit 34a51c5

Browse files
authored
Enable utbot-maven module #867 (#869)
1 parent 861b9aa commit 34a51c5

File tree

7 files changed

+97
-69
lines changed

7 files changed

+97
-69
lines changed

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ include 'utbot-instrumentation-tests'
2626

2727
include 'utbot-summary'
2828
include 'utbot-gradle'
29-
//include 'utbot-maven'
29+
include 'utbot-maven'
3030
include 'utbot-summary-tests'
3131
include 'utbot-framework-test'
3232
include 'utbot-rd'

utbot-gradle/docs/utbot-gradle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,4 @@ Please note that the maximum archive size for publishing on the Gradle Plugin Po
234234

235235
### Requirements
236236

237-
UTBot gradle plugin requires Gradle 6.8+
237+
UTBot gradle plugin requires Gradle 7.4.2+

utbot-gradle/src/main/kotlin/org/utbot/gradle/plugin/GenerateTestsAndSarifReportTask.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ open class GenerateTestsAndSarifReportTask @Inject constructor(
6464
private val logger: KLogger = org.utbot.gradle.plugin.logger
6565

6666
private val dependencyPaths by lazy {
67-
val thisClassLoader = this::class.java.classLoader as URLClassLoader
67+
val thisClassLoader = this::class.java.classLoader as? URLClassLoader
68+
?: return@lazy System.getProperty("java.class.path")
6869
thisClassLoader.urLs.joinToString(File.pathSeparator) { it.path }
6970
}
7071

utbot-maven/build.gradle

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
//noinspection GroovyAssignabilityCheck
2-
plugins {
3-
id 'maven-publish'
4-
}
1+
//file:noinspection GroovyAssignabilityCheck
52

63
apply from: "${parent.projectDir}/gradle/include/jvm-project.gradle"
74

8-
//noinspection GroovyAssignabilityCheck
95
configurations {
10-
mavenEmbedder // it is used to run maven tasks from gradle
6+
mavenEmbedder // maven embeddable component, with CLI and logging support
117
}
128

139
dependencies {
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

utbot-maven/docs/utbot-maven.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ For example, the following configuration may be used:
135135

136136
If you want to change the source code of the plugin or even the whole utbot-project,
137137
you need to do the following:
138-
- Publish UTBot to the local maven repository using `utbot/publishToMavenLocal` gradle task.
139-
- Publish `utbot-maven` to the local maven repository using `utbot-maven/other/install` gradle task.
138+
- Publish plugin to the local maven repository:
139+
`utbot-maven/publishing/publishToMavenLocal`
140140
- Add the plugin to your project (see the section __How to use__).
141141

142142
### How to configure the log level

utbot-maven/src/main/kotlin/org/utbot/maven/plugin/GenerateTestsAndSarifReportMojo.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ class GenerateTestsAndSarifReportMojo : AbstractMojo() {
139139
lateinit var rootMavenProjectWrapper: MavenProjectWrapper
140140

141141
private val dependencyPaths by lazy {
142-
val thisClassLoader = this::class.java.classLoader as URLClassLoader
142+
val thisClassLoader = this::class.java.classLoader as? URLClassLoader
143+
?: return@lazy System.getProperty("java.class.path")
143144
thisClassLoader.urLs.joinToString(File.pathSeparator) { it.path }
144145
}
145146

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.utbot</groupId>
5+
<artifactId>utbot-maven</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>maven-plugin</packaging>
8+
9+
<build>
10+
<!-- paths relative to src/main/resources -->
11+
<directory>../../../build</directory>
12+
<outputDirectory>../../../build/classes/kotlin/main</outputDirectory>
13+
</build>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.utbot</groupId>
18+
<artifactId>utbot-framework</artifactId>
19+
<version>1.0-SNAPSHOT</version>
20+
<scope>compile</scope>
21+
</dependency>
22+
</dependencies>
23+
24+
<repositories>
25+
<repository>
26+
<id>jitpack.io</id>
27+
<url>https://jitpack.io</url>
28+
</repository>
29+
</repositories>
30+
</project>

0 commit comments

Comments
 (0)