-
Notifications
You must be signed in to change notification settings - Fork 199
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
Changes from 3 commits
b33a09d
fbec2a9
64808ed
bed9829
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -58,9 +58,41 @@ dependencies { | |
mavenDeployer "org.apache.maven.wagon:wagon-ftp:2.8" | ||
} | ||
|
||
afterEvaluate { | ||
assert (!projectPomName.isEmpty() && !projectPomDescription.isEmpty()) | ||
pom { | ||
uploadArchives { task -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually don't use the We do still need the build to generate the poms in the "artifacts directory" (see There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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 | ||
|
@@ -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 | ||
} | ||
} | ||
|
||
|
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.