Generate a pom.xml file for multi-module Gradle project - java

I'm trying to generate a pom.xml file for my multi-module Gradle project.
My settings.gradle file looks as follows:
include 'moduleOne'
include 'moduleTwo'
include 'moduleThree'
Each one of the submodules may or may not declare its own dependencies, and I would like for all of them to be included into the result "pom.xml" file.
When using maven Gradle plugin as follows:
/* my buildscript... */
task createPom {
pom {
project {
groupId 'org.test'
artifactId 'test'
version '1.0.0-SNAPSHOT'
}
}.writeTo('pom.xml')
}
in the build.gradle of the root project a "pom.xml" with no dependencies is generated, which is no surprise - the root project itself declares no dependencies.
When creating the createPom task in the subprojects closure, many pom files that contain dependencies are generated, but I would like to have 1 "pom.xml" with all of the dependencies.
Is there a way I can automatically merge all of my dependencies into one super "pom.xml" file?

Related

Gradle Dependency Implementation

I am having two project(ProjectA and projectB). ProjectA have 2 tasks of creating jarA1.jar and jarA2.jar. This is configured with ProjectA's build.gradle. Now I want to add these two created jars as dependency to ProjectB , by just mentioning the project and not specifying the jar's file path separate.
Suppose in future if I add multiple jars at ProjectA , all the jars must be added as dependency to ProjectB without any changes in projectB's build.gradle.
ProjectA => build.gradle
task jarA1(type: Jar, dependsOn: compileGrpcJava) {
...
}
task jarA2(type: Jar, dependsOn: compileGrpcJava) {
...
}
jar {
dependsOn jarA1
dependsOn jarA2
}
ProjectB => build.gradle
Approach1:
dependencies {
implementation project (:ProjectA)
}
is not importing the Jars. But this works.
Approach2:
dependencies {
implementation project (:ProjectA)
implementation files('<path_to_jarA1.jar')
implementation files('<path_to_jarA2.jar')
}
Can anyone say why approach 1 is not working. Why adding the complete project as dependency not taking up all the jars of that project by default?

Gradle to add module dependency in Micronaut project

I have three module as shown below
The fete-bird-apigateway depend on common and fete-bird-product depend on both fete-bird-apigateway and common
In the fete-bird-product settings.gradle I have included the code below
rootProject.name = "fete-bird-product"
include 'fete-bird-apigateway' , 'common'
and in the build.gradle of project
dependencies {
implementation project(':common')
}
Error
Caused by: org.gradle.internal.component.NoMatchingConfigurationSelectionException: No matching configuration of project :common was found.
I don't want to create a multi-module build project describe here https://docs.gradle.org/current/userguide/multi_project_builds.html. Each project should build individually and dependent modules should load while building.
How can I achieve this?
Well I found that I had the wrong concept for different module projects.
Assuming all modules are part of the same multi-module build then in fete-bird-apigateway.gradle and service\build.gradle you add:
plugins {
id 'java'
id 'maven-publish'
}
dependencies {
implementation project(':common')
}
However if common, fete-bird-apigateway and service are separate projects and don't share the same root build.gradle you have to publish the common module into a shared repository and use it like any regular dependency. Easiest to do with Maven Local repository.
To publish to the local maven
In fete-bird-apigateway.gradle
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'library'
version = '1.1'
from components.java
}
}
}
Reference - https://docs.gradle.org/current/userguide/declaring_repositories.html
https://docs.gradle.org/current/userguide/publishing_maven.html#gsc.tab=0
In the dependent project add the dependency as regular
repositories {
mavenLocal()
}
implementation("fete.bird:fete-bird-apigateway:0.1")
We need to run the task or gradle command for publish. I am using Intellj so did with below task
We can run the gradle command gradle publishToMavenLocal

Publish Android library with external dependencies

I have an Android closed source module that will be be used as a library in other projects. It contains external dependencies.
To publish it, I'm creating a Maven artifact with the following gradle task:
apply plugin: 'maven'
def coreAarFile = file('...build\\outputs\\aar\\android-sdk-release.aar')
artifacts {
archives coreAarFile
}
uploadArchives {
repositories.mavenDeployer {
repository(url: "file://.\\mvn-repo")
pom.project {
groupId 'a.blabla'
artifactId 'blabla-sdk'
version "1.0.0"
}
}
}
It generates the .aar file, the pom.xml, etc without problems.
Then I create a project that have a dependeny to my library declared. It works until it needs to access to the external dependencies, when throws a class not found exception.
How can I edit my gradle task to include external dependencies or at least a reference to them? They are published in mvnrepository.com and github.com.
I moved uploadArchives to the build.gradle of the module and removed the artifacts element. It works!
Thanks to CommonsWare for pointing to the right direction.

Sharing class path configuration between multiple gradle subprojects

I have two different java projects, projectA and projectB. I'm at a stage where projectB depends on projectA. How do I pull in the jars created by building projectA and all of its dependencies?
In essence, I would like to mirror the effect of adding a projectA to the build path of projectB in eclipse,effectively pulling in jars on the classpath projectA.
If the two projects are closely related you can make Project A and Project B part of the same Multi-project build, then:
//ProjectB build.gradle
dependencies {
compile project(':ProjectA')
}
If they're not closely related you can publish the output of Project A to a repository e.g. your local Maven, Artificatory, Bintray etc. Then in Project B you'll get it like any other dependency. E.g.
//ProjectB build.gradle
repositories {
mavenLocal()
//Or something like this:
//maven { url "http://urltoyourartifactory.com/repo/" }
}
dependencies {
compile 'com.yourname:project-a:1.0.0'
}

How to alter dependencies for a generated artifact?

Gradle 2.3; shadow plugin 1.2.1.
In my build.gradle, I use the shadow plugin in order to repackage a dependency, like such:
shadowJar {
relocate("com.google.common", "r.com.google.common");
}
I also add the shadow jar to the list of artifacts to publish:
artifacts {
archives jar;
archives sourcesJar;
archives javadocJar;
archives shadowJar;
}
However the list of dependencies of the shadow jar still contains all the dependencies of the "normal" jar, even though it has every dependency builtin.
Is this the intended behavior? How can I make the shadow jar exclude this or that dependency?
Here at work we had the same problem and we just put this in a build.gradle of one of our projects:
def installer = install.repositories.mavenInstaller
def deployer = uploadArchives.repositories.mavenDeployer
[installer, deployer]*.pom*.whenConfigured { pom ->
pom.dependencies.retainAll {
it.groupId == 'our.group.id' && it.artifactId == 'some-api'
}
}
This removes all dependencies from the pom.xml except for the dependency on one of our API projects.
(And it is a pretty verbatim copy of an example from the official Gradle documentation.)

Categories

Resources