Set "Packaging" to POM file with Maven Gradle Plugin - maven

I have an maven gradle plugin in my Android project, that generates the pom files (I only need the POM files, not to actually upload to artifactory).
I am generating an Android libraries (aar), so I need to add
<packaging>aar</packaging>
to the Pom file. However, the pom { } field does not have any "packaging" option. Any idea how to do it?
This is my current code:
task writePom {
doLast {
pom {
//packaging 'aar'
}.writeTo("$buildDir/pom.xml") } }

Related

How to publish an obfuscated jar file and pom to Nexus repository with gradle

I need to publish a couple of obfuscated jar files with their POM (before obfuscation) to a Nexus repository with Gradle.
The problem is if I select components.java then the non-obfuscated jar file is deployed.
I am using yGuard for obfuscation, and now in my build file I have created pom file, clean jar file and obfuscated jar file, but cannot find a way to publish the pom and obfuscated jar file together to our Nexus repository.
By the way the project is a multi module java-library project.
In my main build.gradle file I have something like
configure(subprojects) {
apply plugin: 'java-library'
apply plugin: 'maven-publish'
jar {
archiveFileName = "${project.name}-${project.version}-clean.jar"
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForCleanPublication }
rename '.*', 'pom.xml'
}
}
publishing {
publications {
clean(MavenPublication) {
from components.java
withBuildIdentifier()
}
}
repositories {
maven {
name 'nexus'
url 'SOME URL'
allowInsecureProtocol = true
credentials {
username 'someUserName'
password 'somePassword'
}
}
}
}
}
so the pom is created according to module dependencies, but now I cannot publish the obfuscated jar file with same POM file.
Notes: The obfuscated jar files are generated in same folder as 'build\lib' without '-clean.jar` suffix.
Notes: when I remove from components.java and add my own artifact (obfuscated file) the generated pom won't have dependencies, and I couldn't find a way to override the jar file added to publication.

How to publish a Gradle shadow jar without a shadow pom

I use the shadow jar plugin (https://github.com/johnrengelman/shadow) to create an extra jar file with all my dependencies packaged inside. I also would like to keep the default generated jar that has only my code & resources. This is pretty simple and straightforward until it comes to publishing the jars.
As my shadow jar has all its deps inside, its pom file is not a priority for me in terms of publishing. However if I follow the instructions laid out in the shadowJar plugin documentation (https://imperceptiblethoughts.com/shadow/publishing/#publishing-with-maven-publish-plugin)
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
I end up with two different pom files (one for the regular jar file and one for the shadow jar) published to the same URL with one overriding the other. This behavior causes customers to download the default jar but without any dependencies in the pom file.
I tried many ways to disable shadowJar pom file but without any success. How do I get it to work?
Eventually I just ignored the instructions in the plugin's documentation and published the shadow jar just like I publish my sources jar:
Simplest way to do this for a single project is:
publishing {
publications {
Library(MavenPublication) {
from project.components.java
artifact tasks.sourceJar
artifact tasks.shadowJar
}
}
}
If you have multiple projects (not all having shadowJar plugin applied) and want to add this logic in the root project, you can do this:
subprojects {
afterEvaluate {
publishing {
publications {
Library(MavenPublication) {
from project.components.java
artifact tasks.sourceJar
if (project.tasks.findByName("shadowJar") != null) {
artifact tasks.shadowJar
}
}
}
}
}
}

Setting POM version for upload archive in Gradle build

I am using Gradle to build my own Android Libraries. I put this libraries into a gradle multi project.
Root Project
Lib A
Lib B
and so on.
I am trying to upload the created aar files into my local maven repo (.m2 directory)
I am using the following commands in my gradle file of the Root Project
uploadArchives {
repositories {
mavenDeployer {
repository(url: mavenLocal().url)
pom.groupId = rootProject.group
pom.artifactId = project.name
pom.version = project.version
}
}
}
everything works fine except the settings for the project.version. Where can I define the value for every single subproject?
Regards
Michael
Try checking out Gradle-Fury at https://github.com/gradle-fury/gradle-fury
Essentially, set the pom.version and various other settings in gradle.properties, add a few apply from throughout the build.gradle files and you're set. Definitely works with direct publication to sonatype oss/central.
These settings can be overridden on a per module basis by adding a gradle.properties file in each module that you need to override the parent's settings from.
declaimer: i'm one of the authors

maven project as dependency in gradle project

I have a project which is using Gradle as build tool and a second subproject which is using Maven's POM. I don't have the freedom of changing build tool on the subproject.
What I want to achieve is to add my project with Maven POM as dependency on my Gradle project.
Where root (current dir) is my project with Gradle and contains the build.gradle, the Maven project is under vendor/other-proj/ with POM file just under that directory.
I have tried these variations on my build.gradle file:
1st try:
include("vendor/other-proj/")
project(':other-proj') {
projectDir = new File("vendor/other-proj/pom.xml")
}
dependencies {
compile project(':other-proj')
}
2nd try:
dependencies {
compile project('vendor/other-proj/')
}
3rd try:
dependencies {
compile project('vendor/other-proj/pom.xml')
}
4th try:
dependencies {
compile files 'vendor/other-proj/pom.xml'
}
I can't find anything related on the web, it seems most Gradle/Maven use cases are affected by publishing to Maven or generating POM, but I dont want to do any of those.
Can anybody point me to right direction?
you can "fake" including a Maven project like this:
dependencies {
compile files("vendor/other-proj/target/classes") {
builtBy "compileMavenProject"
}
}
task compileMavenProject(type: Exec) {
workingDir "vendor/other-proj/"
commandLine "/usr/bin/mvn", "clean", "compile"
}
This way Gradle will execute a Maven build (compileMavenProject) before compiling. But be aware that it is not a Gradle "project" in the traditional sense and will not show up, e.g. if you run gradle dependencies. It is just a hack to include the compiled class files in your Gradle project.
Edit:
You can use a similar technique to also include the maven dependencies:
dependencies {
compile files("vendor/other-proj/target/classes") {
builtBy "compileMavenProject"
}
compile files("vendor/other-proj/target/libs") {
builtBy "downloadMavenDependencies"
}
}
task compileMavenProject(type: Exec) {
workingDir "vendor/other-proj/"
commandLine "/usr/bin/mvn", "clean", "compile"
}
task downloadMavenDependencies(type: Exec) {
workingDir "vendor/other-proj/"
commandLine "/usr/bin/mvn", "dependency:copy-dependencies", "-DoutputDirectory=target/libs"
}
You cannot "include" a maven project in gradle settings.gradle. The simplest way would be to build the maven project and install it to your local repo using mvn install(can be default .m2, or any other custom location) and then consume it from your gradle project using groupname:modulename:version
repositories{
mavenLocal()
}
dependencies{
implementation 'vendor:otherproj:version'
}
It is possible to depend directly on the jar of the maven project using compile files but this isn't ideal because it wont fetch transitive dependencies and you'll have to add those manually yourself.

Getting Gradle to publish a source Jar instead of a standard compiled Jar

I am slowly moving two projects over to Gradle. Project A produces a jar file that is used by Project B.
Project A is a GWT component and Project B is a GWT application.
How do I get Gradle to generate a Jar that contains the Java source code of Project A rather than the compiled classes?
I have tried adding the following to the build.gradle file, but this generates a second Jar file containing the source - I want the main artefact to contain the source, so that it is published to my local Maven repository when I run the install task.
task sourceJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourceJar
}
Is there a way to override the standard Jar creation task?
You can define your own 'myArtifacts' configuration and publish that instead. Note that since the install task is of type Upload, you should be able to change the default artifacts configuration from archives to sourceArchives:
configurations{
sourceArchives
}
artifacts{
sourceArchives sourceJar
}
install.configuration = configurations.sourceArchives
Hopefully, install should now just publish members of sourceArchives configuration.

Resources