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 3 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
146 changes: 47 additions & 99 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,9 +58,41 @@ dependencies {
mavenDeployer "org.apache.maven.wagon:wagon-ftp:2.8"
}

afterEvaluate {
assert (!projectPomName.isEmpty() && !projectPomDescription.isEmpty())
pom {
uploadArchives { task ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do these parameters(username, pass, url) get passed through now? I see you trimmed down the the hard coded urls.

Copy link
Author

@lhotari lhotari Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't change the way the parameters are passed. The mavenRepositoryUrl, mavenUsername and mavenUserPassword parameters are assumed to be passed by gradle project properties (more), just like before. To explain the change that you have commented on:
Instead of adding the uploadArchives task in afterEvaluate, I moved the repository config to the doFirst block of uploadArchives task. I also changed the way that the existence of mavenRepositoryUrl, mavenUsername and mavenUserPassword parameters are checked. These parameters are only required when the uploadArchives task is part of the Gradle task graph and will be executed during the build. The same approach was used in Gradle core build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually don't use the uploadArchives task anymore. We now use an external mechanism.

We do still need the build to generate the poms in the "artifacts directory" (see getArtifactsDirectory). See my latest PR, #592, where I've addressed this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@littleaj Thanks for the clarification. I added a commit bed9829 to this PR that makes #592 obsolete. That commit adds a task "generatePom" that generates a pom beside the artifact jar file. This task is a now a dependency of copyLibsToGlobalArtifactsFolder task, so it should cover your way to publish.
I think it makes sense to keep "uploadArchives" and "install" tasks since those help users of the library to work with local forks and patches. Users would typically want to start using a change internally before the changes have been merged upstream.

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)
}
}
}
}

install {
doFirst {
updatePomWithGeneralProjectInformation(repositories.mavenInstaller.pom)
}
}

// 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
Expand Down Expand Up @@ -88,26 +120,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 @@ -145,79 +169,3 @@ task prepare {
}

// 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