Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix customizing pom.xml in Gradle build #582

Merged
merged 4 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ projectPomDescription = "This is the core module of " + project.msftAppInsightsJ

whenPomConfigured = { p ->
def agentArtifactId = project(":agent").jar.baseName
p.dependencies = p.dependencies.findAll { dep -> dep.artifactId != agentArtifactId }
writePomToArtifactsDirectory(p, project.name)
p.dependencies = p.dependencies.findAll { dep ->
dep.artifactId != agentArtifactId &&
!(dep.groupId in ['org.apache.http', 'eu.infomas', 'org.apache.commons', 'commons-io',
'com.google.guava', 'com.google.code.gson', 'org.apache.httpcomponents'])
}
}

// endregion Publishing properties
161 changes: 61 additions & 100 deletions gradle/publishing.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply from: "$buildScriptsDir/provided-configuration.gradle"
ext {
projectPomName = ""
projectPomDescription = ""
whenPomConfigured = { p -> writePomToArtifactsDirectory(p, project.name) }
whenPomConfigured = { p -> }
}

def releaseNotesFileDir = "$project.buildDir/src/common/main/resources"
Expand Down Expand Up @@ -58,12 +58,57 @@ dependencies {
mavenDeployer "org.apache.maven.wagon:wagon-ftp:2.8"
}

afterEvaluate {
assert (!projectPomName.isEmpty() && !projectPomDescription.isEmpty())
pom {
// not used at Microsoft, could be used for publishing a fork
uploadArchives { task ->
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(task)) {
// check properties defined and fail early
mavenRepositoryUrl
mavenUsername
mavenUserPassword
}
}

doFirst {
repositories {
mavenDeployer {
configuration = configurations.mavenDeployer

repository(url: mavenRepositoryUrl) {
authentication(userName: mavenUsername, password: mavenUserPassword)
}

updatePomWithGeneralProjectInformation(pom)
}
}
}
}

// task for installing artifacts to maven local repository
install {
doFirst {
updatePomWithGeneralProjectInformation(repositories.mavenInstaller.pom)
}
}

// generates a pom file "beside" the jar file
task generatePom {
doLast {
def pomFileName = jar.archiveName.replaceFirst(/\.jar$/, '.pom')
def pomFileObj = new File(jar.destinationDir, pomFileName)
def pomObject = updatePomWithGeneralProjectInformation(pom())
pomObject.writeTo(pomFileObj)
}
}

// customizes pom, used for both uploadArchives.repositories.mavenDeployer.pom and install.repositories.mavenInstaller.pom
def updatePomWithGeneralProjectInformation(pomObject) {
configure(pomObject) {
assert (!projectPomName.isEmpty() && !projectPomDescription.isEmpty())
project {
name = projectPomName
description = projectPomDescription
artifactId = archivesBaseName
url = "https://github.com/Microsoft/ApplicationInsights-Java"

licenses {
Expand All @@ -88,26 +133,18 @@ afterEvaluate {

scopeMappings.addMapping(MavenPlugin.PROVIDED_COMPILE_PRIORITY, configurations.getByName("provided"), Conf2ScopeMappingContainer.PROVIDED)

whenConfigured whenPomConfigured
}.writeTo("pom.xml")

uploadArchives {
ext.requriedProperties = ["mavenRepositoryUrl", "mavenUsername", "mavenUserPassword"]
if (requiredPropertiesExist(requriedProperties)) {
repositories {
mavenDeployer {
configuration = configurations.mavenDeployer

repository(url: mavenRepositoryUrl) {
authentication(userName: mavenUsername, password: mavenUserPassword)
}

updatePomWithGeneralProjectInformation(pom)
}
}
} else {
logger.warn "Missing required properties for maven publish"
withXml {
// Append license text to pom.xml
def pomText = asString()
def licenseComment = new StringBuilder()
licenseComment.append("<!--\n")
licenseComment.append(rootProject.file("LICENSE.txt").text)
licenseComment.append("\n-->\n")
// insert in pom.xml after the first row (xml declaration)
pomText.insert(pomText.indexOf(String.valueOf((char)'\n')) + 1, licenseComment)
}

whenConfigured whenPomConfigured
}
}

Expand Down Expand Up @@ -137,87 +174,11 @@ task copyLibsToGlobalArtifactsFolder(type: Copy) {
from libsDir
into new File(artifactsDirectoryRoot, project.name)
exclude '**/windows/**'
dependsOn assemble, prepareSourcesArchive, prepareJavadocArchive
dependsOn assemble, prepareSourcesArchive, prepareJavadocArchive, generatePom
}

task prepare {
dependsOn copyLibsToGlobalArtifactsFolder
}

// endregion Publishing configuration

// region Public methods
ext {
//updatePomWithGeneralProjectInformation = this.&updatePomWithGeneralProjectInformation
writePomToArtifactsDirectory = this.&writePomToArtifactsDirectory
getArtifactsDirectory = this.&getArtifactsDirectory
}

def updatePomWithGeneralProjectInformation(pomObject) {
pomObject.project {

name = projectPomName
description = projectPomDescription
url = "https://github.com/Microsoft/ApplicationInsights-Java"

licenses {
license {
name = "MIT License"
url = "http://www.opensource.org/licenses/mit-license.php"
}
}

scm {
url = "scm:git:https://github.com/Microsoft/ApplicationInsights-Java"
connection = "scm:git:git://github.com/Microsoft/ApplicationInsights-Java.git"
}

developers {
developer {
id = "microsoft"
name = "Microsoft"
}
}
}

pomObject.scopeMappings.addMapping(MavenPlugin.PROVIDED_COMPILE_PRIORITY, configurations.getByName("provided"), Conf2ScopeMappingContainer.PROVIDED)

pomObject.whenConfigured whenPomConfigured
}

def writePomToArtifactsDirectory(pomObject, directoryName) {
def pomFilename = pomObject.getArtifactId() + '-' + pomObject.getVersion() + '.pom'
def artifactsDir = getArtifactsDirectory(directoryName)
def pomWriter = new FileWriter(new File(artifactsDir, pomFilename), false)

// Converting the POM object to string, to be able to read one by line.
StringWriter stringWriter = new StringWriter()
pomObject.writeTo(stringWriter)
StringReader reader = new StringReader(stringWriter.toString())

// Appending xml schema first
String schema = reader.readLine()
pomWriter.append(schema + "\n")

// Appending license text
def licenseText = GFileUtils.readFile(new File("LICENSE.TXT"))
pomWriter.append("<!--\n")
pomWriter.append(licenseText + "\n")
pomWriter.append("-->\n")

// Appending the rest of the pom file.
for (String line : reader.readLines()) {
pomWriter.append(line + "\n")
}

pomWriter.close()
}

def getArtifactsDirectory(artifactId) {
File dir = new File(artifactsDirectoryRoot, artifactId)
dir.mkdirs()

return dir
}

// endregion Public methods
Empty file modified gradlew
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion logging/logging.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ sourceSets {

whenPomConfigured = { p ->
p.dependencies = p.dependencies.findAll { dep -> dep.artifactId != "junit" && dep.artifactId != "mockito-all" }
writePomToArtifactsDirectory(p, project.name)
}

// endregion Publishing properties
Expand Down
7 changes: 5 additions & 2 deletions web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ projectPomDescription = "This is the web module of " + project.msftAppInsightsJa

whenPomConfigured = { p ->
def agentArtifactId = project(":agent").jar.baseName
p.dependencies = p.dependencies.findAll { dep -> dep.artifactId != agentArtifactId }
writePomToArtifactsDirectory(p, project.name)
p.dependencies = p.dependencies.findAll { dep ->
dep.artifactId != agentArtifactId &&
!(dep.groupId in ['org.apache.http', 'org.apache.commons', 'commons-io',
'org.apache.httpcomponents'])
}
}

// endregion Publishing properties