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
I am trying to understand what repositories does in buildscript. I have read quite a few posts explaining it, but I still feel that I have not grasped the concept. Here is the project build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
// repositories {
// jcenter()
// maven {
// url 'https://maven.google.com/'
// name 'Google'
// }
// }
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
google()
}
}
You can see that I have commented out repositories in buildscript. The app is built perfectly, and published without any problems. Could anyone shed some light on this? Maybe an example showing repositories in buildscript is required will help me understand it finally.
After trial and error, Android Studio provides the plugins or at least sources of the plugins itself while compiling and building even if the repositories block was commented out. This is most probably due to the fact of it's high integration with Gradle. However when building from the command line using gradlew assembleDebug or something similar, the Gradle wrapper will throw the following error:
"Cannot resolve external dependency
com.android.tools.build:gradle:3.0.1 because no repositories are
defined."
As to further explain the concepts for clarity, the repositories under buildscript defines the sources of downloading the plugins used in compiling your Gradle project. For example the Android Gradle Plugin is downloaded by defining the repo that contains it (Google maven in this case) and adding the dependency com.android.tools.build:gradle:3.0.1 for the buildscript. Hence by doing that, you'll be able to add the plugin with apply plugin: 'com.android.application' inside your app module build.gradle file. This is not yet related to your code libraries, this is just related to the build system in order to compile the project Projects by using plugins which have tools and extra tasks to compile them properly.
If you realize below the buildscript block, there is the allprojects block which states the repositories used in order to handle project dependencies. Meaning the libraries that your are using in your app (which are also inside the dependencies block in app build.gradle file) are retrieved from there and in this case they are from JCenter, JitPack.io and Google Maven.
I'm trying to create a new Kotlin project that builds with Gradle using IntelliJ IDEA (2016.2.5 on Ubuntu 16.04). When I do this I immediately get an error message.
Here's what I'm trying:
Select "Create New Project" from Welcome Screen.
Select "Gradle" from left hand pane, "Kotlin (Java)" from right. Click "Next".
Enter "hello-world" as ArtifactId. Click Next.
Ensure "Create separate module from source set" and "Use default Gradle wrapper" are
selected, nothing else is. Click "Next".
Use defaults for project name and location. Click "Finish".
I then immediately get this error:
Gradle 'hello-world' project refresh failed
Error: Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.1-M02-12.
Searched in the following locations:
https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.pom
https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.jar
Required by:
:hello-world:unspecified
The generated build.gradle looks like this:
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1-M02-12'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
How can I create a Kotlin project that builds with Gradle correctly?
In your build.gradle, change ext.kotlin_version to be:
ext.kotlin_version = '1.1-M02'
It's a minor bug that the IDE plugin puts its own version into build scripts, not the Kotlin version.
And also add the 1.1 EAP repository to the repositories in both buildscript and the root scope:
repositories {
// ...
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" }
}
Kotlin artifacts related to EAP versions are not put into Maven Central like those of public releases, and this repository is not added automatically into the generated build script.
Then refresh the Gradle project, and the build should pass.
Feel free to check with this build.gradle file.
I'm pretty new to Gradle and IntelliJ and I'm trying to understand how it works [cos that's what my project uses]. I created a simple java project in Intellij IDEA with a HelloWorld class and manually added the gradle.build file along with the necessary dependencies as follows:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = 'main.java.test.HelloWorld'
dependencies {
compile 'joda-time:joda-time:2.2'
}
repositories {
mavenLocal()
mavenCentral()
}
jar {
baseName = 'gradle-test'
manifest {
attributes 'Main-Class': 'main.java.test.HelloWorld'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2'
}
While I specified the joda-time dependency, my IntelliJ project is unaware of this library and therefore I can't use of any of the autosuggest features of IntelliJ for the joda-time library.
Could someone give me information on how I can get IntelliJ to automatically start recognizing the downloaded jars and allow me to use the auto-suggest features?
In other words,
where are the dependency jars downloaded ?
How can I have these jars to automatically be included in the
class path of the project ? [Is it ok to do so ? If not, what
would be your recommendation ?]
Thanks !
I used Peter's suggestion to run gradle idea and imported the project into IntelliJ and I was able to start using the auto-suggestion features in IntelliJ.
Thanks
I have a multiproject build with multiple war modules that depends on one jar module.
Both war and jar modules have dependencies over libraries like Spring, Hibernate and so on, those dependencies are defined as providedCompile on the war modules and as compile on the jar.
The problem is that when JetGradle updates the dependencies all artifacts have errors, as the dependencies from the jar module are required on the artifacts.
I would like to use any of this solutions:
Include the libraries on the lib folder of the server and have Intellij treat them as provided.
Include the libraries as project wide libraries somehow, so intellij puts them on all artifacts even after the gradle dependencies are updated.
On the other hand my approach could be completely wrong from the beginning.
The dependencies in the war modules are defined as:
providedCompile 'org.slf4j:slf4j-log4j12:1.7.5'
providedCompile 'org.slf4j:jcl-over-slf4j:1.7.5'
...
compile(project(':jarModule')) {transitive = false}
...
The dependencies in the jar module are defined as:
...
compile 'org.slf4j:slf4j-log4j12:1.7.5'
compile 'org.slf4j:jcl-over-slf4j:1.7.5'
...
The best solution I found was to set the transitive "compile" dependencies from the jar module as provided using the following code in the Gradle configuration file:
apply plugin: 'idea'
configurations {
provided
provided.extendsFrom(compile)
}
idea {
module {
scopes.PROVIDED.plus += configurations.provided
}
}
For Gradle 2.0+ modify the last bit to be like this:
idea {
module {
scopes.PROVIDED.plus += [configurations.provided]
}
}
This solution works using the Intellij Gradle plugin and also the idea task in Gradle
I got this solution working based on the info on this urls:
https://github.com/Netflix/RxJava/pull/145
http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
I hope this helps someone else
I tried the above solution but found a problem. In my scenario I had a sub-project that had the above configuration. The problem was that the transitive dependencies of the sub-project were not being exported in the IntelliJ configuration, which caused the base project to stop compiling.
I did some digging around and stumbled upon this little gem which fixed the problem.
https://github.com/gradle/gradle/blob/ccddc438ce09293d84030ebe31668d739c8a228a/gradle/providedConfiguration.gradle
/**
* Adds a configuration named 'provided'. 'Provided' dependencies
* are incoming compile dependencies that aren't outgoing
* dependencies. In other words, they have no effect on transitive
* dependency management.
*/
configurations {
provided
providedPlusCompile.extendsFrom(compile, provided)
testCompile.extendsFrom(providedPlusCompile)
}
sourceSets.main {
compileClasspath = configurations.providedPlusCompile
}
plugins.withType(IdeaPlugin) {
idea.module.scopes.PROVIDED.plus = [ configurations.provided ]
}
Adding to the answer from Adrijardi, for Gadle 2.0 + not only did I have to change
scopes.PROVIDED.plus += configurations.provided
to
scopes.PROVIDED.plus += [configurations.provided]
I also had to change
provided project(":module-name") {
transitive = false
}
to
provided (project(":module-name")) {
transitive = false
}
Note the extra set of brackets on the second code sample
I am trying to get my existing Android Project working with gradle and IntelliJ 12.
Previously I had it working with maven but that didn't seem to be so flexible as gradle, and from what I think I got to know is that I need less subfolders.
My Android project is divided into a clean java library (:core) and the actual Application (:android). These two projects are both in my main project folder.
~-+MainProject
|--+core
| L--build.gradle
|--+android
| L--build.gradle
|--build.gradle
L--settings.gradle
I think the ideal solution to get gradle work with this is to treat the (:core) and (:android) project as nested projects, meaning I can simply be with my cmd in MainProject source folder to start the gradle tasks.
However I came up with different problems:
Gradle dependencies are only included in the core project
that means neither in the instrumentatetionTest nor in the main project files are any
directories correctly set as source / test directories
IntelliJ doesn't resolve any classes that I added as dependencies in gradle (AndroidSDK, junit, mockito, those from the :core project)
tried to use the plugin 'idea' but either I used it wrong or it didn't solve the problem
I get duplicate dependency Error for building the APK
must have something to do with junit:4.11 and mockito-core:1.9.5 that I added
what I tried:
deleting the dependencys -> build fails cause some classes of course could'nt be resolved
changing to junit:4.5+ as suggesed in some other thread -> no change at all
Here are the *.gradle configuartions
MainProject:
-- settings.gradle
include ':core', ':android'
-- build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
subprojects {
repositories {
mavenLocal()
maven { url "http://repo.maven.apache.org/maven2" }
}
}
:core
-- build.gradle
apply plugin: 'java'
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.9.5'
}
:android
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile project(":core")
compile 'com.google.android:android:4.1.1.4'
instrumentTestCompile 'junit:junit:4.11'
instrumentTestCompile 'org.mockito:mockito-core:1.9.5'
instrumentTestCompile 'com.google.dexmaker:dexmaker:1.0'
instrumentTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:3.6'
}
/* ... androidSettings
I hope someone could help me with that
MFG Dornathal
Alright, so you've got the right idea, but there are a few changes you still need.
Your root build.gradle file should be as follows:
subprojects {
repositories {
mavenCentral()
}
}
You only need to include mavenLocal() if you are using a locally installed repo. Most people don't, and nothing in your project indicates that you need one.
mavenCentral() can be used to replace to maven URL you were using.
We only need to modify the buildscript for the android project, so we should localize that to the android project's build.gradle.
Your settings.gradle and your build.gradle for the core project are good.
Your build.gradle for the android project however, needs some changes:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
dependencies {
compile project(":core")
instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:3.6'
}
By including the android plugin, we link directly to the installed SDK. This means we no longer need to include the android dependency: compile 'com.google.android:android:4.1.1.4'.
We don't need to include junit. It's provided by the SDK, so we'll just use that. One thing to note is that the SDK only includes JUnit 3.
We don't need to include mockito and dexmaker unless we actually use it for the android tests. If it's only being used for the tests on the java library, we don't need it here.
To answer your questions:
I'm not sure what you're asking here. It might help if you should your project structure including the directories in which you have files and such.
Your hunch is correct. Hamcrest, which provides matchers for tests, made a breaking change to their API between versions 1.1 and 1.3. JUnit 4.11 has a dependency on Hamcrest 1.3. Mockito 1.9.5 however, depends on Hamcrest 1.1. So when both are included, the 1.3 overrides the 1.1 & Mockito no longer works. Reverting to JUnit 4.5 is also a problem. Junit 4.5 includes the Hamcrest 1.1 jar as a file rather than as a dependency in the POM. So this will cause issues when we have 2 versions of the same file. JUnit 4.10 is the way you want to go here. This has the dependency on Hamcrest 1.1 and includes it in the POM instead of as a file. Win win! I've had issues with this too, and the best way for me to figure it out was to just look at the POM files on Maven Central and see what they tell me.
One final note. Intellij 12 cannot handle Gradle Multi-project builds. You need to switch to Android Studio or Intellij 13 for that.