I'm using the maven-publish plugin in my build.gradle. For each of the compile dependencies, the group, name and version are used to generate the appropriate tags in the generated pom.xml file - and this works fine.
Here are the example dependencies I have:
dependencies {
compile (
[group: 'com.actionbarsherlock', name: 'actionbarsherlock', version: '4.4.0', ext: 'aar'],
[group: 'com.android.support', name: 'support-v4', version: '19.0.+']
)}
Here is the publishing element I have:
publishing {
publications {
// A publication named 'mavenAndroid'
mavenPublishLibrary(MavenPublication) {
// Artifact using the aar from the build output
artifact "${project.projectDir}/build/outputs/aar/${project.name}-${project.version}.aar"
// Generate dependencies manually for POM
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
//Iterate over the compile dependencies, adding a <dependency> node for each
configurations.compile.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
// This is what I'd want to do...
if (it.ext != '') {
dependencyNode.appendNode('type', it.ext)
}
// ...but this doesn't work!
}
}
}
}
}
I'd like to be able to add the ext value (e.g. aar) to the pom.xml along with the group, name and version, something like what I have in the above snippet.
I ask these questions when I've been struggling for hours, and then I always seem to answer myself soon after asking it!
I used the following to get the dependency extension, replacing the if() in the question:
it.getArtifacts().each {
dependencyNode.appendNode('type', it.getExtension())
}
Related
I made a decision to migrate from Dependency Management Plugin to Gradle built-in BOM import support. Since Gradle built-in BOM import support has better performance But
I run into the issue:
I cannot find alternatives for dependency and dependencySet in native Gradle:
dependencyManagement {
dependencies {
dependency("org.springframework:spring-core:4.0.3.RELEASE")
}
}
//or
dependencyManagement {
dependencies {
dependencySet(group:'org.slf4j', version: '1.7.7') {
entry 'slf4j-api'
entry 'slf4j-simple'
}
}
}
and then I could use dependency without version
dependencies {
compile 'org.springframework:spring-core'
}
How can I get the same behavior in naive Gradle? I mean: I'd like to define a version once as I did it when using Dependency Management Plugin
Solution below helps to avoid versions copy-paste. However it isn't the same with Dependency Management plugin.
For Gradle Kotlin Dsl:
You can create buildSrc with you own code, when you can place any constants.
Algorithm:
Create folder buildSrc/src/main/kotlin
Create file buildSrc/src/main/kotlin/Versions.kt with content:
object Versions {
const val junitVersion = "5.5.5" // just example
}
Create file buildSrc/build.gradle.kts with content:
plugins {
`kotlin-dsl`
}
Use the following syntax in your gradle.kts files:
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:${Versions.junitVersion}")
}
For Gradle Groovy:
Create file gradle.properties
Put versions there with syntax like okhttp_version=4.2.0
Use the following syntax in your gradle files:
dependencies {
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: okhttp_version
}
You can do so on the gradle.properties file. I.e.:
# APPLICATION PROPERTIES
name=projectName
group=com.domain
version=1.0.0
description=A brief description
gradleScripts=https://raw.githubusercontent.com/hexagonkt/hexagon/1.2.0/gradle
# DEPENDENCIES VERSIONS
kotlinVersion=1.3.61
kotlinCoroutinesVersion=1.3.2
Or in settings.gradle if you don't want to create another file:
rootProject.name = "hexagon-contact-application"
gradle.rootProject {
allprojects {
version = "1.0.0"
group = "org.hexagonkt"
description = "Contact application backend api"
}
extensions.gradleScripts = "https://raw.githubusercontent.com/hexagonkt/hexagon/1.0.18/gradle"
extensions.kotlinVersion = "1.3.50"
extensions.kotlinCoroutinesVersion = "1.3.2"
extensions.hexagonVersion = "1.0.21"
extensions.logbackVersion = "1.2.3"
extensions.bcryptVersion="0.8.0"
extensions.javaJwtVersion="3.8.2"
}
And if you want to avoid adding the version variable to all related dependencies, you can create a method in the build file:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
}
apply from: "$gradleScripts/kotlin.gradle"
apply from: "$gradleScripts/service.gradle"
apply from: "$gradleScripts/junit.gradle"
defaultTasks("test")
mainClassName = 'com.hexagonkt.contact.ApplicationKt'
applicationDefaultJvmArgs = ["-Xms64M", "-Xmx2G", "-XX:+UseNUMA", "-XX:+UseParallelGC"]
dependencies {
httpkt(it, "http_server_jetty")
httpkt(it, "store_mongodb")
httpkt(it, "hexagon_web")
implementation("at.favre.lib:bcrypt:$bcryptVersion")
implementation("com.auth0:java-jwt:$javaJwtVersion")
testImplementation("com.hexagonkt:port_http_client:$hexagonVersion")
}
private void httpkt(final def dependencies, final String artifact) {
dependencies.implementation("com.hexagonkt:$artifact:$hexagonVersion")
}
I'm trying to publish a library to an AWS S3 Maven repository using this guide. After finally getting it to upload the artifacts to the S3 bucket without error, I made it a dependency of a new project per the guide.
When I tried to build the new project, an error occurred stating that it couldn't find the first of my library's dependencies. Sure enough, there was no pom.xml file generated that would have included that dependency (and others).
Not knowing a lot about how program Gradle tasks, I think the problem is within the pom.withXml {} portion of the script. Or the problem may occur before that since there's not even an empty pom.xml file.
Here is the entire script:
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
// update these next lines to fit your submodule
group = 'com.myproject'
version = '1.0'
// Add sources as an artifact
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "source"
}
// Loop over all variants
android.libraryVariants.all { variant ->
variant.outputs.all { output ->
// This creates a publication for each variant
publishing.publications.create(variant.name, MavenPublication) {
// The sources artifact from earlier
artifact sourceJar
// Variant dependent artifact, e.g. release, debug
artifact source: output.outputFile, classifier: output.name
// Go through all the dependencies for each variant and add them to the POM
// file as dependencies
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
// Filter out anything that's not an external dependency. You shouldn't
// be publishing artifacts that depend on local (e.g. project) dependencies,
// but who knows...
configurations.getByName(variant.name + "CompileClasspath").allDependencies
.findAll { it instanceof ExternalDependency }
.each {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', it.version)
}
}
}
}
}
// Ensure that the publish task depends on assembly
tasks.all { task ->
if (task instanceof AbstractPublishToMaven) {
task.dependsOn assemble
}
}
// Configure the destination repository with
// S3 URL and access credentials
publishing {
repositories {
maven {
url "s3://sdk.myproject.com.s3.amazonaws.com"
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}
Any ideas about what's going wrong? Thank you!
I have a question about Ivy publishing in Gradle.
From Ivy, I expect that if I publish an artifact with for example this dependency:
<dependency org="org.apache.ant" name="ant" rev="1+"/>
My published ivy.xml gets both the fixed and the dynamic version:
<dependency org="org.apache.ant" name="ant" rev="1.9.6" revConstraint="1+"/>
I want that also in Gradle. I have Gradle 2.10.
Here is my Gradle project:
apply plugin: "java"
group = 'org.wibble'
version = "1.2.3"
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.ant', name: 'ant', version: '1+' // resolves to version 1.9.6 at the time of writing
}
uploadArchives.repositories {
ivy { name "testrepo"; url "$buildDir/testrepo" }
}
If I run gradle uploadArchives the resulting ivy.xml just has this:
<dependency org="org.apache.ant" name="ant" rev="1+" conf="compile->default"/>
In the source code of Gradle I do see that there is a facility for writing both rev and revConstraint:
if (!dep.getDynamicConstraintDependencyRevisionId().equals(dependencyRevisionId)) {
<...>
writer.attribute("revConstraint", dep.getDynamicConstraintDependencyRevisionId().getRevision());
}
With debugging I also see that this code is hit, but, in my case both getDynamicConstraintDependencyRevisionId and dependencyRevisionId give '1+' at this point, and the 1.9.6 version is forgotten at this point.
What can I do to get the dependency version recorded in the published ivy.xml, just like in Ivy?
Not sure if this is of any help anymore. But I had to address a similar issue recently, and came up with the following approach. Hope it can help some one else looking for a solution. I derived to this answer based on clues from RaGe's Pom solution and gradle forum solution.
The reason I had to implement this was, the ivy being published by gradle had rev, but not revConstraint attribute. But rev always said "latest.release" which is what is used for our internal libraries. So I had to manually put the rev to actual revision and the revConstraint to latest.release.
publishing {
publications {
ivyJava(IvyPublication) {
from components.java
configurations.create('sources')
artifact(sourceJar) {
type "source"
conf "sources"
classifier "sources"
}
configurations.create('javadoc')
artifact(javadocJar) {
type "javadoc"
conf "javadoc"
classifier "javadoc"
}
descriptor {
withXml {
if (project.configurations.findByName("runtime") != null) {
Map resolvedVersionMap = [:]
Configuration runtimeConfiguration = project.configurations.getByName('runtime')
ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
resolution.getAllComponents().each { ResolvedComponentResult versionResult ->
resolvedVersionMap.put("${versionResult.moduleVersion.group}:" +
":${versionResult.moduleVersion.name}", versionResult.moduleVersion.version)
}
asNode().dependencies.dependency.each { dep ->
if ("latest.release".equalsIgnoreCase(dep.#rev) || "latest.integration".equalsIgnoreCase(dep.#rev)
|| "latest.snapshot".equalsIgnoreCase(dep.#rev)) {
dep.#revConstraint = dep.#rev
dep.#rev = resolvedVersionMap.get("${dep.#org}:" +
":${dep.#name}")
dep.#changing = "true"
}
}
}
}
}
}
}
}
I use maven-publish plugin for deploying android library(.aar).
My library has another dependencies, which are also .aar
How can I import all dependencies from build.gradle, dependencies section:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile ('com.android.support:recyclerview-v7:22.2.1'){
exclude group: 'com.android.support', module: 'support-v4'
}
compile 'com.inthecheesefactory.thecheeselibrary:stated-fragment-support-v4:0.10.0'
//Http communication, websockets, etc.
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
//Fonts
compile 'uk.co.chrisjenx:calligraphy:2.1.0'
//Unit tests
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
//Other
compile ('org.apache.commons:commons-lang3:3.4'){
exclude group: 'org.apache.httpcomponents'
}
//Reactive programmnig
compile 'io.reactivex:rxjava:1.0.13'
compile 'io.reactivex:rxandroid:0.25.0'
compile 'com.github.bumptech.glide:glide:3.6.1'
}
To generated pom.xml dependencies section:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.sdk</groupId>
<artifactId>SDK</artifactId>
<version>0.0.1</version>
<packaging>aar</packaging>
<dependencies>
...
</dependencies>
</project>
I found some explanation how to do similar things:
Optional Gradle dependencies for Maven libraries
How to change artifactory runtime scope to compile scope?
https://issues.gradle.org/browse/GRADLE-1749
http://www.scriptscoop.net/t/6ac07fb41846/how-to-maven-publish-a-gradle-project-jar-with-provided-scope.html
So, I understood, that I should use pom.withXml, import all dependecies from project.configurations.compile.allDependencies with scope compile, and put it into asNode().dependencies
But I'm not familiar with it, and I think I doing something wrong. Here is my current code:
publishing {
publications {
maven(MavenPublication) {
artifact "${project.buildDir}/outputs/aar/${project.name}-release.aar"
artifactId = POM_ARTIFACT_ID
groupId = GROUP
version = VERSION_NAME
// Task androidSourcesJar is provided by gradle-mvn-push.gradle
//artifact androidSourcesJar {
// classifier "sources"
//}
pom.withXml {
def depsNode = asNode().dependencies.'*'
project.configurations.compile.allDependencies.each { dep ->
if(dep.name != null && dep.group != null && dep.version != null) {
def depNode = new Node(null, 'dependency')
def groupIdNode = new Node(depNode, 'groupId', dep.getGroup())
def artifactIdNode = new Node(depNode, 'artifactId', dep.getName())
def versionNode = new Node(depNode, 'version', dep.getVersion())
depsNode.add(depNode)
println depsNode
}
}
}
}
}
repositories {
maven {
credentials {
username System.getenv('NEXUS_USER_NAME')
password System.getenv('NEXUS_PASSWORD')
}
url "http://nexus-repo"
}
}
}
To publish a .aar library with the dependencies listed correctly in pom.xml, it might be easier to use this plugin, rather than assemble the dependencies section yourself using the maven-publish plugin.
To apply plugin:
plugins {
id "com.github.dcendents.android-maven" version "1.3"
}
and run task install to push library to local .m2 repo.
You can override the repo being published to like this:
install {
repositories {
mavenDeployer {
repository(...)
}
}
}
You can of course, continue to use the maven-publish plugin if you so prefer. In your code, you're looking for depsNode which does not already exist. You likely need to create a new Node for depsNode and add it to the pom first. Or just use append:
pom.withXml {
def depsNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each { dep ->
def depNode = depsNode.appendNode('dependency')
depNode.appendNode('groupId', dep.group)
depNode.appendNode('artifactId', dep.name)
depNode.appendNode('version', dep.version)
//optional add scope
//optional add transitive exclusions
}
}
The caveat here is that, you still need to handle exclusions correctly. Also, if you have variants in your project, and have different compile configurations such as androidCompile or somethingElseCompile, you need to handle those correctly as well.
You can update the dependencies with the dependencies of another project or library in the maven publish task. This has to be done before jar generation task. In this way, the changes will be reflected in the pom generation and no need for pom xml manipulation. getDependencies is the method that you extract those dependencies and you should implement it ;)
This is the snippet in the publish task
artifactId = POM_ARTIFACT_ID
groupId = GROUP
version = VERSION_NAME
project.configurations.implementation.withDependencies { dependencies ->
dependencies.addAll(getDependencies(project))
}
from components.java
I'm using the "maven" plugin to upload the artifacts created by Gradle build to Maven central repository. I'm using a task similar to the following one:
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
pom.project {
name 'Example Application'
packaging 'jar'
url 'http://www.example.com/example-application'
scm {
connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
url 'http://foo.googlecode.com/svn/trunk/'
}
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
}
}
}
}
However the POM file created by this task does not report correctly the dependencies that have been excluded in my Gradle build file. For example:
dependencies {
compile('org.eclipse.jgit:org.eclipse.jgit.java7:3.5.2.201411120430-r') { exclude module: 'commons-logging' }
compile('com.upplication:s3fs:0.2.8') { exclude module: 'commons-logging' }
}
How to have excluded dependencies managed correctly in the resulting POM file?
You can simply override the dependencies of the pom by filtering out the unwanted dependencies, e.g. to exclude junit you can add the following lines to the mavenDeployer configuration:
pom.whenConfigured {
p -> p.dependencies = p.dependencies.findAll {
dep -> dep.artifactId != "junit"
}
}
The problem was that in the exclude definition was not specified the group but only the module.
Adding the both of them the exclusions are added correctly in the POM file. For example:
compile('org.eclipse.jgit:org.eclipse.jgit.java7:3.5.2.201411120430-r') {
exclude group: 'commons-logging', module: 'commons-logging'
}
compile('com.upplication:s3fs:0.2.8') {
exclude group: 'commons-logging', module: 'commons-logging'
}
Using 'exclude' on a Gradle dependency is normally the correct answer, but I still needed to remove some of my "runtimeOnly" dependencies from the POM that led me to this StackOverflow page. My testing using Gradle 4.7 seems to show that using "compileOnly" leaves the dependency out of the pom entirely, but "runtimeOnly" adds a "runtime" dependency in the pom, which in my case, is not what I wanted. I couldn't figure out a "standard" Gradle way of leaving runtime dependencies out of the POM.
The pom.whenConfigured method shown in another answer works for legacy "maven" plugin publishing, but doesn't work for the newer "maven-publish" plugin. My experimentation led to this for "maven-publish":
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
pom.withXml {
asNode().dependencies.dependency.each { dep ->
if(dep.artifactId.last().value().last() in ["log4j", "slf4j-log4j12"]) {
assert dep.parent().remove(dep)
}
}
}
}
}
}