I have a project which has a SharedCode
(Java) module and secondly an Android
(Android library) module which depends on the SharedCode
module. I've previously been using the maven
plugin in my build.gradle
files and I've been using the uploadArchives
task of that plugin to publish the artifacts from my two modules. This has worked and produced pom files which reflect the dependencies in my build.gradle
files.
I thought I'd replace the old maven
plugin with the new maven-publish
plugin. However, I see that the pom files produced by the maven-publish
plugin contain no dependencies. Is this by design, is this a bug in the plugin or am I using the plugin incorrectly?
The build.gradle
file in my SharedCode
module is as follows:
apply plugin: 'java'
apply plugin: 'maven-publish'
group = "${projectGroupId}"
version = "${projectVersionName}"
dependencies {
compile 'com.google.guava:guava:18.0'
}
publishing {
publications {
SharedCode(MavenPublication) {
groupId "${projectGroupId}"
artifactId 'SharedCode'
version "${projectVersionName}"
artifact("$buildDir/libs/SharedCode-${projectVersionName}.jar")
}
}
}
The build.gradle
file in my Android
module is as follows:
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
group = "${projectGroupId}"
version = "${projectVersionName}"
android {
// android stuff here...
}
dependencies {
compile project(':SharedCode')
}
publishing {
publications {
Android(MavenPublication) {
groupId "${projectGroupId}"
artifactId 'Android'
version "${projectVersionName}"
artifact "$buildDir/outputs/aar/Android-release.aar"
}
}
}
The pom file created from the SharedCode
module is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.sdk</groupId>
<artifactId>SharedCode</artifactId>
<version>0.0.2</version>
</project>
The pom file created from the Android
module is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.sdk</groupId>
<artifactId>Android</artifactId>
<version>0.0.2</version>
<packaging>aar</packaging>
</project>
Note the absence of the dependencies in the pom files.
An important part of a Maven publication is the POM file. We already saw that Gradle added a generatePom<publicationName> task to our project. Furthermore, we can define some properties of the POM file inside a publication configuration. Gradle also offers a hook to customize the generated POM file even further.
The Maven Publish Plugin provides the ability to publish build artifacts to an Apache Maven repository. A module published to a Maven repository can be consumed by Maven, Gradle (see Declaring Dependencies) and other tools that understand the Maven repository format.
We can publish a pre-built jar into a Maven repo using Gradle by using java plugin and maven-publish plugin. maven-publish has a feature to inject dependencies into generated pom using pom.
Use from components.java
instead of the artifact...
line in your publications
section for the java project. That should produce the dependencies in the pom automatically.
The android project isn't recognized as a standard java project, which makes it a bit trickier. You can create your own dependencies section using pom.withXml {}
and then iterating through your dependency list. Alternatively, there is this gradle plugin that does it for yout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With