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.
Related
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.
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
I keep getting this error. Any ideas?
What went wrong:
A problem occurred configuring project ':app'.
Could not resolve all dependencies for configuration ':app:classpath'.
Could not find com.android.tools.build:gradle:2.0.0-beta7.
Searched in the following locations:
https://repo1.maven.org/maven2/com/android/tools/build/gradle/2.0.0-beta7/gradle-2.0.0-beta7.pom
https://repo1.maven.org/maven2/com/android/tools/build/gradle/2.0.0-beta7/gradle-2.0.0-beta7.jar
You are referring maven central for finding android plugin artifacts, android gradle plugin is published at jcenter
add jcenter() to buildscript.repositories block in your root build.gradle file.
It should look like as below:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:X.X.X' // your verison
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
I've already solved this but i'm still curious as to why gradle behaves this way.
In my gradle project i have 2 gradle projects, one named app that is configured with the ear plugin and one named core that is deployed in the lib folder of the ear
I was getting the following error when trying to build my gradle project:
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':app:earlib'.
> Could not find org.slf4j:slf4j-api:1.7.5.
Required by:
saturn:app:unspecified > saturn:core:unspecified
> Could not find commons-io:commons-io:2.4.
Required by:
saturn:app:unspecified > saturn:core:unspecified
> Could not find org.slf4j:slf4j-log4j12:1.7.5.
Required by:
saturn:app:unspecified > saturn:core:unspecified
> Could not find log4j:log4j:1.2.17.
Required by:
saturn:app:unspecified > saturn:core:unspecified
My app build.gradle file is:
apply plugin: 'ear'
dependencies {
earlib project(":core")
}
and my core build.gradle is:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
runtime 'org.slf4j:slf4j-log4j12:1.7.5'
runtime 'log4j:log4j:1.2.17'
testCompile 'junit:junit:4.11'
}
Temporary Solution:
I was able to resolve the issue by adding the mavenCentral repository to my app build.gradle like so:
apply plugin: 'ear'
repositories {
mavenCentral()
}
dependencies {
earlib project(':core')
}
HOWEVER I'm still curious as to why the depending project needs to know what repository the core project resolves it's dependencies from. this document on dependency management doesn't seem to have a very good explanation.
When resolving a configuration, Gradle (only) uses the repositories declared in the same project as the configuration. Hence when resolving the ear project's earlib configuration, only the ear project's repositories are taken into account. That's one reason why it's common to declare all repositories under subprojects in the root build script.
Java Spring project with Gradle 1.9 and vertx. Local gradle distribution.
Some lines of build.gradle
apply plugin 'java'
apply plugin 'groovy'
apply plugin 'idea'
buildscript {
repositories {
jcenter()
}
}
repositories {
mavenCentral()
}
dependencies {
...
compile 'org.springframework:spring-context-support:3.2.5.RELEASE'
compile 'org.springframework:spring-aop:3.2.5.RELEASE'
compile 'org.springframework:spring-aspects:3.2.5.RELEASE'
...
}
I have an existing gradle project downloaded from git with xml configs in .idea/libraries folder named spring-aop_3_2_5_RELEASE.xml, for example, where we can find xml tag <library name="spring-aop-3.2.5.RELEASE">...</library>.
After I had imported this project new file Gradle__spring-aop_3_2_5_RELEASE.xml appeared with only difference in name attribute of the library tag: Gradle: spring-aop-3.2.5.RELEASE. So i have duplicate xml configs for dependencies. I wonder why my gradle added that prefix.
The prefix is hardcoded, IDEA 13 needs a reimport of your old Gradle projects that were created in IDEA 12. It's not obvious, but there will be a notification about it in the next update.
In the Gradle generated project you can exclude the library files from the version control, same for the .iml files that can be also ignored when using Maven. Other files can be still shared (like code style, run configurations, inspection profiles, etc). Check this document for details.