Build with gradlew and make it available in ./m2 [duplicate] - java

I have 2 different project build on mvn. I am trying to replace to Gradle.
Project 1 is an SDK, and project 2 is using that sdk (example).
In the time of maven it creates artifact using mvn install which adds the whole project into local repository.
I like to work in gradle like that. I like project 1 build.gradle need to post it as a gradle local repository and then example project need to use it.
In maven we do mvn install which adds a project artifact into .m2 folder but how to do in gradle so what i can add a project artefact's into the local repository.
Any way that I can do so?

sdk/build.gradle:
apply plugin: "maven"
group = "foo"
version = "1.0"
example/build.gradle:
repositories {
mavenLocal()
}
dependencies {
compile "foo:sdk:1.0"
}
$sdk> gradle install
$example> gradle build

You may be looking for:
gradle publishToMavenLocal
build.gradle:
plugins {
// other plugins
id 'maven-publish'
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
}
See: Maven Publish Plugin

Check out Gradle's documentation on multi-project builds.
Here's an example, with some extra dependencies. Just call gradle install in the root folder, and all will be built and put to your local repo.
Folder structure:
root
+--> build.gradle
+--> settings.gradle
+--> sdk
| +--> build.gradle
+--> example
+--> build.gradle
root/build.gradle:
allprojects {
apply plugin: 'java'
apply plugin: 'maven'
group = 'myGroup'
version = '0.1-SNAPSHOT'
}
root/settings.gradle:
include 'sdk'
include 'example'
root/sdk/build.gradle:
dependencies {
// just an example external dep.
compile group:'commons-lang', name:'commons-lang', version:'2.3'
}
root/example/build.gradle:
dependencies {
compile project(':sdk')
compile group:'log4j', name:'log4j', version:'1.2.16'
}

You need to publish your own library to your local repository. You can do that in the following way:
Add maven-publish plugin:
plugins {
// your other plugins come here...
id 'maven-publish'
}
Add the publishing section to your build file:
publishing {
publications {
myCoolLibrary(MavenPublication) {
from components.java
}
}
}
Run gradle build publishToMavenLocal
Find more details in the documentation.

Related

Gradle task 'wrapper' not found in project ':subproject'

I have root gradle project which needs to have a small Spring Boot subproject. This subproject will be deployed in the same pod as root project, so it needs to be built whenever root project is built. I tried by creating the following structure:
subproject/
├─ src/...
├─ build.gradle
settings.gradle
with the following contents:
settings.gradle:
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'root'
include 'subproject'
build.gradle:
plugins {
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.demo'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
But when I refresh the project from within the IDEA, it shows Task 'wrapper' not found in project ':subproject'. What did miss? This subproject cannot exist by itself and shouldn't have a 'wrapper', it should be build by the root project.
$ ./gradlew -q projects
Root project 'root'
+--- Project ':subproject'
Gradle version: 6.8.3, Java version: 11
The problem actually does not exist. Everything is correct, except that IDEA added subproject as separate gradle project (I guess). So when I hit refresh, it could not be performed because IDEA was trying to find wrapper in the subproject. Simply saying, if you have such problem, check how many projects you have in your IDEA's Gradle view. In my case, there was two - root and subproject, the latter is unneeded.

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

How to build Spring Statemachine with maven

I downloaded Spring Statemachine (ZIP)
I don't have any a pom.xml/maven instruction
In [site] the maven link isn't available https://github.com/spring-projects/spring-framework/wiki/Downloading-Spring-artifacts
for Maven repository information.
How should I build project with maven ?
Its gradle project.. You can make use of gradle to build it.
Gradle to Maven:
Add Maven plugin in the build.gradle file.
build.gradle should look like this:
apply plugin: 'java'
apply plugin: 'maven'
group = 'com.abc.app'
// artifactId is taken by default, from folder name
version = '0.1-SNAPSHOT'
dependencies {
compile 'commons-lang:commons-lang:2.3'
}
Run gradle install in the directory containing build.gradle will do the job.
It will create pom-default.xml in the build/poms subfolder.
Reference links for Gradle Build:
https://guides.gradle.org/creating-new-gradle-builds/
https://www.eclipse.org/community/eclipse_newsletter/2018/february/buildship.php
https://spring.io/guides/gs/gradle/
https://www.jetbrains.com/help/idea/gradle.html

Can't resolve dependency for jsoup

I'm creating a news downloading gradle project with the following directory structure.
news-feed (root)
|-bbc-plugin (sub-project)
I want to use the jsoup library for my sub-projects so I add the dependency to my root build.gradle file as follows.
import org.gradle.api.artifacts.*
apply plugin: 'base' // To add "clean" task to the root project.
apply plugin: 'application'
mainClassName = 'newsFeed.MainMenu'
subprojects {
apply from: rootProject.file('common.gradle')
apply plugin: 'java'
dependencies {
compile 'org.jsoup:jsoup:1.10.3'
}
}
dependencies {
compile project(':bbc-plugin')
}
I can't build the project because of the error
* What went wrong:
Could not resolve all files for configuration ':bbc-plugin:compileClasspath'.
> Can't resolve external dependency org.jsoup:jsoup:1.10.3 because no repositories are defined.
Required by:
project :bbc-plugin
Is there are way to specify the dependency in the root build file without having to specify in the build.gradle file of each sub-project?
You need to tell gradle from which repository it can download your dependency. To achieve this, you need to add a repositories section to your build script.
To use Maven Central, for instance, you need to add the following lines to your to root build.gradle:
repositories {
mavenCentral()
}
See here and here in the Gradle user guide for a more detailed description.

Gradle multimodule project build in IntelliJ - relation between artifacts and wars

I'm facing some problems with my multimodule project gradle build in IntelliJ. My gradle build works correctly, but IntelliJ is not picking it up as it should causing my development cycle to slow down drastically.
I have this dependency setup between my projects (5 projects, 3 wars, shared has my model etc. common-web has the common web components)
TD (war) -> common-web
CP (war) -> common-web
CP (war) -> Shared
Web (war) -> common-web
Web (war) -> Shared
Shared
common-web
With the following structure:
|- TD
|- Web
|- Shared
|- common-web
|- CP
| settings.gradle
| build.gradle
I have one gradle build file and one gradle settings file.
include 'TD', 'CP', 'Web', 'Shared', 'common-web'
allprojects {
repositories {
mavenCentral()
}
sourceCompatibility = 8
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
dependencies {
// dependencies...
}
}
project(':CP') {
apply plugin: 'war'
dependencies {
// dependencies...
compile project(':shared')
compile project(':common-web')
}
}
project(':web') {
apply plugin: 'war'
dependencies {
// dependencies...
compile project(':common-web')
compile project(':shared')
}
}
project(':shared') {
dependencies {
// dependencies...
}
}
project(':TD') {
apply plugin: 'war'
dependencies {
// dependencies...
compile project(':common-web')
}
}
project(':common-web') {
apply plugin: 'war' // needed to allow the providedComp
dependencies {
// dependencies...
}
}
Is there a way I should configure the gradle idea plugin to allow this to be build correctly? have found the make button does not work like I think it should work, ignoring the compile errors in the modules etc. I see that in the root of the project, IntelliJ creates a folder classes/artifacts and does not use the (correct) build in the module/lib folder.
The errors I'm seeing is the wrong include of the jars. For example the web project has a dependency to the TD project, which is defined nowhere.
Is this a bug in the gradle plugin of IntelliJ or am I doing something I shouldn't?

Categories

Resources